connect() sometimes succeeds when it shouldn't
Subsystems (2)
Hi
I've found that if I run a program that does lots of calls to connect() then it will eventually succeed when there's no server listening. For SCTP it takes a few thousand, for TCP up to a couple of million. I first came across the problem in Windows, where the connection was being processed so quickly that by the time the handler function had finished, the 'connecting' flag had already been reset, which wasn't expected. I've not checked the Linux sources to see if something similar is happening though.
The machine I tested it on was running a 2.6.32 i686 kernel.
The test code can be found at
http://www.cran.org.uk/~brucec/linux/badconnect.c
Signature · 1 line
Bruce Cran
Quoted 8 lines · Bruce Cran
I've found that if I run a program that does lots of calls to
connect()then it will eventually succeed when there's no server listening. For SCTP it takes a few thousand, for TCP up to a couple of million. I first came across the problem in Windows, where the connection was being processed so quickly that by the time the handler function had finished, the 'connecting' flag had already been reset, which wasn't expected. I've not checked the Linux sources to see if something similar is happening though.
Quoted 4 lines
The machine I tested it on was running a 2.6.32 i686 kernel.
The test code can be found at
http://www.cran.org.uk/~brucec/linux/badconnect.c
Perhaps you just managed to dial your own number? ...Please try with my improved version of badconnect.c below. :-)
Signature · 44 lines
i.
--
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <netinet/in.h>
#include <errno.h>
int main(void)
{
struct sockaddr_in sa;
int fd;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd <= 0)
err(1, "socket");
memset(&sa, 0, sizeof(sa));
sa.sin_addr.s_addr = INADDR_ANY;
sa.sin_family = AF_INET;
sa.sin_port = htons(60234);
if (bind(fd, (struct sockaddr*)&sa, sizeof(sa))) {
perror("bind");
exit(1);
}
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(60234);
if (inet_aton("127.0.0.1", &(sa.sin_addr)) != 1)
exit(1);
if (connect(fd, (struct sockaddr*)&sa, sizeof(sa)) == 0) {
printf("bad connect on iter %d\n", 1);
exit(1);
}
close(fd);
return 0;
}Quoted 10 lines · On Thursday 18 February 2010 23:09:09 Ilpo Järvinen wrote:
Quoted 9 lines · Bruce Cran
I've found that if I run a program that does lots of calls to
connect()then it will eventually succeed when there's no server listening. For SCTP it takes a few thousand, for TCP up to a couple of million. I first came across the problem in Windows, where the connection was being processed so quickly that by the time the handler function had finished, the 'connecting' flag had already been reset, which wasn't expected. I've not checked the Linux sources to see if something similar is happening though.
Quoted 7 lines
Quoted 4 lines
The machine I tested it on was running a 2.6.32 i686 kernel.
The test code can be found at
http://www.cran.org.uk/~brucec/linux/badconnect.cPerhaps you just managed to dial your own number? ...Please try with my improved version of
badconnect.cbelow. :-)
That was the problem: calling bind() before connect() fixed it. Sorry for the noise.
Signature · 1 line
Bruce Cran