Solved

CSocket Question

Posted on 2003-11-06
4
330 Views
Last Modified: 2013-11-20
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
Comment
Question by:SGyves
  • 2
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
Roshan Davis earned 400 total points
ID: 9698762
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
 

Author Comment

by:SGyves
ID: 9698953
what about Connecting rosh??? I see create but no connect.
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9699071
Ohh yes, after the Create(0, then call Connect().Create will call Bind.
 Just a try out, not sure
0
 

Author Comment

by:SGyves
ID: 9819903
Sorry for the delay on the points Rosh.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now