Socket.ConnectAsync接收400个错误请求,而Socket.Connect获得200个ok

时间:2017-01-07 14:21:56

标签: c# .net sockets

我想使用套接字连接到服务。当我使用Socket.Connect时,响应是200 ok我可以成功接收数据,但是当我使用Socket.ConnectAsync时,响应是400错误请求。

以下是Socket.Connect的代码:

SocketAsyncEventArgs myev;
private void Receive(SocketAsyncEventArgs e)
{
    myev = e;
    SocketAsyncEventArgs readEventArgs = m_readWritePool.Pop();
    readEventArgs.RemoteEndPoint = remote;
    Socket connectSocket;
    connectSocket = new Socket(remote.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
    connectSocket.Connect(remote);//Sync Connect 
    ProcessConnect(readEventArgs);
}
private void ProcessConnect(SocketAsyncEventArgs e)
{
    AsyncUserToken token = (AsyncUserToken)myev.UserToken;
    if (myev.BytesTransferred > 0 && myev.SocketError == SocketError.Success)
    {
        e.SetBuffer(myev.Offset, myev.BytesTransferred);
        bool willRaiseEvent = ((AsyncUserToken)e.UserToken).Socket.SendAsync(e);
        if (!willRaiseEvent)
        {
            ProcessServerSend(e);
        }
    }
    else
    {
        CloseClientSocket(e);
    }
}

以下是Socket.ConnectAsync:

的代码
SocketAsyncEventArgs myev;
private void Receive(SocketAsyncEventArgs e)
{
    myev = e;
    SocketAsyncEventArgs readEventArgs = m_readWritePool.Pop();
    readEventArgs.RemoteEndPoint = remote;
    Socket connectSocket;
    connectSocket = new Socket(remote.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
    ((AsyncUserToken)readEventArgs.UserToken).Socket = connectSocket;
    bool willRaiseEvent = connectSocket.ConnectAsync(readEventArgs);//Async Connect
    if (!willRaiseEvent)
    {
        ProcessConnect(readEventArgs);
    }

}
private void ProcessConnect(SocketAsyncEventArgs e)
{
    AsyncUserToken token = (AsyncUserToken)myev.UserToken;
    if (myev.BytesTransferred > 0 && myev.SocketError == SocketError.Success)
    {
        e.SetBuffer(myev.Offset, myev.BytesTransferred);
        bool willRaiseEvent = ((AsyncUserToken)e.UserToken).Socket.SendAsync(e);
        if (!willRaiseEvent)
        {
            ProcessServerSend(e);
        }
    }
    else
    {
        CloseClientSocket(e);
    }
}

请注意,我们已将m_readWritePool定义为SocketAsyncEventArgsPool m_readWritePool;并填写了1000个项目。

我们已将ProcessServerSend定义为:

private void ProcessServerSend(SocketAsyncEventArgs e)
{
    if (e.SocketError == SocketError.Success)
    {                
        AsyncUserToken token = (AsyncUserToken)e.UserToken;
        SocketAsyncEventArgs readEventArgs = m_readWritePool.Pop();
        ((AsyncUserToken)readEventArgs.UserToken).Socket = token.Socket;         
        bool willRaiseEvent = token.Socket.ReceiveAsync(readEventArgs);
        if (!willRaiseEvent)
        {
            ProcessServerReceive(readEventArgs);
        }
    }
    else
    {
        CloseServerSocket(e);
    }
}

ProcessServerReceive如下:

private void ProcessServerReceive(SocketAsyncEventArgs e)
{                   
    AsyncUserToken token = (AsyncUserToken)myev.UserToken;
    if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
    {
        myev.SetBuffer(e.Offset, e.BytesTransferred);
        byte[] buf = new byte[e.BytesTransferred];
        for (int i = 0; i < e.BytesTransferred; i++)
        {
            buf[i] = e.Buffer[e.Offset + i];
        }
        string message = Encoding.UTF8.GetString(buf);
        //The message contains '200 ok' or '400 bad request' when using Connect or ConnectAsync respectively.
        bool willRaiseEvent = token.Socket.SendAsync(myev);
        if (!willRaiseEvent)
        {
            ProcessSend(myev);
        }
    }
    else
    {
        CloseServerSocket(e);
    }
}

请注意,所有功能都是相同的连接方法!我检查发送的消息是否一样!

更新

回复是:

错误请求 - 动词无效

HTTP错误400.请求谓词无效。

1 个答案:

答案 0 :(得分:1)

出现此问题的原因是使用SocketAsyncEventArgs变量与1024个元素(1024个元素的空缓冲区)初始化的缓冲区进行连接。我通过在readEventArgs.SetBuffer(0, 0);设置零长度缓冲区之前添加以下代码行来解决问题:Async Connect。因此,我的Receive函数已更改为:

private void Receive(SocketAsyncEventArgs e)
{
    myev = e;
    SocketAsyncEventArgs readEventArgs = m_readWritePool.Pop();
    readEventArgs.RemoteEndPoint = remote;
    Socket connectSocket;
    connectSocket = new Socket(remote.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
    ((AsyncUserToken)readEventArgs.UserToken).Socket = connectSocket;
    readEventArgs.SetBuffer(0, 0);//This line of code resolves my issue.
    bool willRaiseEvent = connectSocket.ConnectAsync(readEventArgs);//Async Connect
    if (!willRaiseEvent)
    {
        ProcessConnect(readEventArgs);
    }
}