Can anybody help and explain why?
I wrote two programs just to test broadcasting in C#, which both run on the
same machine. However, the receiver program can't receive any messages from
the sender program. Key parts of the source codes are listed below:
/****** Sender.cs *****/
int remotePort = 8888;
// Init the socket
try {
Socket sendSocket = new Socket( AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp );
sendSocket.SetSocketOption( SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1);
IPEndPoint remoteEP = new IPEndPoint( IPAddress.Broadcast, remotePort );
}
catch (Exception e ) {
Console.WriteLine( e.ToString() );
}
while ( true ) {
sendSocket.SendTo( data, remoteEP );
Thread.CurrentThread.Join( new TimeSpan( 0, 0, 0, 5, 0 ) );
}
/****** Listener.cs *****/
byte[] data = new byte[128];
localPort = 8888;
try{
IPEndPoint remoteEP = new IPEndPoint( new IPAddress( 0 ), 0 );
UdpClient udpListener = new UdpClient( localPort );
}
catch ( Exception e ) {
Console.WriteLine( e.ToString() );
}
while ( true ) {
data = udpListener.Receive( ref remoteEP );
}
Thank you ! LOL~