6

I'm writing a message layer for my distributed system. I'm using IOCP, ie the Socket.XXXAsync methods.

Here's something pretty close to what I'm doing (in fact, my receive function is based on his): http://vadmyst.blogspot.com/2008/05/sample-code-for-tcp-server-using.html

What I've found now is that at the start of the program (two test servers talking to each other) I each time get a number of SAEA objects where the .Buffer is entirely filled with zeroes, yet the .BytesTransferred is the size of the buffer (1024 in my case).

What does this mean? Is there a special condition I need to check for? My system interprets this as an incomplete message and moves on, but I'm wondering if I'm actually missing some data. I was under the impression that if nothing was being received, you'd not get a callback. In any case, I can see in WireShark that there aren't any zero-length packets coming in.

I've found the following when I Googled it, but I'm not sure my problem is the same: 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

Carlos
  • 5,991
  • 6
  • 43
  • 82
  • I haven't had any data processing errors from lost data, so I think there's nothing lost. But that still leaves the question of why the method would return with nothing, several times. – Carlos Jun 08 '12 at 10:16
  • 1
    It is hard to provide solution for you need without your brief description (with code). However I had experience with Sockets and I can recommend to use higher abstraction - Network Stream. Network Stream has all capabilities of Async callback that you want to use. Look at http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.aspx and http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.beginwrite.aspx HTH – Daniel Skowroński Jun 12 '12 at 15:20
  • Is it the same underlying calls? It sounds useful. Also, the code I'm referring to is in the link. – Carlos Jun 14 '12 at 07:12
  • 1
    I don't have an answer for you, but maybe this will provide some insight: a while back I was doing a project in .NET 3.5 for a university course with my friend. We encountered a strange behavior, where on one version version of Windows, the Buffer was empty, and on the other it was full of zeroes. The version were WinXP and Win7, but I don't recall which way around. – Paweł Motyl Jun 14 '12 at 14:01
  • 1
    @Carlos Yes Network Service is only abstraction to standard Socket communication, and extended to more new useful features. I recommend that you create PoC (Proof of Concept) project and try it. It is worth a try. – Daniel Skowroński Jun 14 '12 at 22:31

1 Answers1

0

I am sure not what is going on in the linked example. It appears to be using asynchronous sockets in a synchronous way. I cannot see any callbacks or similar in the code. You may need to rethink whether you need synchronous or asynchronous sockets :).

To the problem at hand stems from the possibility that your functions are trying to read/write to the buffer before the network transmit/receive has been completed. Try using the callback functionality included in the async Socket. E.g.

// 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 ......
}
themartinmcfly
  • 2,004
  • 2
  • 13
  • 12
  • The callbacks are the event handlers on the SAEA objects. I've actually run the code with some minimal grammatical errors. – Carlos Jul 13 '12 at 18:15
  • Is it possible for you to paste your receiver code into pastebin or similar, I have only seen the example you are basing yours upon. – themartinmcfly Jul 16 '12 at 02:59