By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,773 Members | 1,777 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,773 IT Pros & Developers. It's quick & easy.

Use of dup() and close()

P: 29
I have the following code snippet that is used in a simple multi-threaded server program I am testing. The ServerSocket class binds to a port and creates a listening socket. Then it sits and waits for connection requests. When the request comes in, it returns a socket file descriptor for that connection. This is used to launch the client session thread. The Session class uses dup() to duplicate the file descriptor generated by ServerSocket into its own local copy.

The issue I am noticing is that after the Session thread exits and closes the duplicated socket file descriptor it does not appear that the original file descriptor (newSocketFd below) was closed or released. Is this normal, good or bad? and is there a way to address the closing of the original FD?

Thanks!

Expand|Select|Wrap|Line Numbers
  1. // in main.cpp
  2.       ServerSocket server(29000);
  3.       int newSocketFd;
  4.       while(true)
  5.       {
  6.          // Wait for and accept incoming client connections.
  7.          // Return the file descriptor for the established connection with
  8.          // the client.
  9.          newSocketFd = server.accept();
  10.  
  11.          // Create a new session thread using the connection file descriptor.
  12.          Session *ses = new Session(newSocketFd);
  13.  
  14.          // Run client session thread
  15.          ses->Execute();
  16.       }
  17.  
  18. // In Session.cpp
  19.  
  20.       Session::Session(int &FD)
  21.       {
  22.           // mSocketFd is defined in Session.h as
  23.           // private: int mSocketFd;
  24.           mSocketFd = dup(FD);
  25.       }
  26.  
  27.       Session::Execute()
  28.       {
  29.           //....................
  30.           //  Create and execute the client connection thread.
  31.           //....................
  32.  
  33.           while(connected)
  34.           {
  35.              // Send and receive data through mSocketFd;
  36.           }
  37.  
  38.           // When the thread is done, close the file descriptor for the
  39.           // client socket.
  40.           close(mSocketFd);
  41.       }
  42.  
And here is some basic output from the code above on the server side (described above in main.cpp):

Expand|Select|Wrap|Line Numbers
  1. Startup!!!
  2. Successfully opened ServerSocket socket: 3
  3. Successfully bound socket 3 to port 29000
  4. Listening for incoming connections on socket: 3
  5.  
  6. Accepted connection request on socket: 3
  7. Created new client socket (newSocketFd) : 4
  8. Duplicated session socket (Session::mSocketFd): 5
  9. Running Session thread...
  10. received data: "Hello World"
  11. Closing Session socket: 5
  12.  
  13. // new session without restarting the server:
  14. Accepted connection request on socket: 3
  15. Created new client socket (newSocketFd) : 6
  16. Duplicated session socket (Session::mSocketFd): 5
  17. Running Session thread...
  18. received data: "Hello World"
  19. Closing Session socket: 5
  20.  
  21. // And so on.......
  22.  
Mar 4 '09 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.