Solved

Winsock and Connection Failures

Posted on 2006-10-30
6
252 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

726 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