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

标签 c# python macos sockets

当我在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/

相关文章:

macos - 无法在 Linux 和 macOS 上安装 EB CLI(错误 : The Python ssl extension was not compiled. 缺少 OpenSSL 库?))

c# - 如何获取通用列表中的比较项目

c# - 使用 uiAccess=True 安装桌面 WPF 应用程序时的注意事项

python - 将 South Migrations 添加到第 3 方 Django 应用程序

python - pygame 中的碰撞检测无法正常工作

python - 如何调试文件上传?

c# - C#中集合数据类型的比较

c# - 将 JSON 数组反序列化为字符串数组

macos - 如何将剪贴板上的文本行作为 bash 命令执行

cocoa - 在 NSView 中添加边框和圆角矩形