Python program for two way communication using socket programming (client-server chat)
File: server.py
import socket

server_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host=socket.gethostname()
port=5001

server_socket.bind((host,port))
server_socket.listen(5)
client_socket,address=server_socket.accept()

while True:
    client=client_socket.recv(2048)
    print("Client> ",client)
    client_socket.sendall(bytearray(input("You> "),"utf-8"))
client_socket.close()   
server_socket.close()
File: client.py
import socket

client_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host=socket.gethostname()
port=5001

client_socket.connect((host,port))

while True:
    print("You> ",end='')
    server=(bytearray(input(),"utf-8"))
    client_socket.sendall(server)
    server=client_socket.recv(2048)
    print("Server> ",server)
client_socket.close()
Output (Terminal-1)
kodingwindow@kw:~$ python3 server.py
Client>  b'Hi Server'
You> Hello Client 
Client>  b'How are you?'
You> Thank You. I am fine. How about you?
Client>  b'I am good.'
You> Bye. Catch you later.
Client>  b'Okay. Bye'
You> 
Output (Terminal-2)
kodingwindow@kw:~$ python3 client.py
You> Hi Server
Server>  b'Hello Client'
You> How are you? 
Server>  b'Thank You. I am fine. How about you?'
You> I am good.
Server>  b'Bye. Catch you later.'
You> Okay. Bye
Advertisement