0

I am using Python to send information over a socket. I am using the function sendall, but the data isn't actually sent until the end of the program. When I input a 10 second delay after sending the data, the computer at the other end of the connection doesn't receive the data until after the delay finishes and the program ends.

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(10)
conn, addr = s.accept()

data = Arbitary
conn.sendall(data)   #Send the data
print "This is after sending"
time.sleep(10)
print "This is after delay"

What can I do to have the computer at the other end of the connection receive the data as soon as it is sent, rather than when the Python program ends?

I don't think the code on the other computer is necessary, but please let me know if I need to add it.

jalconvolvon
  • 158
  • 1
  • 10

1 Answers1

2

To my mind, you should simply close the connection in your Python code with conn.close() right after conn.sendall. Maybe this data is stored in a buffer (the connection's not closed yet, so you may want to send more data so why waste resources and send it continuously?)

The reason the data is sent only at script shutdown is that then the socket gets flushed and closed automatically.

ForceBru
  • 43,482
  • 10
  • 63
  • 98