Shoaib_3 0 Newbie Poster

Hello all i have asked a question related to this and i have solved the previous problem but now i am looking something more advance and i want to know a scenerio , i have implemented the SOCKETASYNCEVENTARGS class for a chat server in c# and it is working fine i have a pool of SAEA objects for connect , one for send and one for receive event.All is working fine but consider a scenerio where server will send more than two or two messages at the same time to a client as described for every client there will be only one SAEA object which will send messages to that specific client, but if server will send two message at a time to a single client ,it will cause an exception ("socket async operation is already in progress) something like this so how to tackle this this is my send code

public void StartSend(SocketAsyncEventArgs receiveSendEventArgs)
{
    DataHoldingUserToken receiveSendToken = (DataHoldingUserToken)receiveSendEventArgs.UserToken;
    if (receiveSendToken.sendBytesRemainingCount <= this.socketListenerSettings.BufferSize)
    {
        receiveSendEventArgs.SetBuffer(receiveSendToken.bufferOffsetSend, receiveSendToken.sendBytesRemainingCount);
        Buffer.BlockCopy(receiveSendToken.dataToSend, receiveSendToken.bytesSentAlreadyCount, receiveSendEventArgs.Buffer, receiveSendToken.bufferOffsetSend, receiveSendToken.sendBytesRemainingCount);
    }
    else
    {
        receiveSendEventArgs.SetBuffer(receiveSendToken.bufferOffsetSend, this.socketListenerSettings.BufferSize);
        Buffer.BlockCopy(receiveSendToken.dataToSend, receiveSendToken.bytesSentAlreadyCount, receiveSendEventArgs.Buffer, receiveSendToken.bufferOffsetSend, this.socketListenerSettings.BufferSize);
    }
    bool willRaiseEvent = receiveSendEventArgs.AcceptSocket.SendAsync(receiveSendEventArgs);
    if (!willRaiseEvent)
    {
        ProcessSend(receiveSendEventArgs);
    }            
}

and finally

private void ProcessSend(SocketAsyncEventArgs receiveSendEventArgs)
    {
        DataHoldingUserToken receiveSendToken = (DataHoldingUserToken)receiveSendEventArgs.UserToken;
  if (receiveSendEventArgs.SocketError == SocketError.Success)
  {
    receiveSendToken.sendBytesRemainingCount = receiveSendToken.sendBytesRemainingCount - receiveSendEventArgs.BytesTransferred;                 

    if (receiveSendToken.sendBytesRemainingCount == 0)
    { 
        StartReceive(receiveSendEventArgs);
    }
    else
    {                
        receiveSendToken.bytesSentAlreadyCount += receiveSendEventArgs.BytesTransferred;
        StartSend(receiveSendEventArgs);
    }
}
else
{
    receiveSendToken.Reset();
    CloseClientSocket(receiveSendEventArgs);
}            

`

}

`

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.