receive data with s.recv()

319 views Asked by At

I have this little part of code and I think my computer is connected to my sensor (Gyro sensor) but it has a big problem with recv part. With this s.recv() part my program stuck and no result. Waiting for idea. Thanks in advance

import socket 

TCP_IP = '......'
TCP_PORT = ....                                      
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))

while True:
    data = s.recv(50)
    print data
    if not data:
        break

s.close()
1

There are 1 answers

4
DevShark On

Your problem is likely to be in the code that sends the data.

However, a few comments with your code here:

  • Do a better job to catch exceptions.
  • You are asking to recv only a size of 50 in your socket. If the sender sends less that's when you'll get stuck. Check to see what is the length of the data sent by the sender.
  • You should not use sockets like that without a timeout.
  • Use a print statement after connect to check whether you get stuck in the connect, or in the recv part. That will tell you where to debug your sender code.