I want to send data more than once. I have the following code on server and client:
On server :
import socket
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(14,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)
serversocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host="10.168.1.50"
port=80
print(host)
print(port)
serversocket.bind((host,port))
serversocket.listen(5)
print('server started listening')
while 1:
(clientsocket,address)=serversocket.accept()
print("connection established from : ",address)
data=clientsocket.recv(1024).decode()
print(data)
if (data=='hai'):
GPIO.output(14,True)
GPIO.output(15,False)
print 'hello'
else:
GPIO.output(14,False)
GPIO.output(15,False)
clientsocket.send("data is sent".encode())
On client:
import socket
s = socket.socket()
host = "10.168.1.50"
port = 80
s.connect((host,port))
while True:
in_data=raw_input(" Enter data to be sent > ")
s.send(in_data.encode())
s.send('hai'.encode())
data = ''
data = s.recv(1024).decode()
print (data)
s.close
I send the first string, get the response, but when I send the second string, it hangs. How can I solve this?
s.closehere but i guess on your side you intends.close()and that's your problem...you're closing the connection after the first send.