Data type via socket

1

Is there any way / trick / algorithm to let me know what kind of data is coming via socket? I can send both text and files via socket, but I'd like to know what I'm getting to handle differently. Any ideas?

    
asked by anonymous 09.10.2015 / 16:10

1 answer

1

Would TCP / IP socket? If so, you get a "stream" of bytes. You have to have an application protocol, known from both sides, to know what is being received. There is no way you simply send a data via TCP without any labeling, and the other side "guess" what it is.

It could be something based on JSON, for example - when a valid JSON message is completely received, you know that the first message is over, and the remaining bytes are already part of a second message.

There are protocols like SCTP that allow you to send messages atomically - the application always receives an indivisible "package", and each message can have a label, which makes it easy to identify the type of the message. But unfortunately SCTP is little used, TCP is what you use and the application protocol has to carry this burden.

There are many libraries that do this service - use a TCP channel to send and receive indivisible and typed messages. XMPP is one of them.

    
11.10.2015 / 07:25