* connect() sometimes succeeds when it shouldn't @ 2010-02-18 19:47 Bruce Cran 2010-02-18 23:09 ` Ilpo Järvinen 0 siblings, 1 reply; 3+ messages in thread From: Bruce Cran @ 2010-02-18 19:47 UTC (permalink / raw) To: netdev 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 -- Bruce Cran ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: connect() sometimes succeeds when it shouldn't 2010-02-18 19:47 connect() sometimes succeeds when it shouldn't Bruce Cran @ 2010-02-18 23:09 ` Ilpo Järvinen 2010-02-19 10:55 ` Bruce Cran 0 siblings, 1 reply; 3+ messages in thread From: Ilpo Järvinen @ 2010-02-18 23:09 UTC (permalink / raw) To: Bruce Cran; +Cc: netdev On Thu, 18 Feb 2010, Bruce Cran wrote: > 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 Perhaps you just managed to dial your own number? ...Please try with my improved version of badconnect.c below. :-) -- 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; } ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: connect() sometimes succeeds when it shouldn't 2010-02-18 23:09 ` Ilpo Järvinen @ 2010-02-19 10:55 ` Bruce Cran 0 siblings, 0 replies; 3+ messages in thread From: Bruce Cran @ 2010-02-19 10:55 UTC (permalink / raw) To: Ilpo Järvinen; +Cc: netdev On Thursday 18 February 2010 23:09:09 Ilpo Järvinen wrote: > On Thu, 18 Feb 2010, Bruce Cran wrote: > > 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 > > Perhaps you just managed to dial your own number? ...Please try with > my improved version of badconnect.c below. :-) That was the problem: calling bind() before connect() fixed it. Sorry for the noise. -- Bruce Cran ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2010-02-19 10:55 UTC | newest] Thread overview: 3+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2010-02-18 19:47 connect() sometimes succeeds when it shouldn't Bruce Cran 2010-02-18 23:09 ` Ilpo Järvinen 2010-02-19 10:55 ` Bruce Cran
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox