[英]C# .Net receiving UDp packets in separater thread and application exit
[英]Receiving unexpected udp packets in my c# client
當我在 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
有人可以讓我知道我哪里出錯了。
提前致謝
如果 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 設備上的端口。
看起來您在端口選擇方面遇到了問題。 您看到的數據包來自 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 的Intense Traffic Generator 測試客戶端,並使用NirSoft TcpUdpWatch確認流量
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.