Is there possible that accept() (on redhat Enterprise 4/linux kernel
2.6) return a same socket value for different tcp connections from the
same process of a same application and same machine?
I am so surprised that when I got such a result that many connections
have the same socket value on server side when I checked the log file!!
How is it possible?!!
By the way, I am using TCP blocking socket to listen.
-
main(){
-
int fd, clientfd, len, clientlen;
-
sockaddr_in address, clientaddress;
-
-
fd = socket(PF_INET, SOCK_STREAM, 0);
-
....
-
memset(&address, 0, sizeof address);
-
-
address.sin_address = AF_INET;
-
address.sin_port = htons(port);
-
....
-
bind(fd, &address, sizeof address);
-
listen(fd, 100);
-
do {
-
clientfd = accept(fd, &clientaddress, &clientlen);
-
-
if (clientfd < 0) {
-
....
-
}
-
-
printf("clientfd = %d", clientfd);
-
-
switch(fork()){
-
case 0:
-
-
//do something else
-
exit(0);
-
default:
-
...
-
}
-
-
-
} while(1);
-
}
my question is that why printf("clientfd = %d"); prints a same number for different connections!!!
Answers
If server runs in multiple processes (like Apache with mpm worker
model), then every process has its own file descriptor numbering
starting from 0.
In other words, it is quite possible that different processes will
get exact same socket file descriptor number. However, fd number it does
not really mean anything. They still refer to different underlying
objects, and different local TCP ports.
阅读(1052) | 评论(0) | 转发(0) |