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.
Ws2_32.lib
John
Linked to that too
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
Thank you! I guess I hadn't done that yet. I could've sworn I had.
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.
Did you enabled M$ extensions in compiler options tab?
// 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
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.
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.
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
Thanks for the info, and I apologize for my noobiness.