Solved

Winsock and Connection Failures

Posted on 2006-10-30
6
245 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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.
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.

762 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

12 Experts available now in Live!

Get 1:1 Help Now