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
Solved

Winsock and Connection Failures

Posted on 2006-10-30
6
251 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
  • 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 168 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 166 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 166 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

856 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