Thread: Error with first socket program

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    Error with first socket program

    So I started out with some basic socket programming, first on VC++ 9.0 but it gave me the same error as I got on Cygwin, apparently it's some 'linker error' but I really have no idea how to address that issue, apparently there are some dependencies called ws2_32.dll and ws2_32.lib that needs to be linked for the socket program to work properly. The error I get is:

    undefined reference to WSAStartup
    undefined reference to WSACleanup

    Code:
    //#include "stdafx.h"
    #include <windows.h>
    #include <winsock.h>
    #include <stdio.h>
    
    int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
      
      WSADATA ws;
      char buf[1000], buf1[100], buf2[100], buf3[100], buf4[100], buf5[100], buf6[100];
      
      WSAStartup (0x0101, &ws);
      
      buf[0] = "";
    
      sprintf(buf1,"\nWinsock Ver Requested : %d.%d", HIBYTE(ws.wVersion), LOBYTE(ws.wVersion));
      sprintf(buf2,"\nWinsock Ver Available : %d.%d", HIBYTE(ws.wHighVersion), LOBYTE(ws.wHighVersion));
      sprintf(buf3,"\nCurrent WinSock Implementation: %s", &ws.szDescription);
      sprintf(buf4,"\nSystem Status : %s", &ws.szSystemStatus);
      sprintf(buf5,"\nMaximum Sockets : %u", ws.iMaxSockets);
      sprintf(buf6,"\nMaximum Message Size : %u", ws.iMaxUdpDg);
      strcat(strcat(strcat(strcat(strcat(strcat(buf,buf1),buf2),buf3),buf4),buf5),buf6);
    
      MessageBox(0,buf,"Info",0);
      WSACleanup();
      
      return 0;
    }

  2. #2
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Aha, I found the answer, I'm supposed to link as such:

    gcc -Wall -o socket socket.c -lws2_32

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with simple socket client/server program
    By spencer88 in forum C Programming
    Replies: 6
    Last Post: 05-05-2009, 11:05 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Writing to an Output Socket in use by another program
    By maththeorylvr in forum Windows Programming
    Replies: 4
    Last Post: 10-28-2005, 12:17 PM
  4. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM