using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
class BroadcastSenderDemo
{
public static void Main()
{
Console.WriteLine("请设置发送的线程数:");
string myEnter = Console.ReadLine();
int num = int.Parse(myEnter);
for (int i = 0; i < num; i++)
{
Thread t = new Thread(new ThreadStart(broadcast));
t.Start();
}
Console.ReadKey();
}
public static void broadcast()
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
IPEndPoint ipep = new IPEndPoint(IPAddress.Broadcast, 15);
string hostname = Dns.GetHostName();
byte[] data = Encoding.ASCII.GetBytes(hostname);
socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1);
socket.SendTo(data, ipep);
Console.WriteLine("已发送");
socket.Close();
}