2017-06-22 158 views
0

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

回答

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設備上的端口。

+0

謝謝..#我試圖使用端口1900,但我收到什麼,如果我使用任何港口以外的137和138 – renuka

+0

我知道,這是微不足道的,但你確定防火牆不會阻止你的應用程序或端口(137,138除外)? –

+0

即使禁用防火牆,我也沒有收到任何東西。這次我也打開了一個新的端口並分配給它( – renuka

相關問題