Data type via socket

Asked

Viewed 191 times

1

Is there any way/trick/algorithm that allows me to 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 treat differently. Any idea?

1 answer

1


Would it be TCP/IP socket? If so, you get a "torrent" of bytes. You have to have an application protocol, known on both sides, to know what is being received. There is no way you just 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 fully 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 sending messages in an atomic way - the application always receives an indivisible "package", and each message can have a label, which makes it easy to identify the type of message. But unfortunately the SCTP is little used, the TCP is what is used and the application protocol has to carry this burden.

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

  • Thanks @epx, I even created a protocol, which requires that before text is sent a string to identify. It was the simplest and most effective way to solve.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.