Socket.BeginReceive ...回调错误 [英] Socket.BeginReceive... callback error

查看:110
本文介绍了Socket.BeginReceive ...回调错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

也许我遗漏了一些东西但是下面的代码需要2次迭代才能工作。在下面的代码中,我不明白为什么第一个 OnDataReceived回调是随机发生的。也就是说,当接收到从1字节到1000字节的任何地方时可能会发生这种情况(但它似乎总是少于BeginReceive中指定的数量。所以我在回调中添加了第二个BeginReceive以捕获剩余的字节进入。第二个似乎准确地捕捉到我告诉它确实捕获的数字,但是第一次回叫发生是随机发生的,没有普遍的模式。

任何人都知道为什么?

请参阅下面的内容。

public void SendMessage( int iTxByteCount)

Hello,

Maybe I am missing something but the following code takes 2 iterations to work. In the following code I do not understand why the first OnDataReceived callback occurs randomly. That is, it may occur when anywhere from 1 byte to 1000 bytes are received(but it always seems to be shy of the amount specified in BeginReceive. So I added a second BeginReceive inside the call back to catch the remaining bytes coming in. The second one seems to catch exactly the number I tell it do catch, however the first call back occurance happens randomly with no apperent pattern.

Anyone know why?

See following.

public void SendMessage(int iTxByteCount)

{

 

如果 (ConnectionCreated == true

if (ConnectionCreated == true)

{

ClientSocket.Send(bTxBuf,iTxByteCount, SocketFlags .None);

ClientSocket.Send(bTxBuf, iTxByteCount, SocketFlags.None);



}


public void OnDataReceived( IAsyncResult 结果)

public void OnDataReceived(IAsyncResult Result)

{

int iTempBuildIndex = 0;

int iTempBuildIndex = 0;

int bytesRead ;

int bytesRead;

bytesRead = ClientSocket.EndReceive(Result);

bytesRead = ClientSocket.EndReceive(Result);

尝试

try

{

if (bytesRead> 0)

if (bytesRead > 0)

{

//可能有更多数据,因此存储到目前为止收到的数据。

// There might be more data, so store the data received so far.

while (bytesRead> iTempBuildIndex) //如果收到字节,将它们添加到现有的构造缓冲区(不要使用stringbuilder类)

while (bytesRead > iTempBuildIndex) //if bytes received, add them to the existing contructuction buffer(dont use stringbuilder class)

{

bRxBuf [bTempConstuctIndex ++] = bTempRxBuf [iTempBuildIndex ++];

bRxBuf[bTempConstuctIndex++] = bTempRxBuf[iTempBuildIndex++];

}

if (iResponceBytesExpected> bTempConstuctIndex) //获取其余数据。

if (iResponceBytesExpected > bTempConstuctIndex)// Get the rest of the data.

ClientSocket.BeginReceive(bTempRxBuf,0,(iResponceBytesExpected - ( int )bTempConstuctIndex), SocketFlags .None, < font color ="#0000ff"size = 2> new AsyncCallback (OnDataReceived), null );

ClientSocket.BeginReceive(bTempRxBuf, 0, (iResponceBytesExpected -(int)bTempConstuctIndex), SocketFlags.None, new AsyncCallback(OnDataReceived), null);

else

else

bAsyncRxDone = true ;

bAsyncRxDone = true;

}

else

else

bAsyncRxDone = true ;

bAsyncRxDone = true;

}

catch 例外

catch (Exception)

{

bytesRead = 0;

bytesRead = 0;

}

}

推荐答案

TCP套接字是一种流(不是数据包)抽象。所以,即使你知道1000个字节即将到来,它们也许不会同时到达。

你只需要跟踪已经到达的数量,然后继续阅读你的OnDataReceived还没到。对于TCP套接字来说这是完全正常的。

TCP sockets are a stream (not packet) abstraction. So, even if you know that 1000 bytes are coming, they may not all arrive at the same time.

You just need to keep track of how many have arrived, and then continue reading in your OnDataReceived if they haven't all arrived yet. This is perfectly normal for TCP sockets.

-Steve

P.S。您可以告诉套接字将数据直接放在最终缓冲区而不是临时缓冲区中。这就是"抵消"的意思。参数适用于。

       -Steve

P.S. You can just tell the socket to put the data directly in your final buffer instead of a temporary one. That's what the "offset" parameter is for.


这篇关于Socket.BeginReceive ...回调错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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