首页 > 其他 > 详细

UDP通信

时间:2017-09-25 13:55:28      阅读:159      评论:0      收藏:0      [点我收藏+]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep1 = new IPEndPoint(IPAddress.Broadcast, 8093);
IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("192.168.31.255"), 9001);
string hostname = Dns.GetHostName();
byte[] data = Encoding.ASCII.GetBytes(hostname);

sock.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1);
Thread thread1 = new Thread(send);
Thread thread2 = new Thread(send);
Thread thread3 = new Thread(send);
Thread thread4 = new Thread(send);
Thread thread5 = new Thread(send);
Thread thread6 = new Thread(send);
thread1.Start();
thread2.Start();
thread3.Start();
thread4.Start();
thread5.Start();
thread6.Start();

void send() {
while (true)
{
sock.SendTo(data, iep1);
sock.SendTo(data, iep2);
}

}


}
}
}

UDP通信

原文:http://www.cnblogs.com/zhuzhengchao2017/p/7591458.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!