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

时间:2017-06-22 10:00:19

标签: c# python macos sockets

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

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

客户端(C#):

logRequestDetails: function(req, res, next) {
  req.on("end", function() {
    sails.log.verbose('Request', req.options.controller, req.options.action);
  });
},

服务器(蟒-3.6):

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();
    }

预期的消息(我在mac中收到以下内容):

这是您收到的消息:h01rx360ry151rz009e007

我在windows中收到以下内容:

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)

有人可以告诉我哪里出错了。

提前致谢

2 个答案:

答案 0 :(得分:0)

如果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设备上的端口。

答案 1 :(得分:0)

好像您在选择端口时遇到问题。您看到的数据包来自Windows。 您不需要bind语句。这是我在.net core中使用的工作示例

 internal class Program
    {
        private const int PORT = 5678;

        private static void Main(string[] args)
        {
            Console.WriteLine("Packet Forwarding UP!");
            var client = new UdpClient(PORT);
            // can use IPAddress.Any or other IP depending on where they are coming from.
            var ipEndPoint = new IPEndPoint(IPAddress.Loopback, PORT); 
            while (true)
            {
                Console.Write("Waiting... ");
                var receive = client.Receive(ref ipEndPoint);
                Console.WriteLine(" - Received Data - " + receive.Length);                
            }
        }
    }

我一直在使用PacketSender's Intense Traffic Generator测试客户端,并使用NirSoft TcpUdpWatch确认流量

demonstration