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
- // in main.cpp
- ServerSocket server(29000);
- int newSocketFd;
- while(true)
- {
- // Wait for and accept incoming client connections.
- // Return the file descriptor for the established connection with
- // the client.
- newSocketFd = server.accept();
- // Create a new session thread using the connection file descriptor.
- Session *ses = new Session(newSocketFd);
- // Run client session thread
- ses->Execute();
- }
- // In Session.cpp
- Session::Session(int &FD)
- {
- // mSocketFd is defined in Session.h as
- // private: int mSocketFd;
- mSocketFd = dup(FD);
- }
- Session::Execute()
- {
- //....................
- // Create and execute the client connection thread.
- //....................
- while(connected)
- {
- // Send and receive data through mSocketFd;
- }
- // When the thread is done, close the file descriptor for the
- // client socket.
- close(mSocketFd);
- }
Expand|Select|Wrap|Line Numbers
- Startup!!!
- Successfully opened ServerSocket socket: 3
- Successfully bound socket 3 to port 29000
- Listening for incoming connections on socket: 3
- Accepted connection request on socket: 3
- Created new client socket (newSocketFd) : 4
- Duplicated session socket (Session::mSocketFd): 5
- Running Session thread...
- received data: "Hello World"
- Closing Session socket: 5
- // new session without restarting the server:
- Accepted connection request on socket: 3
- Created new client socket (newSocketFd) : 6
- Duplicated session socket (Session::mSocketFd): 5
- Running Session thread...
- received data: "Hello World"
- Closing Session socket: 5
- // And so on.......