gpt4 book ai didi

c# - 在我的C#客户端中接收意外的udp数据包

转载 作者:行者123 更新时间:2023-12-03 12:09:22 25 4
gpt4 key购买 nike

当我在Windows(同一系统中的客户端和服务器)中运行代码时,收到意外的udp数据包。我的客户端是用C#编写的,服务器是python的。

当我在Mac中以相同的代码运行时,我没有任何问题,并且收到了预期的消息(这里我在Mac中为udp打开了一个端口)。

客户端(C#):

public static void Main(string[] args)
{
Console.WriteLine("Receiver");
// This constructor arbitrarily assigns the local port number.
UdpClient udpClient = new UdpClient();
//udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, 137));
try
{
//IPEndPoint object will allow us to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 137);
string message ;

do
{
// Blocks until a message returns on this socket from a remote host.
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
message = Encoding.ASCII.GetString(receiveBytes);

// Uses the IPEndPoint object to determine which of these two hosts responded.
Console.WriteLine("This is the message you received: " +
message);
//Console.WriteLine("This message was sent from " +
// RemoteIpEndPoint.Address.ToString() +
// " on their port number " +
// RemoteIpEndPoint.Port.ToString());
}
while (message != "exit");
udpClient.Close();
//udpClientB.Close();

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}

Console.WriteLine("Press Any Key to Continue");
Console.ReadKey();
}

服务器(python-3.6):
import socket
from time import sleep

rx=0 #000
ry=0 #000
rz=0 #000
e=0 #000

UDP_IP = "172.20.10.4"
UDP_PORT = 137
MESSAGE = ""


sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
while(1):
if (rx<360):
rx=rx+1
if ((ry<360) & (rx>=360)):
ry=ry+1
if ((rx>=360) & (ry>=360)):
rx=0
ry=0
if (rz<360):
rz=rz+1
if (rz>=360):
rz = 0
if (e<10):
e=e+1
if(e>=10):
e=0
#verify rx
if (rx<10):
rxs='00'+str(rx)
if ((rx>=10) & (rx<100)):
rxs='0'+str(rx)
if (rx>=100):
rxs=str(rx)
#verify ry
if (ry<10):
rys='00'+str(ry)
if ((ry>=10) & (ry<100)):
rys='0'+str(ry)
if (ry>=100):
rys=str(ry)
#verify rz
if (rz<10):
rzs='00'+str(rz)
if ((rz>=10) & (rx<100)):
rzs='0'+str(rz)
if (rz>=100):
rzs=str(rz)
#verify e
if (e<10):
es='00'+str(e)
if ((e>=10) & (e<100)):
es='0'+str(e)
if (e>=100):
es=str(e)
MESSAGE = 'h'+'01'+'rx'+rxs+'ry'+rys+'rz'+rzs+'e'+es
#sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
sock.sendto(bytes(MESSAGE, "utf-8"), (UDP_IP, UDP_PORT))
sleep(0.1)

预期的消息(我在Mac中收到以下消息):

这是您收到的消息:h01rx360ry151rz009e007

我在Windows中收到以下信息:
This is the message you received: ?{        EJFDEBFEEBFACACACACACACACACACAAA    

有人可以让我知道我哪里出错了。

提前致谢

最佳答案

如果udp数据包未与您的服务器关联,则可能会有所帮助:https://forum.fortinet.com/tm.aspx?m=106656和此处:https://superuser.com/questions/637696/what-is-netbios-does-windows-need-its-ports-137-and-138-open

Windows将端口137用于其自身的服务。尝试更改Windows设备上的端口。

关于c# - 在我的C#客户端中接收意外的udp数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44696153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com