0

I have this client Python program, sending to a Java server program. My client was working fine before adding the while loop. I needed to add a while loop to keep sending user input; now the text does not appear on the other end until I kill the program (the Python client), then all the text I entered in Python will appear on the other end, but only after killing the program! What causes this?

import socket

HOST = "192.168.0.76"
PORT = 8080

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
while (True):
    sock.sendall(raw_input(""))
Brian L
  • 10,757
  • 5
  • 19
  • 17
aero
  • 372
  • 1
  • 4
  • 18
  • You probably need a new thread to send the message. What is happening is that your program is blocking all the messages by repeating the loop before getting a chance to send anything. – OneCricketeer May 23 '16 at 21:59
  • i thought of that,and i added a time sleep for 2 seconds... didn't work – aero May 23 '16 at 22:00
  • That's because that only pauses the one single thread you are on. – OneCricketeer May 23 '16 at 22:00
  • I'm not sure what you mean by adding a new thread? – aero May 23 '16 at 22:03
  • Sounds to me like something somewhere is buffering the data. – user2357112 May 23 '16 at 22:04
  • A [thread](https://en.wikipedia.org/wiki/Thread_(computing)) is a programming term. – OneCricketeer May 23 '16 at 22:05
  • @user2357112 i know, it sounds like that to me too, iam just wondering why,cricket is talking about threading and maybe he's right although i don't know how that works, i'm not sure why it has to be that complex since it's just a while loop. – aero May 23 '16 at 22:12
  • Sockets are a streaming protocol, so you can't use TCP for this. There is no EOT (end-of-transmission) on one. The only way to ensure all the data has been sent would be to close the socket. See the [Using a Socket](https://docs.python.org/2/howto/sockets.html#using-a-socket) section in the [Socket Programming HOWTO](https://docs.python.org/2/howto/sockets.html#using-a-socket). – martineau May 24 '16 at 01:05

2 Answers2

0

Taking a quick look at the socket docs, I noticed that there is a setsockopt() function. If you look at the docs (man setsockopt), there's a SO_SNDBUF flag, which controls the buffer size while sending. If you didn't send enough, it probably doesn't flush until there's more data.

However, there doesn't appear to be a way to flush sockets (due to TCP frame sizes), and this related answer might give you some more hints: Python Socket Flush

Community
  • 1
  • 1
wli
  • 726
  • 5
  • 5
  • On a related note, have you considered using websockets instead? It should give you quick communications of small data bits without you having to deal with this lowlevel stuff. – wli May 23 '16 at 22:35
-1

change the loop to:

while True:
    input = raw_input("")
    sock.send(input)
Danny
  • 344
  • 1
  • 4
  • 15
  • `sock.send` does not guarantee to send all of the given buffer, the number of bytes sent must be explicitly checked, this approach will cause data loss. – donkopotamus May 23 '16 at 22:43