Broadcst Sample : Broadcast « Network « C# / C Sharp






Broadcst Sample


using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class Broadcst
{
   public static void Main()
   {
      Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
      IPEndPoint iep1 = new IPEndPoint(IPAddress.Broadcast, 9050);
      IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("192.168.1.255"), 9050);

      string hostname = Dns.GetHostName();
      byte[] data = Encoding.ASCII.GetBytes(hostname);

      sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
      sock.SendTo(data, iep1);
      sock.SendTo(data, iep2);
      sock.Close();
   }
}

           
       








Related examples in the same category

1.Receive Broadcast
2.Bad Broadcast