UDP数据从未发送过?

发布于 2024-10-19 05:52:48 字数 953 浏览 1 评论 0原文

我正在尝试使用 UDP 发送 HTTP GET 请求(因为来自侦听服务器的回复无关紧要,我不想阻止该程序)

这是代码:

System.Net.Sockets.UdpClient client = new System.Net.Sockets.UdpClient();

client.Connect("www.domainname.com", 80);
string request_header = "GET /ping.php HTTP/1.1\r\nHost: www.domainname.com\r\n\r\n";

byte[] stre = System.Text.Encoding.ASCII.GetBytes(request_header);
client.Send(stre, stre.Length);
System.Net.IPEndPoint RemoteIpEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 0);
byte[] receiveBytes = client.Receive(ref RemoteIpEndPoint);
string returnData = System.Text.Encoding.ASCII.GetString(receiveBytes);
client.Close(); 

首先,该请求似乎没有在服务器,所以我想发送时可能出了问题? 其次,程序挂在 client.Receive(ref RemoteIpEndPoint) 上,并在那里等待。似乎没有收到任何数据。

我尝试过改变

System.Net.IPEndPoint(System.Net.IPAddress.Any, 0);

……

System.Net.IPEndPoint(System.Net.IPAddress.Any, 80);

,但没有成功。

I'm trying to send a HTTP GET request with UDP (since the reply from the listening server is irrelevant and I don't want to block the program)

This is the code:

System.Net.Sockets.UdpClient client = new System.Net.Sockets.UdpClient();

client.Connect("www.domainname.com", 80);
string request_header = "GET /ping.php HTTP/1.1\r\nHost: www.domainname.com\r\n\r\n";

byte[] stre = System.Text.Encoding.ASCII.GetBytes(request_header);
client.Send(stre, stre.Length);
System.Net.IPEndPoint RemoteIpEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 0);
byte[] receiveBytes = client.Receive(ref RemoteIpEndPoint);
string returnData = System.Text.Encoding.ASCII.GetString(receiveBytes);
client.Close(); 

First, the request doesn't seem to be receieved at the server, so I'm thinking perhaps something goes wrong when sending it?
Second, the program hangs on client.Receive(ref RemoteIpEndPoint), and just waits there. No data seems to be received.

I have tried to change...

System.Net.IPEndPoint(System.Net.IPAddress.Any, 0);

to...

System.Net.IPEndPoint(System.Net.IPAddress.Any, 80);

...but with no luck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

输什么也不输骨气 2024-10-26 05:52:48

如果您不想阻止客户端,则使用异步套接字方法,但使用 TCP。我怀疑您是否会找到一个侦听 UDP 上的 HTTP 请求的 Web 服务器。

您可能还想看看 WireShark,它是一个网络流量记录工具;您可以使用它来查看您的 UDP 数据报很可能正在发送,但服务器没有生成响应。

您还可以在服务器上使用 netstat 查看它是否没有侦听 UDP 端口 80。

If you don't want to block the client then use the async socket methods, but use TCP. I doubt you'll find a web server that listens on UDP for HTTP requests.

You might also want to look at WireShark which is a network traffic logging tool; you could have used this to see that your UDP datagram most probably WAS being sent but there was no response was generated by the server.

You could also use netstat on the server to see that it isn't listening on UDP port 80.

过去的过去 2024-10-26 05:52:48

当服务器侦听 TCP 端口 80 时,它永远不会受到 UDP 帧的影响。

TCP 和 UDP 是不同的协议。两者都支持“端口号”,但这些并不相关。

您可以通过读取 UDP 套接字上的响应来验证这一点。您应该得到一个错误结果。相应的错误代码表明问题所在,(通常)没有人在侦听 UDP 端口 80。

When a server listens at TCP port 80 it will never be affected by a UDP frame.

TCP and UDP are different protocols. Both support "port numbers", but these are not related.

You can verify this by reading a response on the UDP socket. You should get an error result. The corresponding error code indicate the problem, hat (usually) nobody is listening at UDP port 80.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文