C#:远程主机上的套接字ReceiveAsync和SendAsync出现问题

时间:2013-08-22 18:10:53

标签: c# sockets asyncsocket sendasync

首先我想说这个带有服务器/客户端的代码在我的PC上运行正常(在同一台PC上运行的客户端和服务器)

当我在专用服务器上托管服务器时,问题就开始了。我第二次尝试使用客户端发送或接收时,客户端将花费很多时间来完成操作(谈论接收1个字节的数据需要超过1小时!)

**我可以通过在每个发送/接收命令后再次断开和连接套接字来避免这个问题

我不明白为什么如果我在同一个连接会话中做更多1次发送/接收操作需要这么多时间。


以下是一个例子:

连接服务器:

    RC_Server = new SocketConnection_Client(RC_IpEndPoint);

现在客户端连接到服务器,我可以正常开始发送/接收。

我尝试发送或接收它时第一次完美而快速地工作(无论缓冲区长度如何)。

    RC_Server.sendCompleted = false;
    RC_Server.Send(buffer); //in this case buffer size is 1024
    while (!RC_Server.sendCompleted) { } //here thread is waiting for the send to complete

现在,如果我想启动与服务器的接收会话

        RC_Server.receiveCompleted = false;
        RC_Server.Receive(buffer.Length); //in this case byffer needs to get 4 bytes from server
        while (!RC_Server.receiveCompleted) { } // <<- Here it will take VERY long time for the operation to complete
        buffer = RC_Server.buffer;

第二个Receive操作永远不会进入socket_receiveCompleted_Completed事件。


但..如果我将在第一个发送/接收会话后关闭与服务器的连接,然后打开与服务器的新连接,然后执行下一个发送/接收会话, 那么一切都会很完美。 (并且多次复制此方法以进行多次发送/接收)

同样,我的工作站中没有出现问题(如果我在同一台PC上运行服务器和客户端)。只有当我在远程主机上托管服务器时才会发生。

这是我的套接字类的一部分:

    class SocketConnection_Client
    {
        public string errorMsg = string.Empty;
        public bool receiveCompleted = false;
        public bool sendCompleted = false;
        int bytesReceived = 0;
        public byte[] buffer;
        Socket socket;

        public SocketConnection_Client(IPEndPoint ipEP)
        {
            socket = new Socket(ipEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                socket.Connect(ipEP);
            }
            catch (Exception ex)
            {
                errorMsg = ex.Message;
                socket.Dispose();
                socket = null;
            }
        }

        public void Send(byte[] sBuffer)
        {
            buffer = null;
            if (socket == null)
            {
                sendCompleted = true;
                return;
            }
            sendCompleted = false;

            SocketAsyncEventArgs socket_sendCompleted = new SocketAsyncEventArgs();
            socket_sendCompleted.SetBuffer(sBuffer, 0, sBuffer.Length);
            socket_sendCompleted.Completed += socket_sendCompleted_Completed;
            socket.SendAsync(socket_sendCompleted);
        }

        void socket_sendCompleted_Completed(object sender, SocketAsyncEventArgs e)
        {
            sendCompleted = true;
        }

        public void Receive(int bLength)
        {
            bytesReceived = 0;
            buffer = null;
            if (socket == null)
            {
                receiveCompleted = true;
                return;
            }
            receiveCompleted = false;

            buffer = new byte[bLength];
            SocketAsyncEventArgs socket_receiveCompleted = new SocketAsyncEventArgs();
            socket_receiveCompleted.SetBuffer(buffer, 0, buffer.Length);
            socket_receiveCompleted.Completed += socket_receiveCompleted_Completed;
            socket.ReceiveAsync(socket_receiveCompleted);
        }

        private void Receive_Continue()
        {
            SocketAsyncEventArgs socket_receiveCompleted = new SocketAsyncEventArgs();
            socket_receiveCompleted.SetBuffer(buffer, bytesReceived, buffer.Length - bytesReceived);
            socket_receiveCompleted.Completed += socket_receiveCompleted_Completed;
            socket.ReceiveAsync(socket_receiveCompleted);
        }

        void socket_receiveCompleted_Completed(object sender, SocketAsyncEventArgs e)
        {
            bytesReceived += e.BytesTransferred;
            if (e.BytesTransferred == 0 && bytesReceived == 0)
            {
                socket.Dispose();
                socket = null;
                receiveCompleted = true;
                throw new Exception("Server Disconnected");
            }
            else if (bytesReceived != buffer.Length)
                Receive_Continue();
            else
                receiveCompleted = true;
        }
    }

赞赏。 感谢。

1 个答案:

答案 0 :(得分:1)

您忽略了ReceiveAsync的返回值。注意the documentation - 有两种可能的行为,你必须检查返回值以区分它们:

  

如果I / O操作处于挂起状态,则返回true。操作完成后, SocketAsyncEventArgs.Completed 参数上的e事件将被引发。

     

如果I / O操作同步完成,则返回false。 在这种情况下, SocketAsyncEventArgs.Completed 参数上的e事件将不会被提升 e 可以在方法调用返回后立即检查作为参数传递的对象,以检索操作的结果。