select refuses connection with datagrams

Mark Hamilton (mark@rti.com)
Thu, 01 Oct 1998 16:04:25 -0700


I have noticed a problem with Linux that has existed for the last couple
of years. It involves basic networking functionality. I have tried for a
long time to figure out what is wrong but I just can not figure out the
problem. Basically, if you send a datagram with the function sendto you
periodically get the error "Connection refused".

Now, our products run on every major UNIX box, Windows, and several
other operating systems without having this problem. So, I have to
believe I'm doing something extraordinarily dumb. Anyway, I have written
a small test program that shows the problem and I have also attached my
makefile. I would appreciate it if you could show me what I'm doing
wrong.

--- linux.c -------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>

int main(int argc, char **argv)
{
int i = 0;
int sent = 0;
int sd = 0;
int sendsize = 5;
int sockAddrSize = sizeof(struct sockaddr_in);
int selectReturn = 0;
struct timeval timeout = {5,0};
struct sockaddr_in sockAddr;
fd_set readmask;
char buffer[10];

memset((char *) &sockAddr, 0, sizeof(struct sockaddr_in));
sd = socket(AF_INET, SOCK_DGRAM, 0);

/*
* Bind to anyt port.
*/
#ifdef THIS_WORKS
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons((unsigned short)7400);
sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);

if(bind(sd,(struct sockaddr *)&sockAddr, sizeof(struct sockaddr_in))
== 0) {
printf("Success: bind passed\n");
} else {
printf("Error: bind failed.\n");
return 0;
}
#endif
sockAddr.sin_port = htons((unsigned short)7400);
sockAddr.sin_family = AF_INET;

#ifndef REMOTE_HOST
sockAddr.sin_addr.s_addr = inet_addr("206.197.67.101");
#else
sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
#endif

sendsize = 5;
memset(buffer, 'a', sendsize);
for (i = 0; i < 10; i++) {
sent = sendto(sd, buffer, sendsize, 0, (struct sockaddr
*)&sockAddr,
sockAddrSize);
if (sent != sendsize) {
printf("Error: wrong size sent errno %d %d.\n", sent);
perror("Error");
} else {
printf("Success: correct size sent\n");
}

FD_ZERO(&readmask);
FD_SET(sd, &readmask);
printf("Select before\n");
selectReturn = select(sd+1, &readmask, NULL, NULL, &timeout);
printf("Select after %d\n", selectReturn);
}
}

--------------- makefile ---------------------------
linux:
/usr/bin/egcs -B/usr/lib/gcc-lib/ -c linux.cxx -o
objs/i86Linux5.1/linux.o
/usr/bin/egcs -B/usr/lib/gcc-lib/ -o objs/i86Linux5.1/linux
objs/i86Linux5.1/linux.o

-- 
 Mark Hamilton            Real-Time Innovations  http://www.rti.com
 mark@rti.com             408-720-8312
 Creativity is allowing yourself to make mistakes. Art is knowing 
 which ones to keep.

- 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/