100% found this document useful (1 vote)
204 views

Chat Application Using TCP/IP

This document contains code for a simple chat program between a TCP server and client in Python. The server code establishes a connection on port 1234 and waits for a client to connect. It receives the client's name and sends its own name. Messages are sent and received between the server and client until one exits. The client code connects to the server, sends and receives names, and allows sending and receiving messages until exiting.

Uploaded by

Ayan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
204 views

Chat Application Using TCP/IP

This document contains code for a simple chat program between a TCP server and client in Python. The server code establishes a connection on port 1234 and waits for a client to connect. It receives the client's name and sends its own name. Messages are sent and received between the server and client until one exits. The client code connects to the server, sends and receives names, and allows sending and receiving messages until exiting.

Uploaded by

Ayan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Name- Ayan Sadhukhan

Regd Number- 19BCE1872


Faculty In-charge- Subbulakshmi P

Question:
Write the simple chat program to establish a connection between TCP server
and TCP client. Establish a connection between a server and client. Send a
message from client and display the same message in server and send back
to the client.
Language Used - Python

Code 1(Server Code):

# server.py
import time, socket, sys

print("\nWelcome to Chat Room\n")


print("Initialising....\n")
time.sleep(1)

s = socket.socket()
host = socket.gethostname()
ip = socket.gethostbyname(host)
port = 1234
s.bind((host, port))
print(host, "(", ip, ")\n")
name = input(str("Enter your name: "))

s.listen(1)
print("\nWaiting for incoming connections...\n")
conn, addr = s.accept()
print("Received connection from ", addr[0], "(", addr[1], ")\n")

s_name = conn.recv(1024)
s_name = s_name.decode()
print(s_name, "has connected to the chat room\nEnter [e] to exit chat room\n")
conn.send(name.encode())

while True:
message = input(str("Me : "))
if message == "[e]":
message = "Left chat room!"
conn.send(message.encode())
print("\n")
break
conn.send(message.encode())
message = conn.recv(1024)
message = message.decode()
print(s_name, ":", message)
Code 2(Client Code):
# client.py
import time, socket, sys

print("\nWelcome to Chat Room\n")


print("Initialising....\n")
time.sleep(1)

s = socket.socket()
shost = socket.gethostname()
ip = socket.gethostbyname(shost)
print(shost, "(", ip, ")\n")
host = input(str("Enter server address: "))
name = input(str("\nEnter your name: "))
port = 1234
print("\nTrying to connect to ", host, "(", port, ")\n")
time.sleep(1)
s.connect((host, port))
print("Connected...\n")

s.send(name.encode())
s_name = s.recv(1024)
s_name = s_name.decode()
print(s_name, "has joined the chat room\nEnter [e] to exit chat room\n")

while True:
message = s.recv(1024)
message = message.decode()
print(s_name, ":", message)
message = input(str("Me : "))
if message == "[e]":
message = "Left chat room!"
s.send(message.encode())
print("\n")
break
s.send(message.encode())

Output :

You might also like