Created
December 15, 2010 06:08
-
-
Save noqisofon/741685 to your computer and use it in GitHub Desktop.
740092 を簡潔にした感じ。
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; | |
using System.Threading; | |
namespace demo.multicasting { | |
/// <summary> | |
/// | |
/// </summary> | |
class MulticastingSample { | |
/// <summary> | |
/// | |
/// </summary> | |
void run() { | |
UdpClient client = new UdpClient( 2000, AddressFamily.InterNetwork ); | |
IPAddress group_address = IPAddress.Parse( "239.0.0.0" ); | |
IPEndPoint remote_point = new IPEndPoint( group_address, 1000 ); | |
client.JoinMulticastGroup( group_address ); | |
Thread there_worker = new Thread( multicastConversion ); | |
there_worker.Start(); | |
Thread.Sleep( 2000 ); | |
Console.WriteLine( "Alice:" ); | |
string[] greetings = new string[] { "Hello, Bob.", "Have a nice day.", "Over" }; | |
sendGreetings( client, greetings, remote_point ); | |
string ret = receiveUntilStop( client, greetings[2] ); | |
Console.WriteLine( "Alice received:\n{0}\n", ret ); | |
there_worker.Abort(); | |
client.DropMulticastGroup( group_address ); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
void multicastConversion() { | |
UdpClient client = new UdpClient( 1000, AddressFamily.InterNetwork ); | |
IPAddress group_address = IPAddress.Parse( "239.0.0.0" ); | |
client.JoinMulticastGroup( group_address ); | |
IPEndPoint remote_point = new IPEndPoint( group_address, 2000 ); | |
string ret = receiveUntilStop( client, "Over" ); | |
Console.WriteLine( "Bob received: \n{0}\n", ret ); | |
Thread.Sleep( 2000 ); | |
Console.WriteLine( "\nBob:" ); | |
string[] greetings = new string[] { "Hello, Alice.", "You too.", "Over" }; | |
sendGreetings( client, greetings, remote_point ); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="client"></param> | |
/// <param name="terminator_segment"></param> | |
/// <returns></returns> | |
string receiveUntilStop(UdpClient client, string terminator_segment) { | |
StringBuilder result_builder = new StringBuilder(); | |
string segment = string.Empty; | |
Encoding encoding = Encoding.ASCII; | |
IPEndPoint remote_point = new IPEndPoint( IPAddress.Any, 50 ); | |
while ( !segment.StartsWith( terminator_segment ) ) { | |
byte[] received_data = client.Receive( ref remote_point ); | |
segment = encoding.GetString( received_data ); | |
result_builder.AppendLine( segment ); | |
} | |
return result_builder.ToString(); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="client"></param> | |
/// <param name="greetings"></param> | |
/// <returns></returns> | |
bool sendGreetings(UdpClient client, string[] greetings, IPEndPoint remote_point) { | |
int count = 0; | |
byte[] send_bytes = null; | |
foreach ( string greeting in greetings ) { | |
Console.WriteLine( " send to: \"{0}\"", greeting ); | |
send_bytes = getByteArray( greeting ); | |
client.Send( send_bytes, send_bytes.Length, remote_point ); | |
if ( count <= greetings.Length ) | |
Thread.Sleep( 1000 ); | |
++count; | |
} | |
return true; | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="s"></param> | |
/// <returns></returns> | |
byte[] getByteArray(string s) { | |
return Encoding.UTF8.GetBytes( s ); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
static void Main() { | |
MulticastingSample progn = new MulticastingSample(); | |
progn.run(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment