Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Standard data exchange protocol?

 
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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?

Thanks!
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 25682
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Srikanth Raghavan
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Srikanth
 
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic