Python socket receives incomplete message from Java PrintWriter socket

前端 未结 1 944
时光取名叫无心
时光取名叫无心 2021-01-27 01:17

I made a python \"queue\" (similar to a JMS protocol) that will receive questions from two Java clients. The python-server will receive the message from one of the Java clients

1条回答
  •  执笔经年
    2021-01-27 01:54

    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]
    

    0 讨论(0)
提交回复
热议问题