By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,011 Members | 1,000 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,011 IT Pros & Developers. It's quick & easy.

writing a simple server in c for windows

P: 10
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <Winsock2.h>
  6. #include <stdlib.h>
  7.  
  8. #define PORTNUM 2343
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.     char msg[] = "Hello World !\n";
  13.  
  14.     struct sockaddr_in dest; /* socket info about the machine connecting to us */
  15.     struct sockaddr_in serv; /* socket info about our server */
  16.     int mysocket;            /* socket used to listen for incoming connections */
  17.     int socksize = sizeof(struct sockaddr_in);
  18.  
  19.     memset(&serv, 0, sizeof(serv));    /* zero the struct before filling the fields */
  20.     serv.sin_family = AF_INET;         /* set the type of connection to TCP/IP */
  21.     serv.sin_addr.s_addr = INADDR_ANY; /* set our address to any interface */
  22.     serv.sin_port = htons(PORTNUM);    /* set the server port number */    
  23.  
  24.     mysocket = socket(AF_INET, SOCK_STREAM, 0);
  25.  
  26.     /* bind serv information to mysocket */
  27.     bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));
  28.  
  29.     /* start listening, allowing a queue of up to 1 pending connection */
  30.     listen(mysocket, 1);
  31.     int consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
  32.  
  33.     while(consocket)
  34.     {
  35.  
  36.  
  37.         printf("Incoming connection from %s - sending welcome\n", inet_ntoa(dest.sin_addr));
  38.         send(consocket, msg, strlen(msg), 0);
  39.  
  40.  
  41.     }
  42.     close(consocket);
  43.     close(mysocket);
  44.     return EXIT_SUCCESS;
  45. }

When i try to compile i get the following error:
[Linker error] undefined reference to `htons@4'
[Linker error] undefined reference to `socket@12'
[Linker error] undefined reference to `bind@12'
[Linker error] undefined reference to `listen@8'
[Linker error] undefined reference to `accept@12'

How do i get rid of this error.
Any help will be appreciated . Thanks in advance.

I have installed dev c++ on my windows machine, and have tried to compile the code.
Dec 24 '09 #1
Share this Question
Share on Google+
4 Replies


P: 52
Hi,

Many function libraries and API's require you to link in their library files; Winsock2 is one such API. To do so, you need to add the appropriate instruction to your project. I abandoned the Dev-C++ IDE years ago, but I still use the compiler regularly by invoking it from the command line.

Winsock2 requires the library, libws2_32.a . gcc's command-line option for specifying a library file to be linked in is a lower-case "L" preceded by a minus sign. The "lib" prefix on the library file name is dropped, as is the ".a" file extension. The command-line invocation would be something like:
g++ filename.cpp -lws2_32

Somewhere in the IDE is a dialog box where you tell it what object and library files to link in.
Dec 24 '09 #2

P: 10
Hi
Thanks for the reply.
I checked the compile options in dev c++ and looked for the libraries... it includes the folder dev/lib which has all the ".a" files including libws2_32.a.
So I guess the issue you mentioned may not be the cause for the problem.
Dec 24 '09 #3

Airslash
100+
P: 221
you need to link to the ws2_32.lib file located in your system or system32 folder.
Also, prior to calling any kind of winsock function you need to initialize the library in Windows.

you need the following headers:
Expand|Select|Wrap|Line Numbers
  1. #include <winsock2.h>
  2. #include <ws2tcpip.h>
  3. #include <stdio.h>
  4.  
Initialization is done by the following code:

Expand|Select|Wrap|Line Numbers
  1. // Prepare 
  2. WSADATA wsaData;
  3. int iResult;
  4.  
  5. // Initialize Winsock
  6. // load version 2.0
  7. iResult = WSAStartup(MAKEWORD(2,0), &wsaData);
  8. if (iResult != 0) {
  9.     printf("WSAStartup failed: %d\n", iResult);
  10.     return 1;
  11. }
  12.  
After finishing your program's routines; you need to call this function to terminate the library
Expand|Select|Wrap|Line Numbers
  1. WSACleanup();
  2.  
You should visit this site, it has nice tutorials and the complete reference for the winsock2 library and API calls
http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
Dec 24 '09 #4

P: 10
Thanks guys,
i fixed the issue.
To compile the code i needed to link winsock32 lin.
I used the option "-lwsock32" , (without quotes) in the compile options.
Dec 25 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.