首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在c#客户端中收到意外的udp数据包

在c#客户端中收到意外的udp数据包
EN

Stack Overflow用户
提问于 2017-06-22 18:00:19
回答 2查看 188关注 0票数 1

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

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

客户端(c#):

代码语言:javascript
复制
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):

代码语言:javascript
复制
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中收到以下信息:

代码语言:javascript
复制
This is the message you received: ?{        EJFDEBFEEBFACACACACACACACACACAAA    

谁能让我知道我哪里做错了。

提前感谢

EN

回答 2

Stack Overflow用户

发布于 2017-06-22 18:25:11

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

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

票数 0
EN

Stack Overflow用户

发布于 2020-05-27 18:40:48

看起来您在端口选择方面遇到了问题。您看到的数据包来自Windows。您不需要bind语句。这是我在.net核心中使用的一个工作示例

代码语言:javascript
复制
 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高强度流量生成器测试客户端,并使用NirSoft TcpUdpWatch确认流量

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44696153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档