SocketAsyncEventArgs Buffer Filled With Zeros

I am writing a message layer for my distributed system. I am using IOCP, i.e. Socket.XXXAsync methods.

Here is something very close to what I am doing (in fact, my reception function is based on it): http://vadmyst.blogspot.com/2008/05/sample-code-for-tcp-server-using .html

What I found now is that at the beginning of the program (two test servers talk to each other) every time I get several SAEA objects where .Buffer is completely filled with zeros, but .BytesTransferred is the size of the buffer (1024 in my case )

What does it mean? Is a special condition required? My system interprets this as an incomplete message and moves on, but I wonder if I really miss some data. I got the impression that if nothing is received, you will not receive a callback. In any case, I see in WireShark that there are no packets of zero length.

I found the following when I google it, but I'm not sure my problem is this: http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/40fe397c-b1da-428e-a355- ee5a6b0b4d2c

http://go4answers.webhost4life.com/Example/socketasynceventargs-buffer-not-ready-121918.aspx

+6
source share
1 answer

I am sure that does not happen in a related example. Apparently, it uses synchronous asynchronous sockets. I do not see any callbacks or similar in the code. You may need to rethink whether you need synchronous or asynchronous sockets :).

The problem at hand implies that your functions are trying to read / write to the buffer until the transmission / reception of the network is complete. Try using the callback functions included in async socket. For instance.

// This goes into your accept function, to begin receiving the data socketName.BeginReceive(yourbuffer, 0, yourbuffer.Length, SocketFlags.None, new AsyncCallback(OnRecieveData), socketName); // In your callback function you know that the socket has finished receiving data // This callback will fire when the receive is complete. private void OnRecieveData(IAsyncResult input) { Socket inSocket = (Socket)input.AsyncState; // This is just a typecast inSocket.EndReceive(input); // Pull the data out of the socket as you already have before. // state.Data.Write ...... } 
0
source

Source: https://habr.com/ru/post/917107/


All Articles