的SocketAsyncEventArgs缓冲区满零的 [英] SocketAsyncEventArgs buffer is full of zeroes

查看:708
本文介绍了的SocketAsyncEventArgs缓冲区满零的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写我的分布式系统的消息层。我使用IOCP,即Socket.XXXAsync方法。

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

下面的东西pretty的接近我在做什么(其实,我的接收功能是基于他的): <一href="http://vadmyst.blogspot.com/2008/05/sample-$c$c-for-tcp-server-using.html">http://vadmyst.blogspot.com/2008/05/sample-$c$c-for-tcp-server-using.html

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

什么现在我已经发现的是,在节目的开始(两个测试服务器互相交谈),我每次都会获得一些SAEA对象,其中.Buffer完全用零填充的,然而, .BytesTransferred是(在我的案件10​​24),缓冲区的大小。

这是什么意思?是否有一个特殊的情况,我需要检查?我的系统间$ P $点这是一个完整的消息并移动,但我想知道如果我确实错过了一些数据。我下的是IM pression,如果正在接受什么,你不会得到一个回调。在任何情况下,我可以在Wireshark中看到,没有任何零长度的数据包进入。

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.

我发现下面当我用Google搜索,但我不知道我的问题是一样的: <一href="http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/40fe397c-b1da-428e-a355-ee5a6b0b4d2c">http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/40fe397c-b1da-428e-a355-ee5a6b0b4d2c

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

<一个href="http://go4answers.webhost4life.com/Example/socketasynceventargs-buffer-not-ready-121918.aspx">http://go4answers.webhost4life.com/Example/socketasynceventargs-buffer-not-ready-121918.aspx

推荐答案

我肯定不是在链接的例子回事。这似乎是使用异步套接字以同步方式。我看不到任何回调或code相似。您可能需要重新考虑是否需要同步或异步插口。)

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 :).

要在手头的问题,从你的函数试图在网络传输/之前,读/写缓冲区接收已完成的可能性茎。尝试使用包含在异步的Socket回调的功能。例如,

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 ......
}

这篇关于的SocketAsyncEventArgs缓冲区满零的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆