[solved]winsock 2 linking? VS 2005

Feb 13, 2009 at 8:58pm
i'm having troubles linking the WS2 library (WS2_32.Lib) to my program

if someone could give me a heads up, it'd be much appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// client.cpp : Defines the entry point for the console application.
//

#include <WinSock2.h>
#include <ws2tcpip.h>
#include "stdafx.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>


using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	char buf[] = "Hello\n";
	WSAData wsdata; //Declare WSAData
	WORD wsver = MAKEWORD(2, 2); //We want Winsock 2.0
	int nret = WSAStartup(wsver, &wsdata); //Pass version 2.0 and pointer to implement
	if(nret != 0)
	{ 
		//Init failed
		/*A successful return value should be 0 */
		cout << "Startup failed, error code: " << WSAGetLastError(); //Returns error code
		WSACleanup(); //Cleanup Winsock library
		return -1;
	}
	cout << "Init success\n";
	SOCKET kSock=socket(AF_INET, SOCK_STREAM, 0);
	if(kSock == INVALID_SOCKET)
	{
		cout << "Socket init failed";
		return -1;
	}
	cout << "Socket initialized\n";
	sockaddr_in sin;
	sin.sin_port = htons(80);
	sin.sin_addr.s_addr = INADDR_ANY;
	sin.sin_family = AF_INET;
	if(bind(kSock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
	{
		cout << "Failed to bind\n";
		WSACleanup(); //Cleanup Winsock library
		return -1;
	}
	cout << "Bind successful!\n";
	while (listen(kSock, SOMAXCONN) == SOCKET_ERROR); //Loop in order to constantly listen
	/* set the number of connections to SOMAXCONN, in which case the provider chooses a reasonable value (5 in Windows XP
	Professional) */
	SOCKET client;
	int len = sizeof(sin);
	client = accept(kSock, (sockaddr*)&sin, &len);
	cout << "Connection established!\n";
	send(client, buf, sizeof(buf), 0); //Send "Hello"
	closesocket(client); //Close both socket handles
	closesocket(kSock);
	WSACleanup();
	return 0;
}

1
2
3
4
Errors such as..

error C2065: 'WSAData' : undeclared identifier	client.cpp	24
error C3861: 'WSAStartup': identifier not found	client.cpp	26


thanks in advance!
Last edited on Feb 16, 2009 at 4:53pm
Feb 13, 2009 at 11:09pm
I think you are not linking the winsock library...You have to go to your project settings and add winsock32.lib (not sure if the name is right though) to your project dependencies.
Feb 14, 2009 at 1:22am
i know i'm not linking it....specifically..how do i do it?

currently..i'm going to Project>Properties>Configuration Properties>Linker>Input>Additional Dependencies>(put in "ws2_32.lib")>OK

i had earlier put in the settings of visual studio to add the folder its in to the includes

i still get errors
Feb 14, 2009 at 1:42am
A lib isn't truly a header file.

This is for VC++ 2008, but it's probably the same:
Project -> Project Settings -> Config -> Linker -> Input

Go to additional depencies, and type in "wsock32.lib" without quotes.

It should work.
Feb 14, 2009 at 10:53pm
I'm pretty sure it's "ws2_32.lib"

I don't know if this would make any difference, but I don't the the include should have any capitals, i.e. #include <winsock2.h>
EDIT: Nope, it doesn't matter.

When you say added it to the include directories, you mean the directory list for library files, right?
Last edited on Feb 14, 2009 at 11:02pm
Feb 15, 2009 at 7:51am
ya in
tools>options>projects and solutions>vc++ directories
and i added the path to the libraries list

and firedraco..thats what i'm doing..but it isnt working lol
Feb 16, 2009 at 4:54pm
i figured it out...

[quote = http://www.vbforums.com/archive/index.php/t-248229.html]
CornedBee

lib doesn't matter, the problem lies with the header.

Ok, got it. The problem is the way VC++ handles precompiled headers. If you use a precompiled header (and you ALWAYS do when using MFC), the compiler ignores everything that comes before this header. Since the header in your case is stdafx.h, the include directive for winsock2.h is simply ignored....[/quote]
just had to place my stdafx.h at the top of includes
Topic archived. No new replies allowed.