Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. net;
Using system. IO;
Using system. net. Sockets;
Using system. Threading;
Namespace consoleapplication1
{
Class Program
{
Static void main (string [] ARGs)
{
Multicastserver Server = new multicastserver ();
Multicastclient client = new multicastclient ();
Thread clientthread = new thread (New threadstart (client. receivedata ));
Clientthread. Start ();
Thread serverthread = new thread (New threadstart (server. senddata ));
Serverthread. Start ();
}
}
Class multicastserver {
Private IPaddress multicastip = IPaddress. parse ("224.110.10.1 ");
Private int Port = 5001;
Public void senddata ()
{
Console. writeline ("sender start ");
Ipendpoint multicastiep = new ipendpoint (multicastip, Port );
Udpclient sendudpclient = new udpclient ();
Sendudpclient. enablebroadcast = true;
String sendstring = "how are you ";
Byte [] bytes = system. Text. encoding. utf8.getbytes (sendstring );
Try
{
Sendudpclient. Send (bytes, bytes. length, multicastiep );
}
Catch
{
Console. writeline ("send error ");
}
Finally
{
Sendudpclient. Close ();
Console. writeline ("sender close ");
}
}
}
Class multicastclient {
Private IPaddress multicastip = IPaddress. parse ("224.110.10.1 ");
Private int Port = 5001;
Public void receivedata ()
{
Console. writeline ("reciever start ");
Udpclient receiveudp = new udpclient (this. Port );
Try
{
Receiveudp. joinmulticastgroup (this. multicastip, 10 );
}
Catch (socketexception E)
{
Console. writeline (E. Message. tostring ());
}
Ipendpoint remotehost = NULL;
While (true)
{
Try
{
Byte [] bytes = receiveudp. Receive (ref remotehost );
String STR = encoding. utf8.getstring (bytes, 0, bytes. Length );
Console. writeline (STR );
}
Catch
{
Console. writeline ("reciever close ");
Break;
}
}
}
}
}