Advertisement

Basic Networking With DEV-Cpp

Started by May 03, 2005 02:06 PM
5 comments, last by hplus0603 19 years, 9 months ago
I've Read The Beej's Network Programming Guide And made a Basic network Code in DEV-Cpp :
#include <winsock.h> 

int main() 
{ 
    WSADATA WSAData; 
    int server; 
    WSAStartup(MAKEWORD(1,0), &WSAData); 
    int port = 6666; 
  
    SOCKADDR_IN sinserv; 
    sinserv.sin_family=AF_INET; 
    sinserv.sin_addr.s_addr=INADDR_ANY; 
    sinserv.sin_port=htons(port); 
    server=socket(AF_INET,SOCK_STREAM,0); 
    bind(server,(SOCKADDR*)&sinserv,sizeof(sinserv)); 
    listen(server,0); 
}

and i got the compiling errors:
Quote:
[Linker error] undefined reference to `WSAStartup@8' [Linker error] undefined reference to `htons@4' [Linker error] undefined reference to `bind@12' [Linker error] undefined reference to `socket@12' [Linker error] undefined reference to `listen@8' ld returned 1 exit status
What was i doin wrong? Roy
add the following line to linker parameters
-lws2_32
Dolphins - The sharks of the sea.
Advertisement
Wow!
it worked!
:):):)

can i know what that line did?
"-lws2_32"?

Roy
Quote:
Original post by RainbowInBlack
Wow!
it worked!
:):):)

can i know what that line did?
"-lws2_32"?

Roy

It links the Winsock2 library.
In addition, you want to include <winsock2.h> instead of <winsock.h>, and you want to include that before you include <windows.h>.

Also, you want to call WSAStartup() with version 2,2 of WinSock, instead of the 1,0 you have there now. Version 1,0 was very buggy, and when you say you want version 1,0, you get all the bugs, too.
enum Bool { True, False, FileNotFound };
OK, Thanks

BTW
what changed in winsock2? (in the function names, and every thing)
i can still use the same "socket","bind","send", etc functions?

Roy
Advertisement
Yes, you can still use the same functions; you just will get more of the behavior you'd expect and less of behavior that goes against the documentation.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement