错误接收UDP:连接被拒绝 [英] Error receiving in UDP: Connection refused

查看:612
本文介绍了错误接收UDP:连接被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


  

错误接收UDP:连接被拒绝。


可能是什么这方面的原因?服务器是不是我的控制之下,但我知道它的工作的罚款。

我已经加入了code

  INT sockfdudp;
烧焦bu​​fudp [MAXDATASIZE],口岸[6];
结构addrinfo中的提示,* servinfo,* P;
结构sockaddr_storage地址;
INT RV;
个char [INET6_ADDRSTRLEN]
INT bytes_recv,bytes_sent;
socklen_t的LEN;scanf函数(%S,口);
的printf(UDP端口:%S \\ n,口);//开始连接报服务器
memset的(安培;提示,0,sizeof的提示);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;如果((RV =的getaddrinfo(SERVER_NAME,端口&放大器;提示,&放大器;!servinfo))= 0){
    fprintf中(标准错误的getaddrinfo:%S \\ n,gai_strerror(RV));
    返回1;
}//通过所有的结果循环,使插座
为(P = servinfo; P!= NULL; P = P-> ai_next){
    如果((sockfdudp =插座(对GT; ai_family,P-> ai_socktype,
            P-> ai_protocol))== -1){
        PERROR(创建数据报套接字);
        继续;
    }如果(连接(sockfdudp,P-> ai_addr,P-> ai_addrlen)== -1){
        关闭(sockfdudp);
        PERROR(连接流套接字);
        继续;
    }
    打破;
}如果(P == NULL){
    fprintf中(标准错误,ClientUDP:无法绑定套接字\\ n);
    返回2;
}
freeaddrinfo(servinfo);如果((bytes_sent = SENDTO(sockfdudp,UDP_MSG,strlen的(UDP_MSG),0,p型> ai_addr,P-> ai_addrlen))== -1){
    PERROR(ClientUDP:错误发送数据);
    出口(1);
}
的printf(数据%S发出\\ n,UDP_MSG);LEN = sizeof的(结构sockaddr_storage);如果((bytes_recv = recvfrom的(sockfdudp,bufudp,MAXDATASIZE-1,0,(结构sockaddr *)及地址,和放大器; LEN))== -1){
    PERROR(错误接收UDP);
    出口(1);
}的printf(字节的recv%d个\\ N,bytes_recv);bufudp [bytes_recv] ='\\ 0';的printf(ClientUDP:收到\\ n%S \\ n,bufudp);关闭(sockfdudp);返回0;


解决方案

机会是your're送点东西给谁不特定端口上侦听的服务器。
这将导致发送一个ICMP消息回来了,你的下一个recvfrom的将在你连接插座的情况下返回一个错误。

检查使用tcpdump或者Wireshark的正在发生的事情在电线上。

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;

解决方案

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.

这篇关于错误接收UDP:连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆