private void ProcessReceive(SocketAsyncEventArgs e)
{
// Check if the remote host closed the connection.
if (e.BytesTransferred > 0)
{
if (e.SocketError == SocketError.Success)
{
Token token = e.UserToken as Token;
token.SetData(e);
Socket s = token.Connection;
if (s.Available == 0)
{
Boolean willRaiseEvent = false;
// GET DATA TO SEND
byte[] sendBuffer = token.GetRetBuffer();
// this.bufferSize IS SocketAsyncEventArgs buffer SIZE
byte[] tempBuffer = new byte[this.bufferSize];
int offset = 0;
int size = (int)Math.Ceiling((double)sendBuffer.Length / (double)this.bufferSize);
for (int i = 0; i < size - 1; i++)
{
Array.Clear(tempBuffer, 0, this.bufferSize);
Array.Copy(sendBuffer, offset, tempBuffer, 0, this.bufferSize);
e.SetBuffer(tempBuffer, 0, this.bufferSize);
willRaiseEvent = s.SendAsync(e);
offset += this.bufferSize;
}
int remainSize = sendBuffer.Length - this.bufferSize * (size - 1);
Array.Clear(tempBuffer, 0, this.bufferSize);
Array.Copy(sendBuffer, offset, tempBuffer, 0, remainSize);
e.SetBuffer(tempBuffer, 0, remainSize);
willRaiseEvent = s.SendAsync(e);
if (!willRaiseEvent)
{
this.ProcessSend(e);
}
}
else if (!s.ReceiveAsync(e))
{
// Read the next block of data sent by client.
this.ProcessReceive(e);
}
}
else
{
this.ProcessError(e);
}
}
else
{
this.CloseClientSocket(e);
}
}
This code is modified from MSDN
Why in circulation, execute s.SendAsync(e)
the second time, it will be error
Exception:An asynchronous socket operation is already in progress using this SocketAsyncEventArgs instance
How can I send large data ?