Subject: Re: mingw32 sock-faq examples?
From: [email-masked] (Benoit Papillault)

21 Jun 1998 01:06:35 -0700
Message-ID: <Pine.GSO.3.96.980620143446.18074A-100000.cygnus.gnu-win32@dilbert>
References: <[email-masked]>
Mime-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
To: Laszlo Vecsey <[email-masked]>
Cc: [email-masked]

On Fri, 19 Jun 1998, Laszlo Vecsey wrote:

> Has anyone gotten the Unix Sockets FAQ examples to compile with mingw32?
> 
> 	http://www.ibrado.com/sock-faq/
> 
> I'm using the stock egcs-mingw32 distribution from cygnus, linking the
> examples against -lwsock32 (including windows.h and windows32/sockets.h),
> but listen(), accept(), connect() etc are always failing with -1.
> gethostname() also doesnt work, though IP addresses seem ok.
> 
> I've checked the mailing list archive, and tried the suggestion of
> undefining WIN32, WINNT, _WIN32, and even __WIN32__.. no luck. If I nm and
> grep my libwsock32.a and compiled test binaries, I notice things like
> 'listen@8' and 'accept@12' .. whats wrong?

I do not use mingw32 myself but I'm using sockets everyday. Many problems
comes from the TCP/IP stack your are using. First try to compile the
following problem to known which version of Winsock you are using (even if
you do not use Winsock directly).

#include <winsock.h>                                                            
#include <stdio.h>                                                              

int main()                                                                      
{                                                                               
        WSADATA data;                                                           
                                                                                
        if (WSAStartup(0x101,&data)==0)                                         
        {                                                                       
                printf("wVersion=%x\n",data.wVersion);                          
                printf("wHighVersion=%x\n",data.wHighVersion);                  
                printf("szDescription=\"%s\"\n",data.szDescription);            
                printf("szSystemStatus=\"%s\"\n",data.szSystemStatus);          
                printf("iMaxSockets=%d\n",data.iMaxSockets);                    
                printf("iMaxUdpDg=%d\n",data.iMaxUdpDg);                        
        }                                                                       
        else                                                                    
                perror("WSAStartup"); 
        return 0;                                                               
}                                                                               

The most important is wVersion and wHighVersion. If you get wVersion=2,
wHighVersion=2, I think you should be in trouble.

Notice that the WSAStartup() is needed before using any socket() functions
under Windows. However, I you are using mingw32 or GnuWin32, this is not
necessary. 

Why listen@8 and  accept@12? I wonder myself. I think it has to deal with
the PASCAL (or WINAPI) calling conventions. But I have the same think
here. And the real symbol names (in the DLL) is listen or accept, no @.

If you are writing a small piece of code using sockets, you can use
directly Winsock by:
1/ Calling the previous piece of code before using any socket functions.
2/ Replace all socket descriptors to SOCKET. (int s; -> SOCKET s;)
3/ Use only recv() and send() instead of read()/write().

And if you want to have more details about the errors you get, here is a
piece of code which translate Windows error number to sentences. If the
error is something like 100xx, look into winsock.h to have the error
explanation. (WSAECONNREFUSED, ...)

void my_perror(FILE *fp,const char *s)
{
        char *msg;                                                              
        DWORD last_err;                                                         
                                                                                
        last_err = GetLastError();                                              
                                                                                
        if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|                      
                FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,       
                NULL,last_err,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),         
                (LPVOID)&msg,0,NULL))                                           
        {                                                                       
                fprintf(fp,"%s: GetLastError()=%d\n",s,last_err);               
                fflush(fp);                                                     
        }                                                                       
        else                                                       
        {                                                                       
                fprintf(fp,"%s: (%d) %s\n",s,last_err,msg);               
                fflush(fp);                                               
                LocalFree(msg);   
        }                                                                       
} 

Use as my_perror(stderr,"WSAStartup failed"); for example.

Benoit.
Currently developping with ATM sockets :-)

-
For help on using this list (especially unsubscribing), send a message to
"[email-masked]" with one line of text: "help".