Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

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;
            }            
      }

0
SGyves
Asked:
SGyves
  • 2
  • 2
1 Solution
 
Roshan DavisCommented:
Try this, just a guess

         while(aPos && (success == false))
         {
              nextServer = dPtr->GetServerList()->GetNext(aPos);
               ircSock->Close();

              if(ircSock->Create(nextServer->GetRandomPort(), SOCK_STREAM, nextServer->GetServer()) == TRUE)
              {
                   success = true;
              }
         }
         if(!(aPos))
         {
              SetEvent(dPtr->m_hContinueClose);    
               return 0;
         }          

Good Luck
0
 
SGyvesAuthor Commented:
what about Connecting rosh??? I see create but no connect.
0
 
Roshan DavisCommented:
Ohh yes, after the Create(0, then call Connect().Create will call Bind.
 Just a try out, not sure
0
 
SGyvesAuthor Commented:
Sorry for the delay on the points Rosh.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now