I'm trying to test the listen backlog limits. I am collecting informations about this for some days, I know the backlog number provide to listen() is just a hint. But now I'm trying to create a server and a client (using TCP) to test the limit of the backlog.
On my server I use connect(), bind() and listen() and then sleep() for a long time. On my client I use a loop which create the fdsock and use connect().
The server isn't accepting anything so I think that the connections goes to the backlog queue but the programm never stop, even after having crated more than 1000 sockfd.
Is there a way to test the backlog queue maximum limit ?
Thanks by advance.
EDIT:
#include "sys.h"
char *answer(char *port, int (*fun)(int)){
struct addrinfo * info, * p, indices;
struct sockaddr fromaddr;
int fd, t, toclient;
unsigned int len = sizeof fromaddr;
memset(&indices, 0, sizeof indices);
indices.ai_flags = AI_PASSIVE;
indices.ai_family = AF_INET;
indices.ai_socktype = SOCK_STREAM;
t = getaddrinfo(0, port, &indices, &info);
if(t != 0)
{
return "answer: cannot get info on port";
}
for(p = info; p != 0; p = p->ai_next)
{
fd = socket(p->ai_family, p->ai_socktype, 0); // fabriquer la socket
if(fd >= 0)
{
break;
}
}
if(p == 0)
{
return "answer: cannot open socket";
}
t = 1;
t = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &t, sizeof t);
if(t < 0)
{
perror("setsockopt");
}
if(bind(fd, p->ai_addr, p->ai_addrlen) < 0)
{
close(fd);
return "answer: cannot bind socket";
}
freeaddrinfo(info);
t = listen(fd, 1);
if(t < 0)
{
close(fd);
return "answer: cannot listen on socket";
}
for(;;)
{
toclient = accept(fd, &fromaddr, &len);
sleep(600);
if(toclient < 0)
{
return "answer: cannot accept\n";
}
t = fork();
if(t < 0) // error
{
perror("fork");
continue;
}
else if(t != 0) // parent
{
close(toclient);
}
else // child
{
close(fd);
fun(toclient);
exit(0);
}
}
}
int foo(int n){
char c;
int t;
printf("I have a new client\n");
while((t = read(n, &c, 1)) > 0)
{
if(write(n, &c, 1) != 1)
{
perror("write");
break;
}
}
if(t < 0)
{
perror("read");
}
printf("I don't have that client anymore\n");
return 0;
}
int main(int argc, char *argv[]){
char *p;
p = answer(argv[1], foo);
perror(p);
return 1;
}
And here is the code for my client:
# include "sys.h"
int dial(char *machine, char *port){
struct addrinfo *info, *p, indices;
int toserver, t;
memset(&indices, 0, sizeof indices);
indices.ai_family = AF_INET;
indices.ai_socktype = SOCK_STREAM;
t = getaddrinfo(machine, port, &indices, &info);
if(t != 0)
{
return -1;
}
for(p = info; p != 0; p = p->ai_next)
{
toserver = socket(p->ai_family, p->ai_socktype, 0);
if(toserver >= 0)
{
break;
}
}
if(p == 0)
{
return -1;
}
t = connect(toserver, info->ai_addr, info->ai_addrlen);
if(t < 0)
{
freeaddrinfo(info);
close(toserver);
return -1;
}
freeaddrinfo(info);
return toserver;
}
int main(int argc, char *argv[]){
char buffer[1];
int t, fd = dial(argv[1], argv[2]);
while((t = read(0, buffer, sizeof buffer)) > 0)
{
write(fd, buffer, t);
t = read(fd, buffer, sizeof buffer);
write(1, buffer, t);
}
return 0;
}
I set the backlog length to 1 then I let the server sleep for a long time so I can try to launch some clients but even if I launch hundreds clients they're never rejects.