Is there a standard practice when exchanging data through Sockets? I want to transfer some data that is not fixed length. Currently, I am first sending the length of the data as an int and then following it up with the actual data. I was just wandering if there's an RFC or something that defines a standard for this. I tried Googling it but the results are not what I am looking for, they rather point to articles explaining about Socket communication in Java.
And also curious to know one more thing, if I don't know the length of the data and if I just have to keep sending them to the client and when I eventually know that I must stop, how do I signal the client?
I went to using Servlets and JSP to do what you are asking, having followed the same way of going about the problem. In general, you have to have solved much of the problem by being able to send an int. If you could send an int, as a control header, could you send a header field consisting of a byte array, such as:
You can pack a lot of control information in that much space. Sockets refer to the machine to machine connection in general, consistent with what you have found already. The authors of the O'Reilly title on Java Network Programming state that java can be a platform for protocol development, but I have not gotten far enough with the book try it.
Originally posted by Srikanth Raghavan: if I don't know the length of the data and if I just have to keep sending them to the client and when I eventually know that I must stop, how do I signal the client?
One option is to close the connection. That signals the client that you've finished, but it ends the session completely.
If you don't want to do that, then you need to send a special code to signal the end of the transmission. Of course once you decide on that, you need to make sure that the special code can't occur anywhere inside the message. If it can, then you need to invent a procedure for escaping it, along with a procedure for escaping whatever character you chose to escape it.
This was first done (as far as I know) in the 1930's by the Telex network. They had STX and ETX characters to indicate Start of Text and End of Text, and those characters couldn't occur in the text itself. And you'll recognize the "escaping" strategy if you've done anything at all with XML, for example. [ March 31, 2008: Message edited by: Paul Clapham ]
posted 12 years ago
Thanks Nicholas and Paul.
I've also seen that STX and ETX used quite often. Seems like that's a good protocol to use when we don't know the size of the data well ahead.
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad: