Created
December 17, 2010 00:14
-
-
Save noqisofon/744257 to your computer and use it in GitHub Desktop.
"How are you?" で始める UDP マルチキャスト通信。マルチキャスト要らないけど…
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.Collections.Generic; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Text; | |
using System.Threading; | |
namespace demo.multicasting.client { | |
/// <summary> | |
/// | |
/// </summary> | |
class MulticastGuestSample { | |
/// <summary> | |
/// | |
/// </summary> | |
static string[] CONDITIONS = new string[] { "Great!", "Not bad.", "Pretty Good!", "Good!" }; | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="args"></param> | |
public void run(string[] args) { | |
int local_port = 1000; | |
int remote_port = 2000; | |
UdpClient client = new UdpClient( local_port, AddressFamily.InterNetwork ); | |
IPAddress group_address = IPAddress.Parse( "239.0.0.0" ); | |
IPEndPoint remote_point = new IPEndPoint( group_address, remote_port ); | |
client.JoinMulticastGroup( group_address ); | |
Encoding encoding = Encoding.UTF8; | |
int times = 0; | |
string greeting = string.Empty; | |
string segment = string.Empty; | |
byte[] send_bytes = null; | |
while ( true ) { | |
IPEndPoint sent_point = new IPEndPoint( IPAddress.Any, 50 ); | |
byte[] received_bytes = client.Receive( ref sent_point ); | |
segment = encoding.GetString( received_bytes ); | |
Console.WriteLine( "alice > {0}", segment ); | |
if ( segment.StartsWith( "bye" ) ) | |
break; | |
Thread.Sleep( 1000 ); | |
if ( segment.EndsWith( "How are you?" ) ) { | |
Random r = new Random(); | |
int index = r.Next( CONDITIONS.Length ); | |
greeting = string.Format( "{0} Thanks. How are you?", CONDITIONS[index] ); | |
} else { | |
greeting = "over."; | |
} | |
Console.WriteLine( "bob < {0}", greeting ); | |
send_bytes = encoding.GetBytes( greeting ); | |
client.Send( send_bytes, send_bytes.Length, remote_point ); | |
++times; | |
} | |
client.DropMulticastGroup( group_address ); | |
Console.ReadLine(); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="args"></param> | |
static void Main(string[] args) { | |
MulticastGuestSample progn = new MulticastGuestSample(); | |
progn.run( args ); | |
} | |
} | |
} |
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.IO; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Text; | |
using System.Threading; | |
namespace demo.multicasting.server { | |
/// <summary> | |
/// | |
/// </summary> | |
class MulticastHostSample { | |
/// <summary> | |
/// | |
/// </summary> | |
static string[] CONDITIONS = new string[] { "Pretty Good!", "Great!", "Not bad.", "Good!" }; | |
/// <summary> | |
/// | |
/// </summary> | |
void run() { | |
int local_port = 2000; | |
int remote_port = 1000; | |
UdpClient client = new UdpClient( local_port, AddressFamily.InterNetwork ); | |
IPAddress group_address = IPAddress.Parse( "239.0.0.0" ); | |
IPEndPoint remote_point = new IPEndPoint( group_address, remote_port ); | |
// レシーバをマルチキャストグループに追加します。 | |
client.JoinMulticastGroup( group_address, 50 ); | |
Thread.Sleep( 2000 ); | |
Encoding encoding = Encoding.UTF8; | |
int times = 0; | |
string greeting = "Hi, How are you?"; | |
string segment = string.Empty; | |
byte[] send_bytes = null; | |
while ( true ) { | |
Console.WriteLine( "alice < {0}", greeting ); | |
send_bytes = encoding.GetBytes( greeting ); | |
client.Send( send_bytes, send_bytes.Length, remote_point ); | |
if ( greeting.StartsWith( "bye" ) ) | |
break; | |
Thread.Sleep( 1000 ); | |
IPEndPoint sent_point = new IPEndPoint( IPAddress.Any, 50 ); | |
byte[] received_bytes = client.Receive( ref sent_point ); | |
segment = encoding.GetString( received_bytes ); | |
Console.WriteLine( "bob > {0}", segment ); | |
if ( segment.EndsWith( "How are you?" ) ) { | |
Random r = new Random(); | |
greeting = CONDITIONS[r.Next( CONDITIONS.Length )]; | |
} else if ( segment.StartsWith( "over" ) ) { | |
greeting = "bye."; | |
} else { | |
greeting = "bye."; | |
} | |
++times; | |
} | |
// レシーバをマルチキャストグループから脱退します。 | |
client.DropMulticastGroup( group_address ); | |
Console.ReadLine(); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
public static void Main() { | |
MulticastHostSample progn = new MulticastHostSample(); | |
progn.run(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment