I tried to implement a communication between server and client in python. I need to send a message between them, bidirectional , but I don't know how to make it as a continuous communication, because in my programm connection is closed after server and client sent only a message. Can anybody help me?
This is my code for server :
import socket
serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serv.bind(('0.0.0.0', 8082))
serv.listen(5)
while True:
conn, addr = serv.accept()
from_client=''
while True:
from_client=''
data = conn.recv(4096)
if not data : break
from_client+=data
print from_client
print("Send character to CLIENT ")
input1=str(raw_input())
conn.send(input1)
print("You received from client the next Character")
from_client=''
data = conn.recv(4096)
if not data : break
from_client+=data
print from_client
print'\n'
conn.close()
print 'client disconenct'
This is my code for client :
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('0.0.0.0', 8082))
client.send("I am CLIENT\n")
from_server = client.recv(4096)
print("I received from SERVER %s" %from_server)
print("Send your character to SERVER")
input1=str(raw_input())
client.send(input1)
client.close()
Your connection closed because client.close()
If your client behaves just like the server to repeat to request you send some message to the server and then receives what the server send, you can create a Ping-Pong application that one side repeats input-send-receive cycle while the other repeats receive-input-send cycle.