Advertisement

socket() returns -1 in WinSock2, why? (works on linux) [solved]

Started by June 08, 2005 05:18 PM
1 comment, last by MaulingMonkey 19 years, 8 months ago
I've finally gotten around to remembering my networking stuff. With my internet barely working, I decided to get it up and running on my linux box using manpages first - a simple program that broadcasts and recieves packets. I got it working:
panda@industry:~/sockettest$ ./test --listen &
[1] 2751
panda@industry:~/sockettest$ ./test --broadcast
Sending: My name is Mike and I like rootbeer.
Recieved: My name is Mike and I like rootbeer.
Sending: My name is Mike and I like rootbeer.
Recieved: My name is Mike and I like rootbeer.
^C

panda@industry:~/sockettest$ fg %1
./test --listen
^C

panda@industry:~/sockettest$ _
After this, I ported the program to windows using WinSock2 by looking at the headers. I was pleased to find the program compiled with very few changes, but the program now refuses to work on windows - it runs, but socket() returns -1. What's weird is that checking strerror( errno ) returns "No error". I'm running Windows XP SP2, w/ firewall enabled, although adding the program to the exceptions list dosn't help. I'm running it from the command console:
C:\eclipse\workspace\test.1>Debug\test.1 --listen
socket() failed: No error
TCP/IP is installed and functioning normally, I assume UDP is packaged with that network-wise... even if it isn't, all these combinations (some TCP ones included) also return -1: int udp_socket = socket( PF_INET , SOCK_DGRAM , 0 ); int udp_socket = socket( PF_INET , SOCK_DGRAM , IPPROTO_UDP ); int tcp_socket = socket( PF_INET , SOCK_STREAM , 0 ); int tcp_socket = socket( PF_INET , SOCK_STREAM , IPPROTO_TCP ); Relevant source:
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

#include <unistd.h>
#include <errno.h>

//Linux headers:
//#include <sys/socket.h>
//#include <netinet/in.h>
//#include <arpa/inet.h>
//Windows headers:
#include <winsock2.h>

int main ( int argc , char ** argv )
{
        //*snip* - argument reading code

        int udp_socket = socket( PF_INET , SOCK_DGRAM , 0 );
        if ( udp_socket == -1 )
        {
        	cerr << "socket() failed: " << strerror( errno ) << endl;
        	return -1;
        }

        //*snip* - program never reaches past here on Windows
}


I'm compiling the program using MinGW's GCC: g++ -O0 -g3 -Wall -c -fmessage-length=0 -omain.o ../main.cc g++ -o test.1.exe main.o -lws2_32 0 errors or warnings. Anyone know what I'm doing wrong? [Edited by - MaulingMonkey on June 8, 2005 5:50:44 PM]
You did call WSAStartup, as indicated in the winsock documentation, didn't you?

Mark
Advertisement
Quote:
Original post by markr
You did call WSAStartup, as indicated in the winsock documentation, didn't you?

Mark


Nope, seeing as I don't have the winsock documentation locally (and as I've allready mentioned, my internet is barely working - by which I mean 80% of the time my attempts to post/read from GD.NET fail - even worse with most other websites).

I'll try that, thanks! :-).

Edit: Yup, that was it. Also, one needs to get error codes using WSAGetLastError instead of errno. Thanks Mark!

[Edited by - MaulingMonkey on June 8, 2005 5:34:39 PM]

This topic is closed to new replies.

Advertisement