Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Visual-Studio TCP/IP , +
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 26-Oct-12 8:29am
Comments
Sergey Alexandrovich Kryukov at 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 at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Read your config file and then create a thread for each server entry. Every thread should do this:
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.
  Permalink  
v2

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

  Print Answers RSS
0 Kornfeld Eliyahu Peter 169
1 George Jonsson 145
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 28 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100