Created
December 14, 2010 05:51
-
-
Save noqisofon/740051 to your computer and use it in GitHub Desktop.
MulticastOption インスタンスの使用方法を示すかもしれないサンプルコード。
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
using System; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Text; | |
// このサンプルコードは MulticastOption クラスの使用方法の一例を示すリスナークラスです。 | |
// 特に、MulticastOption(IPAddress, IPAddress) は複数のホストを持っている場合に使用する必要があるコンストラクタです。 | |
// 最初のパラメーターは マルチキャストグループアドレスを指定します。 | |
// 2 つ目はデータ交換に使用するネットワークカードのローカルアドレスを指定します。 | |
// 次のように送信側のプログラムと一緒にこのプログラムを実行する必要があります: | |
// コンソールウィンドウを開き、コマンドラインからリスナーを実行します。 | |
// 別のコンソールウィンドウでは送信側のプログラムを開きます。 | |
// どちらの場合でも、ローカル IP アドレスを指定する必要があります。 | |
// このアドレスは、コマンドラインから ipconfig コマンドを実行して入手してください。 | |
namespace demo.multicasting.option { | |
/// <summary> | |
/// | |
/// </summary> | |
class MulticastOptionSample { | |
/// <summary> | |
/// | |
/// </summary> | |
private IPEndPoint group_endpoint_; | |
/// <summary> | |
/// | |
/// </summary> | |
private Socket cast_socket_; | |
/// <summary> | |
/// | |
/// </summary> | |
private MulticastOption cast_option_; | |
/// <summary> | |
/// | |
/// </summary> | |
private void multicastOptionProperties() { | |
if ( cast_option_ != null ) { | |
Console.WriteLine( "current multicast group is: {0}", cast_option_.Group ); | |
Console.WriteLine( "current multicast local address is: {0}", cast_option_.LocalAddress ); | |
} else { | |
Console.WriteLine( "current multicast group is: none" ); | |
Console.WriteLine( "current multicast local address is: none" ); | |
} | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
private void startMulticast(IPAddress address, int port) { | |
group_endpoint_ = new IPEndPoint( address, port ); | |
try { | |
cast_socket_ = new Socket( AddressFamily.InterNetwork, | |
SocketType.Dgram, | |
ProtocolType.Udp | |
); | |
Console.Write( "enter the local IP address: " ); | |
IPAddress local_ip_address = IPAddress.Parse( Console.ReadLine() ); | |
EndPoint local_endpoint = new IPEndPoint( local_ip_address, group_endpoint_.Port ); | |
cast_socket_.Bind( local_endpoint ); | |
// マルチキャストグループアドレスとローカル IP アドレスを指定して | |
// MulticastOption オブジェクトを定義します。 | |
// マルチキャストグループアドレスは、サーバーによって使用されるアドレスと同じです。 | |
cast_option_ = new MulticastOption( group_endpoint_.Address, local_ip_address ); | |
cast_socket_.SetSocketOption( SocketOptionLevel.IP, | |
SocketOptionName.AddMembership, | |
cast_option_ | |
); | |
} catch ( Exception e ) { | |
Console.WriteLine( e.Message ); | |
Environment.Exit( 0 ); | |
} | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
private void receiveBroadcastMessages() { | |
bool done = false; | |
byte[] bytes = new byte[100]; | |
EndPoint remote_endpoint = new IPEndPoint( IPAddress.Any, 0 ); | |
try { | |
while ( !done ) { | |
Console.WriteLine( "waiting for multicast packets......." ); | |
Console.WriteLine( "enter ^C to terminate." ); | |
cast_socket_.ReceiveFrom( bytes, ref remote_endpoint ); | |
Console.WriteLine( "received broadcast from {0} :\n {1}\n", | |
group_endpoint_.ToString(), | |
Encoding.ASCII.GetString( bytes, | |
0, | |
bytes.Length | |
) | |
); | |
} | |
cast_socket_.Close(); | |
} catch ( Exception e ) { | |
Console.WriteLine( e.Message ); | |
Environment.Exit( 0 ); | |
} | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="args"></param> | |
public void run(string[] args) { | |
// マルチキャストアドレスグループとマルチキャストポートを初期化します。 | |
// 両方のアドレスとポートは、関連する RFC 文書で定義されているように決められたセットから選ばれます。 | |
// これらは、送信者によって使用される値と同じです。 | |
// マルチキャストグループを起動します。 | |
startMulticast( IPAddress.Parse( "224.168.100.2" ), 11000 ); | |
// MulticastOption のプロパティを表示します。 | |
multicastOptionProperties(); | |
// ブロードキャストメッセージを受信します。a | |
receiveBroadcastMessages(); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="args"></param> | |
static void Main(string[] args) { | |
MulticastOptionSample progn = new MulticastOptionSample(); | |
progn.run( args ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment