This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes Error detection in Client Server Communication Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "Error detection in Client Server Communication" Watch "Error detection in Client Server Communication" New topic
Author

Error detection in Client Server Communication

Samiul Karim
Greenhorn

Joined: Sep 30, 2002
Posts: 9
Hi All!
I have a client-server application in which I send packets from the server to the client in compressed form. From the server, I first send the size of the compressed bytes (the data size), followed by the actual compressed bytes (the data).
At the client end, I first read the size of the compressed bytes and then read in that many bytes from the socket. This is done for every packet sent by the server. There is no delimiter between any two packets.
Now, I have a doubt that since I am dependent on the data size, if somehow the data size for one packet becomes erroneous, then all subsequent packets will become erroneous as well. Let us consider the following scenario:-
1. Packet1: Length ---> 20 (sent as 2 bytes)
Data -----> The first 20 bytes available
2. Packet2: Length ---> 25 (sent as 2 bytes)
Data -----> The next 25 bytes available
Now, if somehow the client gets the length of packet1 as 21 instead of 20. Thus, what will happen now is that the client will get the first 20 bytes of the data of packet1 plus 1 byte (the MSB) from the length of packet2. Thus, in this case, not only has packet1 become corrupt, but so has packet2 since the length of packet2 has now become 5 instead of 25. As a result, all packets after packet2 will also become corrupt in this way.
Can there be some way such that even if the length of any 1 packet is received incorrectly, only that packet will be affected and no other?
Waiting for a reply from all you learned people out there....
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Write a checksum on each packet either at the end or the beginning. After you read the packet recalcute the checksum on the client and compare the two for integrity. If they don't match request that the packet be resent. This scenario may require some modification of both client and server but should not be very difficult to implement.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Error detection in Client Server Communication
 
Similar Threads
Network assignment HELP NEEDED
online multiplayer snake game: packet overhead
Why notify/notifyAll() method doesnt invoke a waiting thread quickly?????
LAN help
Threading and collections problem.Pl help