Created
December 21, 2010 08:49
-
-
Save noqisofon/749678 to your computer and use it in GitHub Desktop.
複数のゲストから受信するためにスレッド内で実行する関数。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void receivePart(object param) { | |
pair<TcpClient, ManualResetEvent> tmpair = (pair<TcpClient, ManualResetEvent>)param; | |
TcpClient client = tmpair.first; | |
ManualResetEvent receive_complete = tmpair.second; | |
NetworkStream stream = null; | |
// ループカウンタ。 | |
int loop_count = 0; | |
// 待機時間。 | |
TimeSpan waiting_time = TimeSpan.FromMilliseconds( 100 ); | |
// 読み込まれたバイト数。 | |
int amount_of_bytes_read = 0; | |
// 読み込みに使われるバイト配列。 | |
byte[] byte_array_for_reading = new byte[8096]; | |
// バイト配列をエンコードした文字列。 | |
string received_text = string.Empty; | |
try { | |
stream = client.GetStream(); | |
IAsyncResult = read_done = stream.BeginRead( byte_array_for_reading, | |
0, | |
byte_array_for_reading.Length, | |
onReceiveCompleted, | |
null ); | |
/* | |
* 受信待機ループです。 | |
*/ | |
while ( true ) { | |
// WaitHandle の waitOne メソッドで受信終了を少しだけ待機します。 | |
if ( read_done.AsyncWaitHandle.WaitOne( waiting_time ) ) { | |
/* | |
* シグナルを捕捉したので待機ループを終了します。1 | |
*/ | |
amount_of_bytes_read = stream.EndRead( read_done ); | |
receiced_text = Encoding.UTF8.GetString( byte_array_for_reading, | |
0, | |
amount_of_bytes_read ); | |
Console.WriteLine( "read: {0}", received_text ); | |
Console.WriteLine( "received {0} bytes", amount_of_bytes_read ); | |
receive_complete.Set(); | |
} else { | |
/* | |
* タイムアウトしました。 | |
* 指定された時間までにシグナルを捕捉できませんでした。 | |
*/ | |
if ( loop_count > 30 ) { | |
Console.WriteLine( "time outed." ); | |
break; | |
} | |
} | |
++ loop_count; | |
} | |
} finally { | |
stream.Close(); | |
Console.WriteLine( "close." ); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void run() { | |
TcpListener listener = new TcpListener( getLocalIPEndPoint(), LOCAL_PORT ); | |
Console.WriteLine( "start the TCP communication." ); | |
listener.Start(); | |
try { | |
while ( true ) { | |
if ( listener.Pending() ) { | |
ThreadPool.QueueUserWorkItem( acceptPart, listener ); | |
} else { | |
Console.WriteLine( "sorry, no connection request." ); | |
break; | |
} | |
Thread.Sleep( 100 ); | |
} | |
} finally { | |
listener.Stop(); | |
Console.WriteLine( "stop the TCP communication." ); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void acceptPart(object param) { | |
TcpListener listener = param as TcpListener; | |
// ループカウンタ。 | |
int loop_count = 0; | |
// 非同期情報。 | |
IAsyncResult accept_done = null; | |
// TCP クライアント。 | |
TcpClient client = null; | |
ManualResetEvent receive_complete = new ManualResetEvent( false ); | |
// 待機時間。 | |
TimeSpan wait_time = TimeSpan.FromMilliseconds( 100 ); | |
try { | |
accept_done = listener.BeginAcceptTcpClient( onAcceptComplated, null ); | |
/* | |
* 受信要求待機ループです。 | |
*/ | |
while ( true ) { | |
// WaitHandle の waitOne メソッドで接続要求を少しだけ待機します。 | |
if ( accept_done.AsyncWaitHandle.WaitOne( wait_time ) ) { | |
/* | |
* シグナルを捕捉したので、待機ループを終了します。 | |
*/ | |
client = listener.EndAcceptTcpClient( accept_done ); | |
Console.WriteLine( "accept connections." ); | |
IPEndPoint remote_point = (IPEndPoint)client.Client.RemoteEndPoint; | |
Console.Write( "connection has been requested from {0}.", remote_point ); | |
ThreadPool.QueueUserWorkItem( receivePart, new pair<TcpClient, ManualResetEvent>(client) ); | |
break; | |
} else { | |
/* | |
* タイムアウトしました。 | |
* 指定された時間までにシグナルを捕捉できませんでした。 | |
*/ | |
if ( loop_count > 30 ) | |
return; | |
} | |
++ loop_count; | |
} | |
} finally { | |
if ( client != null ) | |
client.Close(); | |
} | |
// | |
receive_complete.WaitOne(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment