Created
June 21, 2011 07:32
-
-
Save noqisofon/1037399 to your computer and use it in GitHub Desktop.
マルチキャスト編。
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Text; | |
using System.Threading; | |
namespace test.async.multicast.host { | |
/// <summary> | |
/// | |
/// </summary> | |
class MulticastHostSample { | |
/// <summary> | |
/// | |
/// </summary> | |
public void run() { | |
// 使用するアドレッシングスキームを表します。。 | |
AddressFamily use_address_family = AddressFamily.InterNetwork; | |
// ローカルポート番号を決定する乱数ジェネレーターです。 | |
Random random = new Random(); | |
// ローカルポート番号です。 | |
//int local_port = random.Next( 49152, 65535 ); | |
int local_port = 51947; | |
// マルチキャストグループ用のエンドポイントです。 | |
IPEndPoint multicast_point = new IPEndPoint( IPAddress.Parse( "239.0.0.0" ), 64500 ); | |
// リモートエンドポイントです。 | |
IPEndPoint remote_point = null; | |
// 送信された文字列です。 | |
string received_text = string.Empty; | |
// マルチキャスト用クライアントを作成します。 | |
UdpClient multicast_client = new UdpClient( local_port, use_address_family ); | |
try { | |
// クライアントをマルチキャストグループに加入させます。 | |
multicast_client.JoinMulticastGroup( multicast_point.Address, 50 ); | |
Thread.Sleep( 1000 ); | |
Encoding transfer_encoding = Encoding.UTF8; | |
byte[] receive_bytes = null; | |
Console.WriteLine( "local end point: {0}", multicast_client.Client.LocalEndPoint ); | |
// 待ち時間です。 | |
TimeSpan waiting_time = TimeSpan.FromMilliseconds( random.Next( 80, 120 ) ); | |
// 受信したバイト数です。 | |
int amount_of_bytes_received = 0; | |
// 受信が完了したことを通知するためのシグナルイベントです。 | |
ManualResetEvent received = new ManualResetEvent( false ); | |
/* | |
* 受信を開始します。 | |
*/ | |
IAsyncResult receiving = multicast_client.BeginReceive( onReceiveCompleted, received ); | |
do { | |
if ( receiving.AsyncWaitHandle.WaitOne( waiting_time ) ) | |
break; | |
} while ( !receiving.IsCompleted ); | |
receive_bytes = multicast_client.EndReceive( receiving, ref remote_point ); | |
Console.WriteLine( "remote point: {0}", remote_point ); | |
amount_of_bytes_received = receive_bytes.Length; | |
Console.WriteLine( "{0} バイト受信しました。", amount_of_bytes_received ); | |
received_text = transfer_encoding.GetString( receive_bytes ); | |
// マルチキャストグループからクライアントを脱退させます。 | |
multicast_client.DropMulticastGroup( multicast_point.Address ); | |
} catch ( SocketException se ) { | |
Console.WriteLine( se ); | |
} | |
Console.WriteLine( received_text ); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="receiving"></param> | |
private void onReceiveCompleted(IAsyncResult receiving) { | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="args"></param> | |
static void Main(string[] args) { | |
MulticastHostSample progn = new MulticastHostSample(); | |
progn.run(); | |
} | |
} | |
} | |
// Local Variables: | |
// coding: utf-8 | |
// mode: ecmascript | |
// End: | |
// MulticastGuestSample.cs end here |
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Text; | |
using System.Threading; | |
namespace test.async.multicast.host { | |
/// <summary> | |
/// | |
/// </summary> | |
class MulticastHostSample { | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="args"></param> | |
public void run(string[] args) { | |
// 使用するアドレッシングスキームを表します。。 | |
AddressFamily use_address_family = AddressFamily.InterNetwork; | |
// ローカルポート番号を決定する乱数ジェネレーターです。 | |
Random random = new Random(); | |
// ローカルポート番号です。 | |
int local_port = 64500; | |
// マルチキャストグループのエンドポイントです。 | |
//IPEndPoint multicast_point = new IPEndPoint( IPAddress.Parse( "239.0.0.0" ), random.Next( 49152, 65535 ) ); | |
IPEndPoint multicast_point = new IPEndPoint( IPAddress.Parse( "239.0.0.0" ), 51947 ); | |
// マルチキャスト用のクライアントを作成します。 | |
UdpClient multicast_client = new UdpClient( local_port, use_address_family ); | |
try { | |
// クライアントをマルチキャストグループに加入させます。 | |
multicast_client.JoinMulticastGroup( multicast_point.Address, 50 ); | |
Thread.Sleep( 1000 ); | |
Encoding transfer_encoding = Encoding.UTF8; | |
string sent_text = "Hello, Everyone!"; | |
byte[] sent_bytes = transfer_encoding.GetBytes( sent_text ); | |
Console.WriteLine( "local end point: {0}", multicast_client.Client.LocalEndPoint ); | |
// 待ち時間です。 | |
TimeSpan waiting_time = TimeSpan.FromMilliseconds( random.Next( 80, 120 ) ); | |
// 送信したバイト数です。 | |
int amount_of_bytes_sent = 0; | |
// 送信が完了したことを通知するためのシグナルイベントです。 | |
ManualResetEvent sent = new ManualResetEvent( false ); | |
/* | |
* 送信を開始します。 | |
*/ | |
IAsyncResult sending = multicast_client.BeginSend( sent_bytes, | |
sent_bytes.Length, | |
multicast_point, | |
onSendCompleted, | |
sent ); | |
do { | |
/* | |
* UDP の BeginSend メソッドから EndSend メソッドまでは短いため、すぐに break されます。 | |
*/ | |
if ( sending.AsyncWaitHandle.WaitOne( waiting_time ) ) | |
break; | |
} while ( !sending.IsCompleted ); | |
amount_of_bytes_sent = multicast_client.EndSend( sending ); | |
Console.WriteLine( "{0} バイト送信しました。", amount_of_bytes_sent ); | |
// マルチキャストグループからクライアントを脱退させます。 | |
multicast_client.DropMulticastGroup( multicast_point.Address ); | |
} catch ( SocketException se ) { | |
Console.WriteLine( se ); | |
} | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="sent"></param> | |
private void onSendCompleted(IAsyncResult sending) { | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="args"></param> | |
static void Main(string[] args) { | |
MulticastHostSample progn = new MulticastHostSample(); | |
progn.run( args ); | |
} | |
} | |
} | |
// Local Variables: | |
// coding: utf-8 | |
// mode: ecmascript | |
// End: | |
// MulticastHostSample.cs end here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment