?
Solved

Winsock and Connection Failures

Posted on 2006-10-30
6
Medium Priority
?
254 Views
Last Modified: 2010-04-01
Hi,

i am trying to get a winsock to retry a connection attemp if it failed the first time.
How can i do this?

//----------------- MY CONNECTION CODE ----------------------------------

  if (::connect(s_, (sockaddr *) &addr, sizeof(sockaddr)))
  {
    error = strerror(WSAGetLastError());
    throw error;
  }

0
Comment
Question by:cossy74
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 17839540
Have you initialized Winsock? You have to make a call to WSAStartup() before you can use any of the Winsock functions.
0
 
LVL 86

Accepted Solution

by:
jkr earned 672 total points
ID: 17839544
Well, just like e.g.

//----------------- MY CONNECTION CODE ----------------------------------

#define MAX_TRY_COUNT 42
unsigned unTryCount = 0;

while(unTryCount < MAX_TRY_COUNT)
{
  if (::connect(s_, (sockaddr *) &addr, sizeof(sockaddr)))
  {
    unTryCount++
    Sleep(1000);
  }
}

  if (unTryCount < MAX_TRY_COUNT)
  {
    error = strerror(WSAGetLastError());
    throw error;
  }
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 664 total points
ID: 17839545
Example:
     int nErr = 0;
     WORD wVersionRequested;
     WSADATA wsaData;

     //********* Initializing windows socket library ******************
     wVersionRequested = MAKEWORD(1,1);
     if(nErr = WSAStartup(   wVersionRequested,  &wsaData))
    {  
       printf("\nunable to initialize WINSOCK, reason: %d\n",nErr);
       exit(0);
    }
     else printf("WSAStartup OK....!!!\n");
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 664 total points
ID: 17840571
Don't think strerror can show socket errors. From my docs it shows errors from errno only.

You may write your own errortext function or take that:

std::string getErrorDesc ()
{
    // on NT we have no chance to get a error description, so we
    // have to hardcode the string. Yuck!
    int code = WSAGetLastError ();
    std::string msg = string ("NT socket error #") + string (code);
    switch (code) {
      case WSAEINVAL:             msg = "invalid parameter";                 break;
      case WSASYSNOTREADY:       msg = "system not ready";                 break;
      case WSAVERNOTSUPPORTED:    msg = "winsock version not supported";           break;
      case WSANOTINITIALISED:          msg = "winsock module not initialized";     break;
      case WSAENETDOWN:       msg = "network is down";                 break;
      case WSAHOST_NOT_FOUND:      msg = "host not found";                 break;
      case WSATRY_AGAIN:       msg = "temporary error - try again";           break;
      case WSANO_RECOVERY:       msg = "fatal network error";                 break;
      case WSANO_DATA:                       msg = "host has no address";                 break;
      case WSAENETRESET:       msg = "connection dropped - must be reset"; break;
      case WSAENOBUFS:                       msg = "buffer deadlock - windows error";    break;
      case WSAECONNABORTED:       msg = "connection was aborted";           break;
      case WSAECONNRESET:       msg = "connection was reset by remote side";break;
      case WSAEADDRINUSE:       msg = "address is already in use";           break;
      case WSAEADDRNOTAVAIL:       msg = "address is unavailable";                           break;
      case WSAENETUNREACH:       msg = "network is unreachable";           break;
      case WSAETIMEDOUT:       msg = "connection timed out";                 break;
      case WSAECONNREFUSED:       msg = "connection refused";                 break;
      case WSAEHOSTDOWN:       msg = "host is down";                           break;
      case WSAEHOSTUNREACH:       msg = "host is unreachable";                 break;
    }
    return msg;
}

Regards, Alex

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

765 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