単純な状況:
Socket 上のクライアントは、ファイル (データ) の一部 (たとえば、256 バイト) をbyte []
サーバーに送信します。サーバーは非同期でデータを受信します。ファイル (データ) が完全に送信されたことを確認する方法は? (サーバ側)
データの受信を担当するサーバー側のコードは次のとおりです
public static void ReadCallback(IAsyncResult ar)
{
String content = String.Empty;
// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
// Read data from the client socket.
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
BinaryWriter writer = new BinaryWriter(File.Open(@"D:\test.png", FileMode.Append));
writer.Write(state.buffer, 0, bytesRead);
writer.Close();
// All the data has been read from the
// client. Display it on the console.
Console.WriteLine("Read {0} bytes from socket!",
bytesRead);
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
}
いくつかの次のことを可能にする方法はありますか?
if (bytesRead > 0)
{
....
if(state.buffer!=end of receive)
{
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
}
または、このbyte[]
オブジェクトに何らかの情報 (<EOF>
タグ付きの文字列など) を追加しようとするかもしれませんが、各ステップでこの情報を分析する必要があります。このチェックをもっと簡単に、そしてどのように行うことができますか? または別の方法を使用します...