Last post Jul 28, 2020 07:06 AM by XuDong Peng
Jul 27, 2020 09:26 AM|robin0323|LINK
I encountered a strange problem.
I write a net project, one client and one server
The package structure is 4 byte HEAD and a lot of byte data of
BODY, the 4 byte is saved how many byte data of body.
I have receive package in a single thread while(true), it seems good. but....
I have use wiresahrk to sniffer package, and when the 4 byte HEAD to splited in two tcp package. I call receive 4 byte and it return, but it has extra ‘00’ byte.
for example :
package 1 :
the end of package of tcp : 96 00 00 ---------> it seem good, when i call receive
HEAD, it not enough 4 byte, so it need read next tcp package to got 4 byte.
the begin of ackage of tcp: 00 7b 22 ..... etc ------------> it seem good.
I found the packge of HEAD is 96 00 00 00 , it seems ok, but when i receive BODY，the bytes is
00 7b 22 ..... etc,
the 00 extra received twice!
This probmle it not definitely appear. It is occurd when the HEAD of 4 byte to splite two package of tcp.
Can someone please help me with this! I've tried everything and I just cannot figure it out! Thanks in advance
int size = NoticeSession.Receive(buffer, offset, Size - offset, SocketFlags.None, out err);
offset += size;
if (err != SocketError.Success)
Console.WriteLine("size：" + size + ", err:" + err.ToString());
} while (offset < Size);
Jul 28, 2020 07:06 AM|XuDong Peng|LINK
C# Socket.receive continuously receives 0 bytes and does not block in the loop
As the reply to the above case said, you need to detect the reception of 0 bytes and treat it as the end.
On the other hand, TCP is stream-based. Don't expect the received bytes to match the buffer you sent.
If I misunderstood your situation, can you provide more details? Mainly include sample code.