在我的客户端接收意外的udp数据包

2024-09-27 18:18:23 发布

您现在位置:Python中文网/ 问答频道 /正文

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

有人能告诉我哪里出了问题吗。你知道吗

提前谢谢


Tags: the消息messageifsocketthisrxconsole

热门问题