TCP Server Resetting AcceptSocket.ReceiveAsync operation on a SocketAsyncEventArgs instance

时间:2015-07-23 19:08:49

标签: c# server tcp-ip

I currently have a TCP Server implemented using C# SAEA. What I would like to do is forward a message between 2 TCP Clients connected to the server (Client 1 and Client 2).

  • The Server uses receiveSendEventArgs.AcceptSocket.ReceiveAsync and receiveSendEventArgs.AcceptSocket.SendAsync commands to send and receive information from each of the connected clients with no problem.
  • The Server is in receiveSendEventArgs.AcceptSocket.ReceiveAsync operation for both Client 1 and Client 2.
  • Client 1 sends one Message and the the Server Accepts the message. The Server sees that Client 2 is also connected, and so needs to get the receiveSendEventArgs reference to Client 2 to forward the message.

However, the Server takes the reference of receiveSendEventArgs of Client 2 and begins to prepare the buffer (SetBuffer) to send the message and I believe since the Socket is still in a "ReceiveSync" state for Client 2, it blows up with the following message:

"An asynchronous socket operation is already in progress using this SocketAsyncEventArgs instance."

Is there a way to switch Client 2 state on the Server from "ReceiveAsync"to "SendAsync" so that it doesn't error out when I try to SendData to Client 2? I know the Completed event is triggered when either Sending or Receiving operations complete, however, Simply Calling my IO_Completed method directly does not change the operation.

In a for loop setting up EventHandlers for Completed events for SocketAsyncEventArgs: eventArgObjectForPool.Completed += new EventHandler(IO_Completed);

void IO_Completed(object sender, SocketAsyncEventArgs e){

        DataHoldingUserToken receiveSendToken = (DataHoldingUserToken)e.UserToken;
        //More business logic here ...
        // determine which type of operation just completed and call the associated handler
        switch (e.LastOperation)
        {
            case SocketAsyncOperation.Receive:
                if (Program.watchProgramFlow == true)   //for testing
                {
                    Program.testWriter.WriteLine("IO_Completed method in Receive, receiveSendToken id " + receiveSendToken.TokenId);
                }                    
                ProcessReceive(e);
                break;

            case SocketAsyncOperation.Send:
                if (Program.watchProgramFlow == true)   //for testing
                {
                    Program.testWriter.WriteLine("IO_Completed method in Send, id " + receiveSendToken.TokenId);
                }

                ProcessSend(e);
                break;

            default:
                //This exception will occur if you code the Completed event of some
                //operation to come to this method, by mistake.
                throw new ArgumentException("The last operation completed on the socket was not a receive or send");
        }
    }

private void StartReceive(SocketAsyncEventArgs receiveSendEventArgs) {

        DataHoldingUserToken receiveSendToken = (DataHoldingUserToken)receiveSendEventArgs.UserToken;

        if (Program.watchProgramFlow == true)   //for testing
        {                
            Program.testWriter.WriteLine("StartReceive(), receiveSendToken id " + receiveSendToken.TokenId);
        }

        switch (receiveSendToken.clientInfo.currentState)
        {
            case MyClient.ClientState.Connecting://This occurs when we get client to connect for first time. However, it will automatically disconnect

                receiveSendToken.theMediator.HandleData(receiveSendToken.theDataHolder);

                // Create a new DataHolder for next message.
                receiveSendToken.CreateNewDataHolder();

                //Reset the variables in the UserToken, to be ready for the
                //next message that will be received on the socket in this
                //SAEA object.
                receiveSendToken.Reset(true);

                receiveSendToken.theMediator.PrepareOutgoingData();
                StartSend(receiveSendToken.theMediator.GiveBack());

                //******************************************************************
                break;

            default:
                //Set the buffer for the receive operation.
                receiveSendEventArgs.SetBuffer(receiveSendToken.bufferOffsetReceive, this.socketListenerSettings.BufferSize);                    

                // Post async receive operation on the socket.
                bool willRaiseEvent = receiveSendEventArgs.AcceptSocket.ReceiveAsync(receiveSendEventArgs);

                //Socket.ReceiveAsync returns true if the I/O operation is pending. The 
                //SocketAsyncEventArgs.Completed event on the e parameter will be raised 
                //upon completion of the operation. So, true will cause the IO_Completed
                //method to be called when the receive operation completes. 
                //That's because of the event handler we created when building
                //the pool of SocketAsyncEventArgs objects that perform receive/send.
                //It was the line that said
                //eventArgObjectForPool.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed);

                //Socket.ReceiveAsync returns false if I/O operation completed synchronously. 
                //In that case, the SocketAsyncEventArgs.Completed event on the e parameter 

                if (!willRaiseEvent)
                {
                    if (Program.watchProgramFlow == true)   //for testing
                    {
                        Program.testWriter.WriteLine("StartReceive in if (!willRaiseEvent), receiveSendToken id " + receiveSendToken.TokenId);
                    }

                    ProcessReceive(receiveSendEventArgs);
                }
                break;
        }
    }

private void StartSend(SocketAsyncEventArgs receiveSendEventArgs) { DataHoldingUserToken receiveSendToken = (DataHoldingUserToken)receiveSendEventArgs.UserToken;

        if (Program.watchProgramFlow == true)   //for testing
        {
            Program.testWriter.WriteLine("StartSend, id " + receiveSendToken.TokenId);
        }
        if (Program.watchThreads == true)   //for testing
        {
            DealWithThreadsForTesting("StartSend()", receiveSendToken);
        }

        if (receiveSendToken.sendBytesRemainingCount <= this.socketListenerSettings.BufferSize)
        {
            Program.testWriter.WriteLine("blocking:?(" + receiveSendEventArgs.AcceptSocket.Blocking + ")");
            receiveSendEventArgs.SetBuffer(receiveSendToken.bufferOffsetSend, receiveSendToken.sendBytesRemainingCount);
            //Copy the bytes to the buffer associated with this SAEA object.
            Buffer.BlockCopy(receiveSendToken.dataToSend, receiveSendToken.bytesSentAlreadyCount, receiveSendEventArgs.Buffer, receiveSendToken.bufferOffsetSend, receiveSendToken.sendBytesRemainingCount);
        }
        else
        {
            //We cannot try to set the buffer any larger than its size.
            //So since receiveSendToken.sendBytesRemainingCount > BufferSize, we just
            //set it to the maximum size, to send the most data possible.
            receiveSendEventArgs.SetBuffer(receiveSendToken.bufferOffsetSend, this.socketListenerSettings.BufferSize);
            //Copy the bytes to the buffer associated with this SAEA object.
            Buffer.BlockCopy(receiveSendToken.dataToSend, receiveSendToken.bytesSentAlreadyCount, receiveSendEventArgs.Buffer, receiveSendToken.bufferOffsetSend, this.socketListenerSettings.BufferSize);

            //We'll change the value of sendUserToken.sendBytesRemainingCount
            //in the ProcessSend method.
        }

        //post asynchronous send operation
        bool willRaiseEvent = receiveSendEventArgs.AcceptSocket.SendAsync(receiveSendEventArgs);

        if (!willRaiseEvent)
        {
            if (Program.watchProgramFlow == true)   //for testing
            {
                Program.testWriter.WriteLine("StartSend in if (!willRaiseEvent), receiveSendToken id " + receiveSendToken.TokenId);
            }

            ProcessSend(receiveSendEventArgs);
        }            
    }

1 个答案:

答案 0 :(得分:1)

这不是我想要做的事情,但我可以打电话给以下人员:

receiveSendEventArgs.AcceptSocket.Write(strMyBuffer);

这允许我写入同一个套接字,我没有任何错误。我想坚持使用异步命令,但却屈服于同步写入,它不会影响我的服务器。也许还有其他解决方案。