Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .Net套接字:如何更好地实现AsyncCallback/AsyncWaitHandle_C#_.net_Sockets_Asyncsocket - Fatal编程技术网

C# .Net套接字:如何更好地实现AsyncCallback/AsyncWaitHandle

C# .Net套接字:如何更好地实现AsyncCallback/AsyncWaitHandle,c#,.net,sockets,asyncsocket,C#,.net,Sockets,Asyncsocket,我正在尝试用C#构建一个套接字服务器,其中一个问题是,当我将AsyncCallback传递给套接字的BeginReceive方法时,回调被调用得太快,而当我读取IAsyncResult的AsyncState属性时,我会得到null 我以为我已经通过添加 .AsyncWaitHandle.WaitOne(100); 但问题仍然存在。谁能告诉我哪里做错了 我的代码如下(缩写): private void Main(){ _beginacept(新的异步回调(HandleConnection),nu

我正在尝试用C#构建一个套接字服务器,其中一个问题是,当我将AsyncCallback传递给套接字的BeginReceive方法时,回调被调用得太快,而当我读取IAsyncResult的AsyncState属性时,我会得到null

我以为我已经通过添加

.AsyncWaitHandle.WaitOne(100);
但问题仍然存在。谁能告诉我哪里做错了

我的代码如下(缩写):

private void Main(){
_beginacept(新的异步回调(HandleConnection),null);
}
私有无效句柄连接(IAsyncResult状态)
{
SocketError socketRecieveError;
_connectionSocket=\u listener.EndAccept(状态);
_connectionSocket.BeginReceive(_-receivedDataBuffer,0,_-receivedDataBuffer.Length,0,out-socketRecieveError,
新的异步回调(HandleHandshake),\u connectionSocket.Available);
if(socketRecieveError!=SocketError.Success)
_Log(“套接字错误:+socketRecieveError”);
}
私有无效握手(IAsyncResult状态)
{
status.AsyncWaitHandle.WaitOne(1000);
int握手长度;
尝试
{

handshakeLength=Convert.ToInt32(status.AsyncState);//IAsyncResult的AsyncState属性包含用户定义的对象,该对象包含有关异步操作()的信息。这与调用异步操作时作为参数“state”提供的对象相同

在代码中,您提供了_connectionSocket.Available作为用户状态。我想您希望为回调方法提供接收的字节数?这不是正确的方法

正确的方法是:

private void HandleConnection(IAsyncResult status)
{
    SocketError socketRecieveError;

    _connectionSocket = _listener.EndAccept(status);
    _connectionSocket.BeginReceive(_receivedDataBuffer, 0, _receivedDataBuffer.Length, 0, out socketRecieveError, new AsyncCallback(HandleHandshake), null);

    if(socketRecieveError !=  SocketError.Success)
        _logger.Log("Socket error: " + socketRecieveError);
}

private void HandleHandshake(IAsyncResult ar)
{
    int bytesReceived = _connectionSocket.EndReceive(ar);

    ....
}

太好了,谢谢……我想你的意思是“status”是EndRecieve方法的参数?
private void HandleConnection(IAsyncResult status)
{
    SocketError socketRecieveError;

    _connectionSocket = _listener.EndAccept(status);
    _connectionSocket.BeginReceive(_receivedDataBuffer, 0, _receivedDataBuffer.Length, 0, out socketRecieveError, new AsyncCallback(HandleHandshake), null);

    if(socketRecieveError !=  SocketError.Success)
        _logger.Log("Socket error: " + socketRecieveError);
}

private void HandleHandshake(IAsyncResult ar)
{
    int bytesReceived = _connectionSocket.EndReceive(ar);

    ....
}