obtaining error description

This is a discussion on obtaining error description within the C Programming forums, part of the General Programming Boards category; Hi, I can't seem to find out how to get the text description of a socket error progmatically. For example, ...

  1. #1
    sockets mad
    Join Date
    Mar 2002
    Posts
    126

    obtaining error description

    Hi,

    I can't seem to find out how to get the text description of a socket error progmatically.

    For example, say I've got this code:

    Code:
    ....
    
    char buf[10];
    int errno;
    
    int retval = recv(sock, buf, 10, 0);
    if (retval < 1) {
      errno = WSAGetLastError();
    }
    
    ....
    How would I then obtain a text description that describes errno?

    I tried using strerror() which works correctly on *nix, but on Windows it just returns "No error"

    Thanks in advance,

    Daniel
    C/C++ Support IRC Channel

    Server: irc.dal.net Channel: #csupport

    Come along and help make it a great resource for the community.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Create a lookup table and reference it. Take a jog over to the msdn and read up on said codes and their values and descriptions. Or, search the MSDN to see if there is an already made list for you.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Windows error codes can be converted to a string description using the FormatMessage() function.

    Code:
    void DisplayError(DWORD dwError)
    {
    	TCHAR szMsg[1024];
    
    	if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError,
    	                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    	                  szMsg, 1024, NULL))
    	{
    		printf("Error %d: %s\n", dwError, szMsg);
    	}
    	else
    	{
    		printf("Error %d: Unknown Error.\n", dwError);
    	}
    }
    Code:
    DisplayError(10060);
    produces:
    Error 10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
    Last edited by anonytmouse; 04-28-2004 at 08:14 PM.

  4. #4
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    Thanks for the help people.

    Why does windows have to be so awkward?....

    This is what I came up with in the end:

    Code:
    void get_sockerrdesc(int errno, char *buffer, int bufsize) {
    #ifdef _WIN32
    	if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errno,
    		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    		buffer, bufsize, NULL))
    	{
    		return 1;
    	} else {
    		strcpy(buffer,"Unknown Error");
    		return 0;
    	}
    #else
    	strcpy(buffer, strerror(errno));
    #endif
    }
    Notice how the *nix equivalent is oh so much simpler

    -dan
    C/C++ Support IRC Channel

    Server: irc.dal.net Channel: #csupport

    Come along and help make it a great resource for the community.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obtaining useful information from Crash Report
    By @nthony in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2009, 09:37 PM
  2. Replies: 21
    Last Post: 09-17-2008, 07:37 AM
  3. Obtaining real types from variants
    By therealwill in forum C++ Programming
    Replies: 6
    Last Post: 05-13-2006, 06:15 AM
  4. Obtaining a txt file through the web
    By robid1 in forum C Programming
    Replies: 7
    Last Post: 10-24-2003, 05:34 AM
  5. Obtaining data through RS-232 port...
    By johny780 in forum C Programming
    Replies: 9
    Last Post: 10-08-2003, 10:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21