Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have implemented a design in C++ that is:- I parse an XML file which contain the ip and port of several servers,for each ip and port i first call the connect to server function in which i make a TCP socket connection with the server whether the connection is established or not i make a thread for each ip and port,if connection is not established i send the status of the server that connection is not established and if connection is established i then make a request to the server and receive response from the server in the thread.This is done after every minute for each thread. Now the problem that i am facing is,if connection terminates or if the server power goes,how to again make a connection i mean after every one minute before sending request and receiving response from the server i have to check whether the connection is still there or not. Can you please tell me how to do that?
Posted
Comments
Sergey Alexandrovich Kryukov 26-Oct-12 15:17pm    
First of all, why? If you do regular exchange via the network, the failure will show you the connection is lost, and when you don't exchange, you might not need connection. You need to thoroughly review your approach.
--SA
Tarun Batra 26-Oct-12 15:32pm    
sir plz tell one thing if the server is closed for a while and started after that can we send the data to the server with same socket with which we have previously connected to server or should we need to create a new socket connection?
Sergey Alexandrovich Kryukov 26-Oct-12 15:51pm    
If by "server is closed" you mean "process terminated" (there is nothing to "close", this is not a window), the connection is lost forever; you will need to connect again.
--SA

1 solution

Read your config file and then create a thread for each server entry. Every thread should do this:
C++
for (;;)  // infinite loop
{
   bool connect_success = connect_to_server();
   if (connect_success)
   {
      for (;;)
      {
          if (send_request())
          {
              if (receive_response())
              {
                  // TODO: process the response
              }
              else
              {
                  // error while receiving response
                  break;
              }
          }
          else
          {
              // error while sending request
              break
          }
      }
   }

   // waiting for 1 second
   Sleep(1000);
}

There is no "checking connection" functionality in the socket api. You find out if the connection is still alive only when you try to send or receive data and the send/recv function returns with error. This is why the send_request() and receive_response() functions in my example return a bool to indicate error.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900