简体   繁体   English

c#udpClient发送的数据包不存在路由器

[英]c# udpClient sent packets don't exist the router

using the following code: 使用以下代码:

UdpClient Sender = new UdpClient();

        try
        {
            Sender.Send(Encoding.ASCII.GetBytes(msgOut), msgOut.Length, ip);
        }

i am trying to send back a reply to the previous request to the same ip/port. 我试图将对先前请求的答复发送回相同的IP /端口。 while working on the same computer, it would only work if i customize the values to localhost, as it would not even work when i state my own computer IP in the NAT. 在同一台计算机上工作时,仅当我将值自定义为localhost时,该方法才有效,因为当我在NAT中声明自己的计算机IP时,它甚至无法工作。

2 facts about it: 2个事实:

1) Same ip/port values work perfectly fine on my c++ version of it. 1)相同的ip / port值在我的c ++版本上可以正常工作。 (even across the globe) with the next code: (甚至在全球范围内),并附上以下代码:

sockaddr_in si_custom;
int s_custom;
//create socket
if ((s_custom = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR)
{
    //socket fail
    return false;
}

//setup address structure
memset((char *)&si_custom, 0, sizeof(si_custom));
si_custom.sin_family = AF_INET;
si_custom.sin_port = htons(stoi(port));
si_custom.sin_addr.S_un.S_addr = inet_addr(ip.c_str());
//send the message
if (sendto(s, msg.c_str(), msg.size(), 0, (struct sockaddr *) &si_custom, slen) == SOCKET_ERROR)
{
    closesocket(s_custom);
    return false;
}
closesocket(s_custom);
return true;

2) after enabling close to anything remotely relative in the firewall, my wireshark packet sniffer tells me that my packets DO leave my computer perfectly normal. 2)在启用接近防火墙中任何远程相对对象之后,我的wireshark数据包嗅探器告诉我,我的数据包确实使计算机完全正常。

I'm trying to figure out, why would ac# udpClient class behave differently than my c++ Udp code in the sense that my router refuses to pass the first one and agrees to pass the latter one. 我试图弄清楚,为什么ac#udpClient类的行为与我的c ++ Udp代码不同,因为我的路由器拒绝通过第一个,并同意通过后一个。

Thanks in advance. 提前致谢。

用我自己的Ip:port初始化UdpClient Sender对象解决了问题

UdpClient Sender = new UdpClient( new IPEndPoint(MyIP,MyPort));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM