1

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?

2
  • Look at your client code. Second time round the loop, what is different about the socket 's'? Yes- you've closed it:) Commented Jan 12, 2014 at 10:28
  • 1
    Beware you write s.close here but i guess on your side you intend s.close() and that's your problem...you're closing the connection after the first send. Commented Jan 12, 2014 at 10:34

2 Answers 2

1

Here is the code that worked

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')
(clientsocket,address)=serversocket.accept()
print("connection established from : ",address)
while 1:
    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))
try:
    while True:

        in_data=raw_input(" Enter data to be sent > ")
        s.send(in_data.encode())
        data = ''
        data = s.recv(1024).decode()
        print (data)
finally:
    s.close()
Sign up to request clarification or add additional context in comments.

Comments

0

This my client and it's working.

import socket

s = socket.socket()
host = "10.168.1.50"
port = 80
s.connect((host,port))
try:
    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)
finally:
    s.close()

2 Comments

Sorry that alone didn't solve my problem. Modification was needed on the server side too. :) Thanks for the tip.
I had to move (clientsocket,address)=serversocket.accept() above the while. Then it worked :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.