Error receiving in UDP: Connection refused
Asked Answered
D

2

18

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;
Dewey answered 3/3, 2010 at 15:12 Comment(2)
Since recvfrom will block until a message is available at the socket, are you sure the server is responding to your message?Offprint
Server Fault has a canonical question about Connection Refused.Ossie
P
38

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.

Piteous answered 3/3, 2010 at 17:50 Comment(3)
This is the right answer - the "Connection Refused" from recvfrom quite likely is caused by the server returning an ICMP "Port Unreachable" response to the initial UDP request.Legislative
My wireshark absolutely agree :-)Afrikah
Chances are your're sending something to a server who does not listen on that particular port...let me add: or IP address. Ex: if you bind a UDP server to a specific IP address such as 192.168.0.10 instead of to INADDR_ANY, then it will listen ONLY on that particular IP address. Even if you try to send this listening server something on the same computer by sending to localhost or 127.0.0.1, it will fail to connect, since it will be listening ONLY on IP 192.168.0.10 instead of on INADDR_ANY, which includes any IP address, including localhost (127.0.0.1).Hage
R
-1

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.

Restrained answered 3/3, 2010 at 15:49 Comment(2)
could there be any reason for the UDP_MSG that i send, which is initialized as constant #define UDP_MSG "HI" be wrong? it needs to be correctly send to get back any response from server.Dewey
Well...there is a minimum message size in UDP itself, but I would imagine the Sockets layer takes care of that.Restrained

© 2022 - 2024 — McMap. All rights reserved.