New Project
  


Publications
 
New Project
NAVIGATION
Home
Complete Projects
Zawarsheni (за&#
Publications
Vitsove (виm
-> Exomo's site
-> Beej Networking Tutorial
-> Forger's WinAPI Tutorial
-> Johnnie WinSock Tutorial
->P2P through NAT
DSP guide
Lichno
Links
Blog






    
Direct Sound
Direct Sound wrapper classes.
download here.


How to get sensible message from WinSock?
If you are network programmer (or wish to become one) you will surely meet the WSAGetLast Message() function. Here I rewrote a simple ReportError() function. The original one is from the Johnnie's Winsock Tutorial.
/*
  Name:ReportError.cpp
  Author: Aleksandar Pavlov
    a.k.a Scorpion in http://cpp-home.com/forum
     and     http://sourcecore.net/forums
  Description: this function translates error message returned by
    WSAGetLastError() into something understandable.
    Basic constuction got from Johnnie's Winsock Tutorial,
    all error meanings are got from MS VC++ 6.0 Docs,
    brought together by me.  
  Date: 15.02.06 15:16
  Copyright: free
*/

#include <windows.h>
#include <stdio.h>


void ReportError(int errorCode, const char *whichFunc)
{

   char errorMsg[100];          // Declare a buffer to hold the generated messages
   char caption[50];
   ZeroMemory(errorMsg, 100);            // Automatically NULL-terminate the strings
   ZeroMemory(caption, 50);
  
   sprintf(caption, "Socket Error on function %s !", (char *)whichFunc);
   switch(errorCode)
   {case 10014:

    sprintf(errorMsg, "Bad address.");//
    break;
    case 10022:
    sprintf(errorMsg, "Invalid argument.");//
    break;
    case 10036:
    sprintf(errorMsg, "Operation now in progress.");//
    break;
    case 10037:
    sprintf(errorMsg, "Operation already in progress. ");//
    break;
    case 10038:
    sprintf(errorMsg, "Socket operation on non-socket.");//
    break;
    case 10040:
    sprintf(errorMsg, "Message too long.");//
    break;
    case 10048:
    sprintf(errorMsg, "Address already in use.");//
    break;
    case 10049:
    sprintf(errorMsg, "Cannot assign requested address.");//
    break;
    case 10050:
    sprintf(errorMsg, "Network is down.");//
    break;
    case 10053:
    sprintf(errorMsg, "Software caused connection abort.");//
    break;
    case 10054:
    sprintf(errorMsg, "Connection reset by remote side.");//
    break;
    case 10055:
    sprintf(errorMsg, "No buffer space available.");//
    break;
    case 10057:
    sprintf(errorMsg, "Socket is not connected.");//
    break;
    case 10060:
    sprintf(errorMsg, "Connection timed out.");//
    break;
    case 10061:
    sprintf(errorMsg, "Connection refused.");//
    break;      
    case 10064:
    sprintf(errorMsg, "Host is down.");//
    break;      
/*  case :
    sprintf(errorMsg, "");
    break;
*/ default:
   sprintf(caption, "Socket Indication");
   sprintf(errorMsg, "Call to %s returned error %d!", (char *)whichFunc, errorCode);  
   }

 MessageBox(NULL, errorMsg, caption, MB_OK  |  MB_ICONERROR);// show it finally
 return;
};//end of ReportError()

How to use it:

int nRet
sckt = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sckt == INVALID_SOCKET)
{
      nRet = WSAGetLastError();
      ReportError(nRet, "socket()");
      WSACleanup();          // Shutdown Winsock
      return ERROR;               //error
}

Or:

int nRet = listen(sckt, 2);
 if (nRet == SOCKET_ERROR)
 {  nRet = WSAGetLastError();
      ReportError(nRet, "listen()");
      closesocket(sckt);
      WSACleanup();
      return ERROR;               //error
}



© 2005 All Rights Reserved.


Create a free website at Webs.com