Results 1 to 1 of 1
I'v recently started programming w/ Linux sockets, and I have a program that creates a server and a client. Both are created in separate functions.The server allows only one connection, ...
- 03-21-2007 #1Just Joined!
- Join Date
- Mar 2007
- Posts
- 1
Prevent duplicate socket file descriptors?
I'v recently started programming w/ Linux sockets, and I have a program that creates a server and a client. Both are created in separate functions.The server allows only one connection, and the code uses a loop to restablish connections like this:
do
{
newfd = accept(sockfd,...)
do{
stat = recv(newfd,...)
if(stat<=0) break
//creates a client in a separate function here - it is opened and closed
// before returning
}while(1)
close(newfd)
...
}while(1)
Here's what I've noticed when connecting to the server:
-Initial connection to server
server sockfd = 5
server newfd = 7
client sockfd = 0
-disconnect from server
-Reconnect to server
server sockfd = 5
server newfd = 0
client sockfd = 0 //oh no!!
closing the client's sockfd now closes server's newfd.
Why would a duplicate file descriptor be issued? is there a way to prevent this?


Reply With Quote