- PR -

ソケットについて

1
投稿者投稿内容
choco
会議室デビュー日: 2003/09/18
投稿数: 14
投稿日時: 2003-10-03 10:44
いつもお世話になっております。

環境[Win2k Pro(SP4) .NET2003 C#]

非同期ソケットについて調べていたところ、
MSDNにある「非同期クライアント ソケットの使用」というページ
を見つけたので試してみました。

BeginReceiveでCallBack関数を呼んでいますが、
このCallBack関数で、受信バッファサイズが0になったら
受信Eventをシグナルにしています。↓(Source)
---------------------------------------------------------------
private static void ReceiveCallback( IAsyncResult ar ) {
try {
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject) ar.AsyncState;
Socket client = state.workSocket;
// Read data from the remote device.
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0) {
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead));
// Get the rest of the data.
client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
new AsyncCallback(ReceiveCallback), state);
} else {
// All the data has arrived; put it in response.
if (state.sb.Length > 1) {
response = state.sb.ToString();
}
// Signal that all bytes have been received.
receiveDone.Set();
}
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}
---------------------------------------------------------------

このEventをシグナルにする見方ですが、私の環境では
最初に受信したデータの後に再度BeginReceiveを行って
CallBackを抜けた後、再びこのCallBackに入ってくることは
ありません(最初のEndReciveで全てのデータを受信できていて、
再度CallBack関数が呼ばれない)。

この場合はどうしたら良いのでしょうか?
試した手段としてはソケットのAvailableを見て
バッファが0であればEventをセットするようにしたのですが、
これも受信データのサイズが大きい場合有効じゃありませんでした。

どなたかご存知の方がおられましたらご教授お願い致します。










1

スキルアップ/キャリアアップ(JOB@IT)