Wsaerror

Aaron Isotton

2004-05-20

Revision History
Revision 12004-05-22
Improved documentation, added Makefile for documentation, added CVS ID to comment headers.

Abstract

Wsaerror - Retrieve human-readable error descriptions from WSAGetLastError()'s return value.


Table of Contents

About Wsaerror
Usage
Reference
wsa_strerror()
Example
Other Resources

About Wsaerror

Wsaerror's home page is http://www.isotton.com/devel/libs/wsaerror/. You can use it under the terms of the MIT License.

Usage

Include wsaerror.h wherever you want to convert error codes to strings, and compile wsaerror.c into your program. Then use wsa_strerror() wherever you need to get a human-readable version of an error code.

Reference

wsa_strerror()

#include "wsaerror.h"
const char* wsa_strerror (error); 
int  error;

Returns a human-readable, English description for an error code returned by WSAGetLastError(). If the error code is unknown, it returns “Unknown error.”. If you look at wsaerror.c, you'll notice that a few error codes are commented out; they're documented on MSDN, but mingw32 doesn't seem to know about them. Please tell me whether they're defined in Visual Studio, I didn't try.

Example

Example 1. Using Wsaerror in a Program

#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include "wsaerror.h"
int main() {
    int retval, sock;
    WSADATA wsad;

    retval = EXIT_SUCCESS;
    WSAStartup(MAKEWORD(2,2), &wsad);
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == INVALID_SOCKET) {
	fprintf(stderr, "Cannot create socket: %s\n",
                wsa_strerror(WSAGetLastError()));
        retval = EXIT_FAILURE;
    }
    WSACleanup();
    return retval;
}

Other Resources

The errors are explained more in Detail on the Windows Sockets Error Codes page at MSDN.