Solved

Winsock and Connection Failures

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 139
C++ standard library based binary archive format 6 98
Header of docx file 17 102
Copy output image from TWindowsMediaPlayer 6 40
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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

813 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

16 Experts available now in Live!

Get 1:1 Help Now