开发者

Sockets programming: Message failing check. Does anyone know a fanthom of a reason?

Right now I have a communication infrastructure compose of a client and server.

The client connects to the server using standard TCP sockets.

I have a message structure that is as follows:

4 bytes -- Message size
n bytes -- Message
4 bytes -- CRC32 checksum

One of the requirement is that to b开发者_运维技巧e valid the message must pass the CRC32 check on the other end of the connection, either the client or the server process the messages the same way.

If the message fails the CRC32 check, the connection is severed and a new connection established.

My question is why the heck I get CRC32 failures at random?

For no apparent reason, even with both client and server on the same machine using loopback address (127.0.0.1).

I thought that even though I've programmed the failsafe in case of a malicious third party or something, I would never see a connection to be dropped during my tests.


You didn't show any code, so I can only guess.

  • You are reading bytes from the sockets without checking the size read. TCP is a stream-oriented protocol so there are no guarantees regarding the number of reads you have to perform to get the entire data sent. The only guarantee is that after an unspecified number of reads, using an unspecified number of segments, you will get all the octets, in order

  • Your checksum functions fails for some inputs because it is incorrect

The first one is probably what's going on. You're reading some data and recv / read returns with fewer bytes read than you expect.

As an aside, you do realize what you are trying to do right ?

  • The ethernet frame has a CRC-32 field
  • The IPv4 packet has a 16b header checksum
  • The TCP segment has a 16-b checksum covering both the header, the data and then some
  • Your data will also have a CRC-32

You realize it's redundant, right ?


BTW the correct way to check a checksum at the receiver is to compute it over the entire message and the checksum, considered jointly as a longer message. The result should be zero. That way you are including the checksum itself in the calculation. The wrong way to do it is to compute the checksum over the message excluding the checksum and then compare to the received checksum.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜