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

问题描述:

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

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

服务器(蟒-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

我收到以下窗口:

This is the message you received: ?{  EJFDEBFEEBFACACACACACACACACACAAA  

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

在此先感谢

+0

在你的python服务器代码中执行一些加密计算后,你会执行'bytes(MESSAGE,“utf-8”)''。但在C#客户端代码中,将这些字节视为ascii - 'Encoding.ASCII.GetString(receiveBytes);'。尝试将其更改为'Encoding.UTF8.GetString(receiveBytes);' – Evk

+0

我做了更改但未能获得预期的输出。:( – renuka

+0

然后确保没有其他应用程序向该端口发送数据。尝试使用另一个端口(不要忘了打开它在防火墙) – Evk

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

+0

谢谢..#我试图使用端口1900,但我收到什么,如果我使用任何港口以外的137和138 – renuka

+0

我知道,这是微不足道的,但你确定防火墙不会阻止你的应用程序或端口(137,138除外)? –

+0

即使禁用防火墙,我也没有收到任何东西。这次我也打开了一个新的端口并分配给它( – renuka