locked
Winsock Problems RRS feed

  • Question

  • Okay, I'm trying to compile a winsock server example from http://www.nullterminator.net/winsock.html. And as you can get I had a few problems. Well, I compiled my attempt at the code and got a couple of thousand errors. Instead of posting them one by one, I thought that I probably just missed one bit of code or typed one thing wrong and its given me all of these errors because of it. Please help... Heres my code: #include #include WSADATA wsadata; WORD version; int error; int main() { version = MAKEWORD( 2, 0 ); error = WSAStartup( version, &wsaData ); // check for errors in the in the WSAStartup if (error != 0) { return FALSE; } // check if the proper version was selected if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 0 ) { WSACleanup(); return FALSE; } // winsock properly initialized // declare the socket for the server SOCKET server; // set the socket up for TCP/IP and a stream socket server = socket ( AF_INET, SOCK_STREAM, 0 ); // create a struct for binding the socket struct sockaddr_in sin; // set up a buffer memset( &sin, 0, sizeof sin ); // set up the struct with all the variables sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_post = htons( 3390 ); // bind the struct to the socket if ( bind( server, &sin, sizeof sin ) == SOCKET_ERROR ) { // couldnt start the server return FALSE; } // start listening on the socket while ( listen ( server, SOMAXCONN ) == SOCKET_ERROR ); // a client connected SOCKET client; int length; // accept the connection length = sizeof sin; client = accept ( server, &sin, &length ); // close the socket closesocket( server ); // clean up WSACleanup(); // end the program return FALSE; }
    Friday, June 29, 2007 6:17 AM

Answers

  • Assuming the code you show is accurate, the first problem you have is that your code starts with #include statements that don't specify a file to be included.  The example you are copying starts with


    #include <windows.h>
    #include <winsock2.h>
     

     

    Without including winsock2.h, the compiler won't know anything about types like SOCKET etc.  That would certainly produce many errors.

    Friday, June 29, 2007 7:13 AM

All replies

  • Assuming the code you show is accurate, the first problem you have is that your code starts with #include statements that don't specify a file to be included.  The example you are copying starts with


    #include <windows.h>
    #include <winsock2.h>
     

     

    Without including winsock2.h, the compiler won't know anything about types like SOCKET etc.  That would certainly produce many errors.

    Friday, June 29, 2007 7:13 AM
  • Your code also starts by declaring a variable called "wsadata" (note the lower case 'd') but seems to use "wsaData" (note the upper case 'D') in the call to WSAStartup and elsewhere.  The C++ language is case-sensitive, wsadata and wsaData are two different names.

    Friday, June 29, 2007 7:24 AM