Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4071844
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:48:31+00:00 2026-05-20T16:48:31+00:00

I have a server program that connects to another program via a given socket,

  • 0

I have a server program that connects to another program via a given socket, and in certain cases I need to close the connection and almost immediately re-open it on the same socket. This by and large works, except that I have to wait exactly one minute for the socket to reset. In the meantime, netstat indicates that the server sees the socket in FIN_WAIT2 and the client sees it as CLOSE_WAIT. I’m already using SO_REUSEADDR, which I thought would prevent the wait, but that isn’t doing the trick. Setting SO_LINGER to zero also does not help. What else can I do to resolve this?

Here are the relevant code snippets:

SetUpSocket()
{
   // Set up the socket and listen for a connection from the exelerate client.
   // Open a TCP/IP socket.
   m_baseSock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
   if (m_baseSock < 0)
   {
      return XERROR;
   }

   // Set the socket options to reuse local addresses.
   int flag = 1;
   if (setsockopt(m_baseSock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)) == -1)
   {
      return XERROR;
   }

   // Set the socket options to prevent lingering after closing the socket.
   //~ linger li = {1,0};
   //~ if (setsockopt(m_baseSock, SOL_SOCKET, SO_LINGER, &li, sizeof(li)) == -1)
   //~ {
      //~ return XERROR;
   //~ }

   // Bind the socket to the address of the current host and our given port.
   struct sockaddr_in addr;
   memset(&addr, 0, sizeof(addr));
   addr.sin_family = AF_INET;
   addr.sin_addr.s_addr = INADDR_ANY;
   addr.sin_port = htons(m_port);
   if (bind(m_baseSock, (struct sockaddr*)&addr, sizeof(addr)) != 0)
   {
      return XERROR;
   }

   // Tell the socket to listen for a connection from client.
   if (listen(m_baseSock, 4) != 0)
   {
      return XERROR;
   }
   return XSUCCESS;
}

ConnectSocket()
{
   // Add the socket to a file descriptor set.
   fd_set readfds;
   FD_ZERO(&readfds);
   FD_SET(m_baseSock, &readfds);

   // Set timeout to ten seconds. Plenty of time.
   struct timeval timeout;
   timeout.tv_sec = 10;
   timeout.tv_usec = 0;

   // Check to see if the socket is ready for reading.
   int numReady = select(m_baseSock + 1, &readfds, NULL, NULL, &timeout);
   if (numReady > 0)
   {
      int flags = fcntl(m_baseSock, F_GETFL, 0);
      fcntl(m_baseSock, flags | O_NONBLOCK, 1);

      // Wait for a connection attempt from the client. Do not block - we shouldn't
      // need to since we just selected.
      m_connectedSock = accept(m_baseSock, NULL, NULL);
      if (m_connectedSock > 0)
      {
         m_failedSend = false;
         m_logout = false;

         // Spawn a thread to accept commands from client.
         CreateThread(&m_controlThread, ControlThread, (void *)&m_connectedSock);

         return XSUCCESS;
      }
   }
   return XERROR;
}

ControlThread(void *arg)
{
   // Get the socket from the argument.
   socket sock = *((socket*)arg);

   while (true)
   {
      // Add the socket to a file descriptor set.
      fd_set readfds;
      FD_ZERO(&readfds);
      FD_SET(sock, &readfds);

      // Set timeout to ten seconds. Plenty of time.
      struct timeval timeout;
      timeout.tv_sec = 10;
      timeout.tv_usec = 0;

      // Check if there is any readable data on the socket.
      int num_ready = select(sock + 1, &readfds, NULL, NULL, &timeout);
      if (num_ready < 0)
      {
         return NULL;
      }

      // If there is data, read it.
      else if (num_ready > 0)
      {
         // Check the read buffer.
         xuint8 buf[128];
         ssize_t size_read = recv(sock, buf, sizeof(buf));
         if (size_read > 0)
         {
            // Get the message out of the buffer.
            char msg = *buf;
            if (msg == CONNECTED)
            {
               // Do some things...
            }
            // If we get the log-out message, log out.
            else if (msg == LOGOUT)
            {
               return NULL;
            }
         }
      }
   } // while
   return NULL;
}

~Server()
{
   // Close the sockets.
   if (m_baseSock != SOCKET_ERROR)
   {
      close(m_baseSock);
      m_baseSock = SOCKET_ERROR;
   }
   if (m_connectedSock != SOCKET_ERROR)
   {
      close(m_connectedSock);
      m_connectedSock = SOCKET_ERROR;
   }
}

SOCKET_ERROR is equal to -1. The server object gets destroyed, at which point the connection should close, and then recreated, at which point the SetUpSocket() and ConnectSocket() routines are called.

So why do I have to wait a minute for the socket to clear? Any ideas would be appreaciated.

EDIT:
Following the advice of my first posters, I found a way to get the client to close the socket from its end. Something still isn’t right, though. Now, netstat shows the socket from the server’s perspective in TIME_WAIT and there is no entry from the client’s perspective. All I’ve got is:

tcp 0 0 localhost.localdomain:19876 localhost.localdomain:54598 TIME_WAIT

and nothing from the other way around. The server and client still require exactly a minute for the TIME_WAIT to clear to be able to reconnect. Now what’s wrong – is using close() on the client’s socket incorrect?

EDIT 2:
Now, if I force the client to reconnect, it will immediately – but if I just let it do its own thing, it waits the full minute for the TIME_WAIT to clear. I suspect something is screwy in the client code. Not too much I can do about that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:48:31+00:00Added an answer on May 20, 2026 at 4:48 pm

    The server is waiting for the client to send a FIN packet. That should be done by closing the socket on the client side (or shutting down the app maybe). Then the server shall go to TIME_WAIT status, waiting for the time-out of the socket. The SO_REUSEADDR enables you to bypass this status.

    enter image description here

    (Source http://upload.wikimedia.org/wikipedia/commons/0/08/TCP_state_diagram.jpg)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a server that sends data via a socket, the data is a
I have a c++/windows program that receives data from another c++ program via a
I have a small server program that accepts connections on a TCP or local
I have a program that runs osql.exe from microsoft sql server tools directory and
I have a .Net program that I want to install on a terminal server.
I'm writing a program that will have both a server side and a client
I have a java program that connects to a MySql database and it's working
i have my C# program that work with sql-server 2008 Enterprise Edition i have
I have a client and server program (both in Obj-C) and I am transferring
I have a classic client/server (fat client and database) program written in Delphi 2006.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.