socket(): No error

socket(): No error

Post by Mick » Fri, 17 Jun 2005 22:02:40


I've been playing with sockets on Linux recently and decided to port some of
my working code to Windows XP. I downloaded lcc and got the code to compile
and link with only a few changes to the #includes's. Although compiling and
linking OK the following piece of code fails to run with "socket(): No
error". Any ideas?

#include <stdio.h>
#include <winsock2.h>

int main ()
{
int sock_fd;

if ((sock_fd = socket (AF_INET, SOCK_DGRAM, 0)) == -1) {
perror ("socket()");
exit (1);
}
}

Mick
 
 
 

socket(): No error

Post by John » Fri, 17 Jun 2005 23:55:26


Mick

Retrieve the last error with WSAGetLastError()

if ((sock_fd = socket (AF_INET, SOCK_DGRAM, 0)) == -1) {
int dwError = WSAGetLastError();
printf("%d\n", dwError);
exit (1);
}

The error is 10093 which according to the SDK means

==================
Successful WSAStartup not yet performed.
Either the application has not called WSAStartup or WSAStartup failed.
The application may be accessing a socket that the current active task
does not own (that is, trying to share a socket between tasks), or
WSACleanup has been called too many times.
==================

John

 
 
 

socket(): No error

Post by Mick » Tue, 21 Jun 2005 22:25:40

John,

Thanks, that pointed me in the right direction. I simply didn't realise you
needed to call an additional routine, WSA Startup. Now I'm reading the
Microsoft web site and have the code working.

Mick