![](https://images2018.cnblogs.com/blog/1260244/201806/1260244-20180622155703569-1593707712.png)
![](https://images2018.cnblogs.com/blog/1260244/201806/1260244-20180622155555668-812865567.png)
![](https://images2018.cnblogs.com/blog/1260244/201806/1260244-20180622155547802-585767908.jpg)
using System.Collections.Generic;
using System.Linq;
using
;
using
.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace _023_socket编程_UDP协议_服务器端
{
class Program
{
private static Socket udpServer;
static void Main(string[] args)
{
//1,创建socket
udpServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//2,绑定ip跟端口号
udpServer.Bind(new IPEndPoint(IPAddress.Parse("
"), 8888));
//3,接收数据
new Thread(ReceiveMessage) { IsBackground = true }.Start();
//udpServer.Close();
Console.ReadKey();
}
static void ReceiveMessage()
{
while (true)
{
EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
byte[] data = new byte[1024];
int length = udpServer.ReceiveFrom(data, ref remoteEndPoint);//这个方法会把数据的来源(ip:port)放到第二个参数上
string message = Encoding.UTF8.GetString(data, 0, length);
Console.WriteLine("从ip:" + (remoteEndPoint as IPEndPoint).Address.ToString() + ":" + (remoteEndPoint as IPEndPoint).Port + "收到了数据:" + message);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using
;
using
.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace _025_udpclient
{
class Program
{
static void Main(string[] args)
{
//创建udpclient 绑定ip跟端口号
Socket udpClient = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
while (true)
{
EndPoint serverPoint = new IPEndPoint(IPAddress.Parse("
"),8888);
string message = Console.ReadLine();
byte[] data = Encoding.UTF8.GetBytes(message);
udpClient.SendTo(data,serverPoint);
}
udpClient.Close();
Console.ReadKey();
}
}
}
![](https://images2018.cnblogs.com/blog/1260244/201806/1260244-20180622155657002-695467936.png)