Receive the Broadcast
Description :
The following socket programming is the example of receive the broadcast
Namespace part
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
Coding part of the receive Broad cast
public class ReceiveBroadcast
{
public static void Main()
{
Socket MySocket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
IPEndPoint MyIPEndPoint = new IPEndPoint(IPAddress.Any, 9050);
MySocket.Bind(MyIPEndPoint);
EndPoint MyEndpoint = (EndPoint)MyIPEndPoint;
Console.WriteLine("Ready to receive dat!");
byte[] MyData = new byte[1024];
int receive = sock.ReceiveFrom(MyData, ref MyEndPoint);
string stringData = Encoding.ASCII.GetString(MyData, 0, receive);
Console.WriteLine("received: {0} from: {1}",
stringData, MyEndPoint.ToString());
MyData = new byte[1024];
receive = Mysocket.ReceiveFrom(MyData, ref MyEndpoint);
stringData = Encoding.ASCII.GetString(MyData, 0, receive);
Console.WriteLine("received: {0} from: {1}",
stringData, MyEndPoint.ToString());
MySocket.Close();
}
}
Code Explanation
1. Create the instance of Socket.
2. Create the instance of IPEndPoint.
3. Bind the IPendpoint with the socket
4. Receive the data from the IP
By
Nathan
