Link to home
Start Free TrialLog in
Avatar of SGyves
SGyves

asked on

CSocket Question

I have a program that connects to a server using a CSocket. There are a list of servers that are available to the application. So what I want is this...if the application cannot connect to the first server...I want it to cycle through a list until it either runs out of servers to try, or makes a good connection. So at the connect statement, I have it check to see if it was successful...if not...I change the server and port parameters and try again. For some reason...this is not working. Is there something I have to do to the socket in order to make this process work? Right now it does not work. Do I have to reinitialize the socket before I try another server? How can I make this work correctly? Here is my code for the loop:

if(ircSock->Connect() == FALSE)
      {
            
            //Here is where we are going to cycle through all possible servers
            POSITION aPos = dPtr->GetServerList()->GetHeadPosition();
            CIRCServer* nextServer;
            bool success = false;

            while(aPos && (success == false))
            {
                  nextServer = dPtr->GetServerList()->GetNext(aPos);
                  ircSock->SetServer(nextServer->GetServer());
                  ircSock->SetPort(nextServer->GetRandomPort());
                  if(ircSock->Connect() == TRUE)
                  {
                        success = true;
                  }
            }
            if(!(aPos))
            {
                  SetEvent(dPtr->m_hContinueClose);      
                  return 0;
            }            
      }

ASKER CERTIFIED SOLUTION
Avatar of Roshan Davis
Roshan Davis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SGyves
SGyves

ASKER

what about Connecting rosh??? I see create but no connect.
Ohh yes, after the Create(0, then call Connect().Create will call Bind.
 Just a try out, not sure
Avatar of SGyves

ASKER

Sorry for the delay on the points Rosh.