SocketAsyncEventArgs 개체에서 버퍼를 사용해야합니다. 기존의 소켓 방식으로SocketAsyncEventArgs 개체에서 버퍼를 사용하는 방법
우리는 다음과 같이 우리의 상태 개체를 캐스팅 것 :
clientState cs = (clientState)asyncResult.AsyncState;
그러나, 3.5 프레임 워크는 다르다.
클라이언트에서 문자열이 청크로 도착했기 때문에 버퍼가 어떻게 작동하는지 확인할 수 없어서 char3을 발견 할 때 전체 문자열을 처리 할 수 있습니다. 순간
코드 :
private void ProcessReceive(SocketAsyncEventArgs e)
{
string content = string.Empty;
// Check if the remote host closed the connection.
if (e.BytesTransferred > 0)
{
if (e.SocketError == SocketError.Success)
{
Socket s = e.UserToken as Socket;
//asyncResult.AsyncState;
Int32 bytesTransferred = e.BytesTransferred;
// Get the message received from the listener.
content += Encoding.ASCII.GetString(
e.Buffer, e.Offset, bytesTransferred);
if (content.IndexOf(Convert.ToString((char)3)) > -1)
{
e.BufferList = null;
// Increment the count of the total bytes receive by the server
Interlocked.Add(ref this.totalBytesRead, bytesTransferred);
}
else
{
content += Encoding.ASCII.GetString(
e.Buffer, e.Offset, bytesTransferred);
ProcessReceive(e);
}
}
else
{
this.CloseClientSocket(e);
}
}
}
사용하여 코드를 수정 한'Buffer' 또는'BufferList', 그러나 결코 모두 같은 시간에. [To Wit] (https://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.bufferlist%28v=vs.90%29.aspx)'버퍼가 비 -null 값이며 BufferList 속성을 null이 아닌 값으로 설정하려고 시도하면 예외가 발생합니다.'BufferList가 null이 아니므로 'BufferList'를'null '로 설정하는 것은 의미가 없습니다. null인데,이 경우'BufferList'는 null이되거나'Buffer'는 null입니다.이 경우'GetString'은 이미'ArgumentNullException'을 던집니다. –