2
TcpClient
구현을 작성하려고합니다. 내 데이터를 처리하는 동안 다음 읽기 시작 싶었지만 바이트 배열 BeginRead
전달 될 때 확실하지 오전 수정되었습니다. 여기에 내가 IO 내가 처리하는 동안 데이터의 다음 청크를 받기 시작하는 배경을 원했기 때문에 나는 장기 실행 기능하기 전에 다음 BeginRead
을 시작 나의 현재 구현BeginRead의 버퍼는 언제 채워 집니까?
private void ProcessData()
{
byte[] buffer = new byte[_tcpClient.ReceiveBufferSize];
var networkStream = _tcpClient.GetStream();
IAsyncResult result = networkStream.BeginRead(buffer, 0, buffer.Length, null, null);
int read;
while ((read = networkStream.EndRead(result)) != 0)
{
result = networkStream.BeginRead(buffer, 0, buffer.Length, null, null);
ProcessChunk(buffer, read);
}
}
입니다. 하나의 byte[]
버퍼로이 작업을 수행 할 수 있습니까? 아니면 BeginRead
호를 번갈아 수행해야합니까?
private void ProcessData()
{
bool bufferChoice = false;
byte[] bufferA = new byte[_tcpClient.ReceiveBufferSize];
byte[] bufferB = new byte[_tcpClient.ReceiveBufferSize];
var networkStream = _tcpClient.GetStream();
IAsyncResult result = networkStream.BeginRead(bufferA, 0, bufferA.Length, null, null);
int read;
while ((read = networkStream.EndRead(result)) != 0)
{
if (bufferChoice)
{
result = networkStream.BeginRead(bufferA, 0, bufferA.Length, null, null);
ProcessChunk(bufferB, read); //Call function with B's buffer
}
else
{
result = networkStream.BeginRead(bufferB, 0, bufferB.Length, null, null);
ProcessChunk(bufferA, read); //Call function with A's buffer
}
bufferChoice = !bufferChoice;
}
}
여러 개의 버퍼를 수행하는 방법에 대한 "예제 코드"를 작성한 후 간단하게 다른 방법으로 변경 한 것을 알았습니다. –