从服务器接收 UDP 消息时出现问题

发布于 2024-08-23 08:19:37 字数 762 浏览 8 评论 0原文

我必须通过 UDP 连接到远程服务器并向其发送预定消息才能获取消息。我首先在 TCP 中尝试过,它有效,但在 UDP 中,在我发送消息并监听 receivefrom() 中的回复后,我什么也没得到。谁能告诉我这里可能有什么问题。

if ((bytes_sent = sendto(sockfd, UDP_MSG, strlen(UDP_MSG), 0, p->ai_addr, p->ai_addrlen)) == -1) {
            perror("ClientUDP: Error sending data");
            exit(1);
        }

printf("Data sent %s\n", UDP_MSG);  

len = sizeof(struct sockaddr_storage);

if ((bytes_recv = recvfrom(sockfd, bufudp, MAXDATASIZE-1, 0,(struct sockaddr*)&addr, &len)) == -1) {
            perror("Error receiving in UDP");
            exit(1);
        }



printf("Bytes recv %d\n", bytes_recv);  

bufudp[bytes_recv] = '\0';
printf("ClientUDP: Received\n %s \n",bufudp );   

使用 scanf() 从键盘输入端口,消息是字符串“HI”。

I have to connect to a remote server via UDP and send a predetermined message to it in order to get a message back. I had first tried this in TCP and it worked but in UDP after I send the message and listen for the reply in recvfrom() I get nothing. Can anyone tell me what might be the problem here.

if ((bytes_sent = sendto(sockfd, UDP_MSG, strlen(UDP_MSG), 0, p->ai_addr, p->ai_addrlen)) == -1) {
            perror("ClientUDP: Error sending data");
            exit(1);
        }

printf("Data sent %s\n", UDP_MSG);  

len = sizeof(struct sockaddr_storage);

if ((bytes_recv = recvfrom(sockfd, bufudp, MAXDATASIZE-1, 0,(struct sockaddr*)&addr, &len)) == -1) {
            perror("Error receiving in UDP");
            exit(1);
        }



printf("Bytes recv %d\n", bytes_recv);  

bufudp[bytes_recv] = '\0';
printf("ClientUDP: Received\n %s \n",bufudp );   

The port is entered from the keyboard using scanf() and the message is the string "HI".

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

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

发布评论

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

评论(2

山有枢 2024-08-30 08:19:37

TCP 和 UDP 不可互换。某些特定协议同时使用这两种协议(例如 DNS),但大多数协议都会选择其中之一。 HTTP(S) 建立在 TCP 之上,因为它需要双向流。许多音频/视频协议都是建立在 UDP 之上的,因为丢失/重新排序的数据包(偶尔)并不重要。

简而言之,您的服务器可能只侦听 TCP 端口,而不侦听 UDP。

如果您知道服务器正在侦听 TCP 和 UDP,请检查两台计算机上以及沿途的防火墙。

TCP and UDP are not interchangeable. Some specific protocols use both (DNS for example), but bulk of them pick one or the other. HTTP(S) is built on top of TCP since is requires a bi-direction stream. Many audio/video protocols are built on top of UDP since missed/re-ordered packets (once in a while) are not critical.

In short, your server is probably only listening on TCP port, and not on UDP.

If you know that server is listening on both TCP and UDP, check the firewalls on both machines and along the route.

你的背包 2024-08-30 08:19:37

如果您想查看 UDP 数据包是否以您期望的正确内容和地址离开您的计算机,我会安装 Wireshark。该实用程序将捕获并解码您的计算机发送/接收的所有 IP 数据包。

一个问题,recvfrom() 是立即返回还是只是阻塞?如果它立即返回,我会检查返回值是否为-1,如果是,则表明 errno 中存在错误代码。

If you want to see whether the UDP packet is leaving your machine with the correct contents and addresses you expect I'd install Wireshark. This utility will capture and decode all IP packets sent/received by your computer.

One question, does recvfrom() return immediately or does it just block? If it returns immediately I'd check to see if the return value is -1, if so that indicates an error code is present in errno.

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