UDP 接收错误:连接被拒绝

发布于 2024-08-23 06:27:28 字数 1913 浏览 6 评论 0原文

我正在尝试通过 UDP 在特定端口将字符串 HI 发送到服务器,然后接收响应。然而,在我尝试使用 recvfrom() 获取响应后,我陷入了阻塞状态。我尝试使用连接的 UDP 但我得到:

UDP 接收错误:连接被拒绝

这可能是什么原因?服务器不在我的控制之下,但我知道它工作正常。

我已经添加了代码

int sockfdudp;
char bufudp[MAXDATASIZE], port[6];
struct addrinfo hints, *servinfo, *p;
struct sockaddr_storage addr;   
int rv;
char s[INET6_ADDRSTRLEN];
int bytes_recv, bytes_sent;
socklen_t len;

scanf("%s",port);
printf("UDP Port: %s \n", port);

// Start connecting to datagram server  
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;

if ((rv = getaddrinfo(SERVER_NAME, port, &hints, &servinfo)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    return 1;
}

// loop through all the results and make a socket
for(p = servinfo; p != NULL; p = p->ai_next) {
    if ((sockfdudp = socket(p->ai_family, p->ai_socktype,
            p->ai_protocol)) == -1) {
        perror("Creating datagram socket");
        continue;
    }

if (connect(sockfdudp, p->ai_addr, p->ai_addrlen) == -1) {
        close(sockfdudp);
        perror("Connecting stream socket");
        continue;
    }
    break;
}

if (p == NULL) {
    fprintf(stderr, "ClientUDP: failed to bind socket\n");
    return 2;
}


freeaddrinfo(servinfo);

if ((bytes_sent = sendto(sockfdudp, UDP_MSG, strlen(UDP_MSG), 0, p->ai_addr, p->ai_addrlen)) == -1) {
    perror("ClientUDP: Error sending data");
    exit(1);
}
printf("Data %s sent\n", UDP_MSG );     

len = sizeof(struct sockaddr_storage);

if ((bytes_recv = recvfrom(sockfdudp, 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 );    

close(sockfdudp);

return 0;

I am trying to send a string HI to a server over UDP in a particular port and then to receive a response. However, after I try to get the response using recvfrom() I was stuck in blocking state. I tried using connected UDP but I got:

Error receiving in UDP: Connection refused

What could be the reasons for this? The server is not under my control, but I do know its working fine.

I have added the code

int sockfdudp;
char bufudp[MAXDATASIZE], port[6];
struct addrinfo hints, *servinfo, *p;
struct sockaddr_storage addr;   
int rv;
char s[INET6_ADDRSTRLEN];
int bytes_recv, bytes_sent;
socklen_t len;

scanf("%s",port);
printf("UDP Port: %s \n", port);

// Start connecting to datagram server  
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;

if ((rv = getaddrinfo(SERVER_NAME, port, &hints, &servinfo)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    return 1;
}

// loop through all the results and make a socket
for(p = servinfo; p != NULL; p = p->ai_next) {
    if ((sockfdudp = socket(p->ai_family, p->ai_socktype,
            p->ai_protocol)) == -1) {
        perror("Creating datagram socket");
        continue;
    }

if (connect(sockfdudp, p->ai_addr, p->ai_addrlen) == -1) {
        close(sockfdudp);
        perror("Connecting stream socket");
        continue;
    }
    break;
}

if (p == NULL) {
    fprintf(stderr, "ClientUDP: failed to bind socket\n");
    return 2;
}


freeaddrinfo(servinfo);

if ((bytes_sent = sendto(sockfdudp, UDP_MSG, strlen(UDP_MSG), 0, p->ai_addr, p->ai_addrlen)) == -1) {
    perror("ClientUDP: Error sending data");
    exit(1);
}
printf("Data %s sent\n", UDP_MSG );     

len = sizeof(struct sockaddr_storage);

if ((bytes_recv = recvfrom(sockfdudp, 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 );    

close(sockfdudp);

return 0;

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

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

发布评论

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

评论(2

无名指的心愿 2024-08-30 06:27:28

您可能正在向不侦听该特定端口的服务器发送某些内容。
这将导致发送回 icmp 消息,并且在连接套接字的情况下,您的下一个 recvfrom 将返回错误。

使用 tcpdump 或wireshark 检查线路上发生的情况。

Chances are your're sending something to a server who does not listen on that particular port.
That would cause an icmp message to be sent back , and your next recvfrom will return an error in the case where you connect the socket.

Check with tcpdump or wireshark what's going on on the wire.

温折酒 2024-08-30 06:27:28

我的猜测是您的 IP 地址不知何故有问题,或者端口已在使用中。 UDP 是无连接的,因此实际上不存在任何会失败的“连接”。

My guess would be that your ip address is bad somehow, or the port is already in use somehow. UDP is connectionless, so there really isn't any "connection" to fail.

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