473,396 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Use of dup() and close()

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
0 2774

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

Similar topics

3
by: Daniel | last post by:
TcpClient close() method socket leak when i use TcpClient to open a connection, send data and close the TcpClient with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket on...
6
by: marcelf3 | last post by:
Hello.. This page opens a window with some information, but everytime the user changes a field in the parent window, the child window needs to be closed. These 2 functions were supposed to do the...
3
by: mike | last post by:
In a previous post I talked about what I thought was a memory leak based on some responses I was getting. I configured this page below to simulate adding and then deleting rows dynamically from...
35
by: Eric Sabine | last post by:
In my Finally block, I was using cn.close (where cn is an ADO.NET connection object, SQLConnection to be exact) and then I came across the following in some microsoft code. If Not cn Is Nothing...
4
by: CSN | last post by:
At startup this appears in the log: WARNING: dup(0) failed after 3195 successes: Bad file descriptor What does it mean? __________________________________________________
2
by: seifyr | last post by:
What sequence of calls to open, dup and/or dup2 do you perform in your program in order to achieve the following effects? $prog1>file 2>&1 and $prog2 2>&1 > file I understand which each...
0
by: gangesmaster | last post by:
what uses do you have to socket.dup? on *nixes it makes, to dup() the socket before forking, but how can that be useful on windows? -tomer
1
by: coldworld | last post by:
The following code is example code of the book "Beginning Linux Programming 3rd Edition" .... I've compiled and ran it...it's work... Can anyone tell me why the child process can read data from...
3
by: Nimion | last post by:
Sorry if I posted in the wrong forum, but since I'm looking at VB code I have a 50/50 chance at being wrong. :) I've been trying a variety of methods to speed up the checking for duplicates. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.