sendto call on a UDP socket gives ECONNREFUSED

From: hkumar@hss.hns.com
Date: Sun Feb 27 2000 - 06:35:12 EST


Hi,

I am using linux kernel 2.2.14 on an i686 platform. I tried a simple program as
given below

------------------snip----------------------

emudev~/workshop $ cat sendto.c
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <errno.h>

main()
{
 struct sockaddr_in sin;
 int sd, i;

 memset(&sin, 0, sizeof(sin));
 sin.sin_port = htons(4000);
 sin.sin_family = AF_INET;
 sin.sin_addr.s_addr = inet_addr("127.0.0.1");

 sd = socket(AF_INET, SOCK_DGRAM, 0);

 for (i = 0; i < 4; i++)
        if (sendto(sd, NULL, 0, 0, (struct sockaddr *)&sin, sizeof(sin)) == -1)
perror("sendto");
}

emudev~/workshop $ make sendto
cc sendto.c -o sendto
emudev~/workshop $ ./sendto
sendto: Connection refused
sendto: Connection refused
emudev~/workshop $
-----------------------------snip------------------------------

As you can see in the output given above sendto returns failure with error
ECONNREFUSED.
The sento call succeeds if we have a receiving socket on the remote end i.e. at
port 4000.
Since it as a UDP socket sendto must not fail even if the remote end does not
exist also the error its giving seems to be quite irrelevant.
This happens only if the remote endpoint is on the local host.
Another point to note is that sendto succeeds and fails in alternate calls, thus
 in four tries it fails two times and succeeds two times in the example given
above.

Why is this happening?

Thanks and Regards

-harendra

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Tue Feb 29 2000 - 21:00:16 EST