Pelles C forum

C language => Beginner questions => Topic started by: pyzaist on September 06, 2009, 03:58:34 AM

Title: winsock troubles
Post by: pyzaist 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:

#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:
QuotePOLINK: error: Unresolved external symbol '_WSAStartup'.

I have no idea why. Yes, I linked to wsock.lib. And it is a console program.
Title: Re: winsock troubles
Post by: JohnF on September 06, 2009, 08:18:02 AM
Ws2_32.lib

John
Title: Re: winsock troubles
Post by: pyzaist on September 06, 2009, 09:01:00 PM
Linked to that too
Title: Re: winsock troubles
Post by: JohnF on September 06, 2009, 09:28:32 PM
I've just tried this here, all ok.


// 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
Title: Re: winsock troubles
Post by: pyzaist on September 06, 2009, 09:57:38 PM
Thank you! I guess I hadn't done that yet. I could've sworn I had.
Title: Re: winsock troubles
Post by: pyzaist 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:

#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;
}


QuoteBuilding 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.
Title: Re: winsock troubles
Post by: frankie on September 07, 2009, 07:32:42 AM
Did you enabled M$ extensions in compiler options tab?
Title: Re: winsock troubles
Post by: JohnF on September 07, 2009, 08:34:33 AM

// 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
Title: Re: winsock troubles
Post by: pyzaist 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.
Title: Re: winsock troubles
Post by: TimoVJL on September 08, 2009, 09:22:22 AM

QuoteThe 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.

Title: Re: winsock troubles
Post by: JohnF on September 08, 2009, 10:26:24 AM
Quote from: pyzaist 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.

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 (http://msdn.microsoft.com/en-us/library/ms738520(VS.85).aspx)

John
Title: Re: winsock troubles
Post by: pyzaist on September 08, 2009, 10:57:50 PM
Thanks for the info, and I apologize for my noobiness.