NO

Author Topic: winsock troubles  (Read 7627 times)

pyzaist

  • Guest
winsock troubles
« on: September 06, 2009, 03:58:34 AM »
I have been trying to learn winsock these days and I can't even start it up:

Code: [Select]
#include <winsock.h>
#include <stdio.h>

int main()
{
WSADATA wsaData;
WORD version=MAKEWORD(1,1);

if (WSAStartup(version, &wsaData) != 0) { exit(1); }



printf("testing...");

int WSACleanup(void);

return 0;
}

When I do this, I get one error:
Quote
POLINK: error: Unresolved external symbol '_WSAStartup'.

I have no idea why. Yes, I linked to wsock.lib. And it is a console program.
« Last Edit: September 06, 2009, 08:59:44 PM by pyzaist »

JohnF

  • Guest
Re: winsock troubles
« Reply #1 on: September 06, 2009, 08:18:02 AM »
Ws2_32.lib

John

pyzaist

  • Guest
Re: winsock troubles
« Reply #2 on: September 06, 2009, 09:01:00 PM »
Linked to that too

JohnF

  • Guest
Re: winsock troubles
« Reply #3 on: September 06, 2009, 09:28:32 PM »
I've just tried this here, all ok.

Code: [Select]
// test1.c

#include <winsock.h>
#include <stdio.h>

#pragma lib "Ws2_32.lib"

int main(void)
{
WSADATA wsaData;
WORD version = MAKEWORD(1, 1);

if (WSAStartup(version, &wsaData) != 0)
exit(1);

printf("testing...");

WSACleanup();

return 0;
}

John

pyzaist

  • Guest
Re: winsock troubles
« Reply #4 on: September 06, 2009, 09:57:38 PM »
Thank you! I guess I hadn't done that yet. I could've sworn I had.

pyzaist

  • Guest
Re: winsock troubles
« Reply #5 on: September 07, 2009, 12:12:58 AM »
now, when I go one step further, call getaddrinfo(), I gives a bunch of errors to. I do exactly what all the things say, but it still messes up:

Code: [Select]
#include <winsock.h>
#include <stdio.h>

#pragma lib "ws2_32.lib"

int main()
{
WSADATA wsaData;
WORD version = MAKEWORD(1, 1);
if (WSAStartup(version, &wsaData) != 0)
exit(1);
printf("testing...\n");

/* begin messing up */

int errorcheck;

struct addrinfo hints, *servinfo, *p;

memset(&hints, 0, sizeof hints);
hints.ai_family=AF_UNSPEC;
hints.ai_socktype=SOCK_STREAM;
hints.ai_flags=AI_PASSIVE;

if ((errorcheck=getaddrinfo(NULL, "80", &hints, &servinfo)) != 0)
exit(1);

/* end messing up */

WSACleanup();
return 0;
}

Quote
Building wintest.obj.
C:\Documents and Settings\Timmy\Desktop\programming\C\windows\wintest.c(18): error #2149: Undefined size for 'hints' with type '(incomplete) struct addrinfo'.
C:\Documents and Settings\Timmy\Desktop\programming\C\windows\wintest.c(20): error #2085: Invalid type argument '(incomplete) struct addrinfo' to 'sizeof'.
C:\Documents and Settings\Timmy\Desktop\programming\C\windows\wintest.c(21): error #2152: Unknown field 'ai_family' of '(incomplete) struct addrinfo'.
C:\Documents and Settings\Timmy\Desktop\programming\C\windows\wintest.c(22): error #2152: Unknown field 'ai_socktype' of '(incomplete) struct addrinfo'.
C:\Documents and Settings\Timmy\Desktop\programming\C\windows\wintest.c(23): error #2152: Unknown field 'ai_flags' of '(incomplete) struct addrinfo'.
C:\Documents and Settings\Timmy\Desktop\programming\C\windows\wintest.c(23): error #2048: Undeclared identifier 'AI_PASSIVE'.
C:\Documents and Settings\Timmy\Desktop\programming\C\windows\wintest.c(25): warning #2027: Missing prototype for 'getaddrinfo'.
C:\Documents and Settings\Timmy\Desktop\programming\C\windows\wintest.c(18): warning #2114: Local 'p' is not referenced.
*** Error code: 1 ***
Done.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: winsock troubles
« Reply #6 on: September 07, 2009, 07:32:42 AM »
Did you enabled M$ extensions in compiler options tab?
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

JohnF

  • Guest
Re: winsock troubles
« Reply #7 on: September 07, 2009, 08:34:33 AM »
Code: [Select]
// test1.c
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>

#pragma lib "ws2_32.lib"

int main(void)
{
WSADATA wsaData;
WORD version = MAKEWORD(1, 1);
if (WSAStartup(version, &wsaData) != 0)
exit(1);
printf("testing...\n");

/* begin messing up */

int errorcheck;

struct addrinfo hints, *servinfo, *p;

memset(&hints, 0, sizeof hints);
hints.ai_family=AF_UNSPEC;
hints.ai_socktype=SOCK_STREAM;
hints.ai_flags=AI_PASSIVE;

if ((errorcheck=getaddrinfo(NULL, "80", &hints, &servinfo)) != 0)
exit(1);

/* end messing up */

WSACleanup();
return 0;
}

John

pyzaist

  • Guest
Re: winsock troubles
« Reply #8 on: September 08, 2009, 01:55:41 AM »
yes, I had on MS extensions, and once I used Johns code, it worked. I guess I needed winsock2 and that other library. Thanks again.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: winsock troubles
« Reply #9 on: September 08, 2009, 09:22:22 AM »

Quote
The getaddrinfo function provides protocol-independent translation from host name to address. Note that the getaddrinfo function is a Microsoft-specific extension to the Windows Sockets 2 API.

May the source be with you

JohnF

  • Guest
Re: winsock troubles
« Reply #10 on: September 08, 2009, 10:26:24 AM »
yes, I had on MS extensions, and once I used Johns code, it worked. I guess I needed winsock2 and that other library. Thanks again.

You need to look these things up on the MSDN site. At the bottom of each API page you will see which headers and which libraries to use.

http://msdn.microsoft.com/en-us/library/ms738520(VS.85).aspx

John

pyzaist

  • Guest
Re: winsock troubles
« Reply #11 on: September 08, 2009, 10:57:50 PM »
Thanks for the info, and I apologize for my noobiness.