Python socket receives incomplete message from Java PrintWriter socket

落爺英雄遲暮 提交于 2019-12-02 05:17:25

From the documentation:

recv(buffersize[, flags]) -> data

Receive up to buffersize bytes from the socket. For the optional flags argument, see the Unix manual. When no data is available, block until at least one byte is available or until the remote end is closed. When the remote end is closed and all data is read, return the empty string.

So recv() can return fewer bytes than you ask for, which is what's happening in your case. There is discussion of this in the socket howto.

Basically you need to keep calling recv() until you have received a complete message, or the remote peer has closed the connection (signalled by recv() returning an empty string). How you do that depends on your protocol. The options are:

  1. use fixed sized messages
  2. have some kind of delimiter or sentinel to detect end of message
  3. have the client provide the message length as part of the message
  4. have the client close the connection when it has finished sending a message. Obviously it will not be able to receive a response in this case.

Looking at your Java code, option 4 might work for you because it is sending a message and then closing the connection. This code should work:

s = socket.socket()         
host = socket.gethostname() 
# host = "192.168.0.20"
port = 12345  
s.bind((host, port))

s.listen(5)                 
while True:
    c, addr = s.accept()     
    # print 'Got connection from', addr

    message = []
    chars_remaining = 8192
    recv_buf = c.recv(chars_remaining)
    while recv_buf:
        message.append(recv_buf)
        chars_remaining -= len(recv_buf)
        if chars_remaining = 0:
            print("Exhausted buffer")
            break
        recv_buf = c.recv(chars_remaining)

    # print message
    message = ''.join(message).strip()
    ipAddress = addr[0]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!