writing a simple server in c for windows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shailesh333
    New Member
    • Aug 2009
    • 10

    writing a simple server in c for windows

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <Winsock2.h>
    #include <stdlib.h>
    
    #define PORTNUM 2343
    
    int main(int argc, char *argv[])
    {
        char msg[] = "Hello World !\n";
     
        struct sockaddr_in dest; /* socket info about the machine connecting to us */
        struct sockaddr_in serv; /* socket info about our server */
        int mysocket;            /* socket used to listen for incoming connections */
        int socksize = sizeof(struct sockaddr_in);
     
        memset(&serv, 0, sizeof(serv));    /* zero the struct before filling the fields */
        serv.sin_family = AF_INET;         /* set the type of connection to TCP/IP */
        serv.sin_addr.s_addr = INADDR_ANY; /* set our address to any interface */
        serv.sin_port = htons(PORTNUM);    /* set the server port number */    
     
        mysocket = socket(AF_INET, SOCK_STREAM, 0);
     
        /* bind serv information to mysocket */
        bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));
     
        /* start listening, allowing a queue of up to 1 pending connection */
        listen(mysocket, 1);
        int consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
     
        while(consocket)
        {
            
     
            printf("Incoming connection from %s - sending welcome\n", inet_ntoa(dest.sin_addr));
            send(consocket, msg, strlen(msg), 0);
     
            
        }
        close(consocket);
        close(mysocket);
        return EXIT_SUCCESS;
    }

    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.
  • sridhard2406
    New Member
    • Dec 2007
    • 52

    #2
    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.

    Comment

    • shailesh333
      New Member
      • Aug 2009
      • 10

      #3
      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.

      Comment

      • Airslash
        New Member
        • Nov 2007
        • 221

        #4
        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:
        Code:
        #include <winsock2.h>
        #include <ws2tcpip.h>
        #include <stdio.h>
        Initialization is done by the following code:

        Code:
        // Prepare 
        WSADATA wsaData;
        int iResult;
        
        // Initialize Winsock
        // load version 2.0
        iResult = WSAStartup(MAKEWORD(2,0), &wsaData);
        if (iResult != 0) {
            printf("WSAStartup failed: %d\n", iResult);
            return 1;
        }
        After finishing your program's routines; you need to call this function to terminate the library
        Code:
        WSACleanup();
        You should visit this site, it has nice tutorials and the complete reference for the winsock2 library and API calls
        Use the links and resources in this topic as a step-by-step guide to getting started with Windows Sockets programming.

        Comment

        • shailesh333
          New Member
          • Aug 2009
          • 10

          #5
          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.

          Comment

          Working...