Thread: WinSock program examples don't work with my compiler - MingGW , windows7

  1. #1
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288

    WinSock program examples don't work with my compiler - MingGW , windows7

    My book I had only covered socket programming for Mac and Linux, so I couldn't use it. Then when I got some samples from MSDN or other sites and tested it, those didn't work for me either. Here is one of the WinSock programs I found and the errors I got.

    Code:
    /*
        Live Server on port 8888
    */
    #include<io.h>
    #include<stdio.h>
    #include<winsock2.h>
     
    #pragma comment(lib,"ws2_32.lib") //Winsock Library
     
    int main(int argc , char *argv[])
    {
        WSADATA wsa;
        SOCKET s , new_socket;
        struct sockaddr_in server , client;
        int c;
        char *message;
     
        printf("\nInitialising Winsock...");
        if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
        {
            printf("Failed. Error Code : %d",WSAGetLastError());
            return 1;
        }
         
        printf("Initialised.\n");
         
        //Create a socket
        if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
        {
            printf("Could not create socket : %d" , WSAGetLastError());
        }
     
        printf("Socket created.\n");
         
        //Prepare the sockaddr_in structure
        server.sin_family = AF_INET;
        server.sin_addr.s_addr = INADDR_ANY;
        server.sin_port = htons( 8888 );
         
        //Bind
        if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
        {
            printf("Bind failed with error code : %d" , WSAGetLastError());
            exit(EXIT_FAILURE);
        }
         
        puts("Bind done");
     
        //Listen to incoming connections
        listen(s , 3);
         
        //Accept and incoming connection
        puts("Waiting for incoming connections...");
         
        c = sizeof(struct sockaddr_in);
         
        while( (new_socket = accept(s , (struct sockaddr *)&client, &c)) != INVALID_SOCKET )
        {
            puts("Connection accepted");
             
            //Reply to the client
            message = "Hello Client , I have received your connection. But I have to go now, bye\n";
            send(new_socket , message , strlen(message) , 0);
        }
         
        if (new_socket == INVALID_SOCKET)
        {
            printf("accept failed with error code : %d" , WSAGetLastError());
            return 1;
        }
     
        closesocket(s);
        WSACleanup();
         
        return 0;
    }
    Code:
    Errors :
    
    (.text+0x38): undefined reference to `WSAStartup@8'
    (.text+0x44): undefined reference to `WSAGetLastError@0'
    (.text+0x86): undefined reference to `socket@12'
    (.text+0x97): undefined reference to `WSAGetLastError@0'
    (.text+0xd2): undefined reference to `htons@4'
    :(.text+0xf9): undefined reference to `bind@12'
    (.text+0x106): undefined reference to `WSAGetLastError@0'
    (.text+0x141): undefined reference to `listen@8'
    (.text+0x1ad): undefined reference to `send@16'
    (.text+0x1cf): undefined reference to `accept@12'
    (.text+0x1e6): undefined reference to `WSAGetLastError@0'
    (.text+0x208): undefined reference to `closesocket@4'
    (.text+0x210): undefined reference to `WSACleanup@0'
    collect2: ld returned 1 exit status
    I compiled the program like this :

    Code:
    gcc internet.c -o in.exe 2> errors.txt
    How do I make it so I can compile winsock programs and be able to learn more about them (eventually being able to make my own)?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    #pragma comment isn't for MinGW.
    Code:
    gcc -Wall -pedantic -std=c99 internet.c -lws2_32
    internet.c:8:0: warning: ignoring #pragma comment  [-Wunknown-pragmas]
    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NAT in Windows7
    By Lieta in forum Windows Programming
    Replies: 1
    Last Post: 04-22-2013, 10:53 AM
  2. DLL Properties @ Windows7 using g++
    By artur in forum C++ Programming
    Replies: 11
    Last Post: 05-30-2012, 06:41 AM
  3. A C++ program examples showing Polymorphism, please help.
    By MarkSquall in forum C++ Programming
    Replies: 19
    Last Post: 06-06-2008, 04:41 AM
  4. I can't get this new compiler to work.
    By Loduwijk in forum C++ Programming
    Replies: 7
    Last Post: 03-29-2006, 06:42 AM
  5. Replies: 17
    Last Post: 09-28-2002, 07:35 AM