0%

【C#】 UDPClient同地址端口复用方法

做的游戏需要个后端,发现服务器客户同端时不太好调试,所以调研一下UDP的端口复用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var Recv_Port = 9996;
var tmp = new UdpClient()
{
EnableBroadcast = true,
ExclusiveAddressUse = false
};
tmp.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
tmp.Client.Bind(new IPEndPoint(0, Recv_Port));
try
{
while (true)
{
IPEndPoint tmpEp = new IPEndPoint(0, 0);
var tmpData = tmp.Receive(ref tmpEp);
Console.WriteLine(Encoding.Default.GetString(tmpData);
}
}
finally
{
tmp.Close();
}