Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket Fragmented Received Data

I'm trying to create some kind of client monitor, like a terminal, to receive data from a serial device over ethernet. I'm trying to use a socket with python, but the problem comes when I create the connection. I'm supposed to receive only one message from the server, and I get the whole message but split into two packets, like this:

Message expected:

   b'-- VOID MESSAGE--'

Message received:

   b'-- VOID'
   b' MESSAGE--'

I don't know if is this a problem of buffer size, decoding or any other function

import socket        

TCP_IP = '192.168.#.#'
TCP_PORT = ### 
BUFFER_SIZE = 1024
data1=' '

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

while(1):
    data = s.recv(BUFFER_SIZE)
    print(data.decode('ASCII'))


s.close()

I've already tried with some codecs options like UTF-8, UTF-16 and ASCII but I still get the same result.


This function helped me to solve the issue.

while(1):                                           
    cadena += s.recv(1)                            
    if (((cadena)[i])=='\n'):       
        print(cadena.decode('ASCII'))               
        cadena=b''                                  
        i=-1                                        

    i+=1 
like image 937
Daniel Hernández Avatar asked May 15 '26 05:05

Daniel Hernández


1 Answers

As it already was said - that's how sockets works. Sent data could be splitted to chunks. So if you want to be sure, that you've received whole message that was sent you need to implement some kind of protocol, the part of which will be contain length of your message. For example:

  • First four bytes (integer) represents length of the message
  • Other bytes - content of the message

In such case algorithm to send a message will be look like:

  • Count length of the message
  • Write to socket integer (4 bytes) with message length
  • Write to socket content of the message

And reading algorithm:

  • Read bytes from socket and write read data to accumulator-buffer
  • Read first four bytes from buffer as integer - it will be message length
  • Check if buffer length is greater or equal "{message length} + 4"
  • If it's then read required amount of bytes and that will message that was sent.
  • Drop first "{message length} + 4" bytes from buffer
  • Repeat from second point
  • If it's not enough bytes to read message content repeat from first point.
like image 63
mayakwd Avatar answered May 16 '26 18:05

mayakwd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!