0

My question is about the way to properly treat data that are received by using a tcp connection. In fact by establishing a tcp connection a Stream is created.Suppose I want to send a message which has a beginning and an end. As the data are flowing in the stream without specifying any boundaries, how can i identify the beginning and the end of a message. I thought to put some special characters at the beginning and at the end of my message in order to recognize them but I wonder if it is a proper way to do. My question is therefore how can i properly establish boundaries to a message for a tcp connection? (I'm using Node.js for client side and java for server side)

thank you in advance

masterbate
  • 19
  • 1
  • 7

1 Answers1

0

A plain TCP connection needs some sort of protocol which defines the data format so the receiving end knows how to interpret what is being sent. For example, http is one such protocol, webSocket is another. There are thousands of existing protocols. I'd suggest you find one that is a good match for what you want to do and use it rather than building your own.

Different protocols use different schemes for defining the data format and thus different ways of delineating pieces of your data. For example in http, it uses \n to delineate headers and then use xxxx: yyyy on each line and then uses a blank line to delineate the end of the headers.

Other protocols use a binary format that define message packets with a message type, a message length and a message payload.

There are literally hundreds of different ways to do it. Since there are so many pre-built choices out there, one can usually find an existing protocol that is a decent match and use a pre-built server and client for each end rather than writing your own protocol generating and parsing code.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • thank jfriend00 for your answer. I read some documentation about existing protocols. My goal is to do a messaging app on android but there is a plenty of protocols and I'm confuse about which one to choose. I notice that there are existing modules like socket.io for creating pre-built websocket server/client or node-xmpp for the xmpp protocol but I dont know which one would match. You certainly have more experience in that field than me so I'm asking you which one would be the good one and provide a good documentation in order to implement it on client side(android/java) and server side(node)? – masterbate Nov 19 '17 at 14:09
  • @masterbate - Well, you've not described your requirements at all, but for messaging where you want to send messages one or both ways and you want to maintain a persistent connection between the two endpoints, then socket.io would be a good match. Beyond that, you'd have to provide a lot more detail about what you're trying to do. There are literally probably more than a thousand protocols already defined so it's not like anyone can describe them all in an answer here. socket.io has pre-built libraries to implement the protocol for both nodejs and android. – jfriend00 Nov 19 '17 at 17:12
  • My requirement is to establish a persistent connection between the client and the server (not a p2p connection). But I looked for more detailed information and socket.io seems to be what i'm looking for. Thank you a lot jfriend00 for sharing your experience. – masterbate Nov 19 '17 at 18:30