C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-03-2008, 08:13 PM   #1
Cogito Ergo Sum
 
Join Date: Mar 2007
Location: Sydney, Australia
Posts: 459
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;
}
JFonseka is offline   Reply With Quote
Old 02-03-2008, 08:47 PM   #2
Cogito Ergo Sum
 
Join Date: Mar 2007
Location: Sydney, Australia
Posts: 459
Aha, I found the answer, I'm supposed to link as such:

gcc -Wall -o socket socket.c -lws2_32
JFonseka is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:40 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22