MadWizard winsock tutorial

I found this really nice winsock tutorial online, and I'm still struggling with it but I need a working client example to play around with.
The one he provided at the end of his tutorial seems to have a type mismatch error.

I try to compile the following code for a client, and it gives me this error:
1>winsock.cpp(108): error C2664: 'lstrlenW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


I'm still pretty new to type casting, and I just can't figure out how to fix it.
Any ideas?

Here's the code:

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* 	Head request example
 *  View with tabsize = 4
 *	Part of the Winsock networking tutorial by Thomas Bleeker
 *	Visit www.MadWizard.org
 */
#include "stdafx.h"
#include <iostream>

#define WIN32_MEAN_AND_LEAN
#include <winsock2.h>
#include <windows.h>
#include <time.h>

using namespace std;

class HRException
{
public:
    HRException() :
         m_pMessage("") {}
    virtual ~HRException() {}
    HRException(const char *pMessage) :
         m_pMessage(pMessage) {}
    const char * what() { return m_pMessage; }
private:
    const char *m_pMessage;
};

const int  REQ_WINSOCK_VER   = 2;	// Minimum winsock version required
const char DEF_SERVER_NAME[] = "www.google.com";
const int  SERVER_PORT       = 80;	
const int  TEMP_BUFFER_SIZE  = 128;

const char HEAD_REQUEST_PART1[] =
{
	"HEAD / HTTP/1.1\r\n" 			// Get root index from server
	"Host: "						// Specify host name used
};

const char HEAD_REQUEST_PART2[] =
{
	"\r\n"							// End hostname header from part1
	"User-agent: HeadReqSample\r\n" // Specify user agent
	"Connection: close\r\n" 		// Close connection after response
	"\r\n"							// Empty line indicating end of request
};

// IP number typedef for IPv4
typedef unsigned long IPNumber;

IPNumber FindHostIP(const char *pServerName)
{
	HOSTENT *pHostent;

	// Get hostent structure for hostname:
	if (!(pHostent = gethostbyname(pServerName)))
			throw HRException("could not resolve hostname.");
	
	// Extract primary IP address from hostent structure:
	if (pHostent->h_addr_list && pHostent->h_addr_list[0])
		return *reinterpret_cast<IPNumber*>(pHostent->h_addr_list[0]);
	
	return 0;
}

void FillSockAddr(sockaddr_in *pSockAddr, const char *pServerName, int portNumber)
{
	// Set family, port and find IP
	pSockAddr->sin_family = AF_INET;
	pSockAddr->sin_port = htons(portNumber);
	pSockAddr->sin_addr.S_un.S_addr = FindHostIP(pServerName);
}

bool RequestHeaders(const char *pServername)
{
	SOCKET 		hSocket = INVALID_SOCKET;
	char		tempBuffer[TEMP_BUFFER_SIZE];
	sockaddr_in	sockAddr = {0};
	bool		bSuccess = true;
	
	try
	{
		// Lookup hostname and fill sockaddr_in structure:
		cout << "Looking up hostname " << pServername << "... ";
		FillSockAddr(&sockAddr, pServername, SERVER_PORT);
		cout << "found.\n";
	
		// Create socket
		cout << "Creating socket... ";
		if ((hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
			throw HRException("could not create socket.");
		cout << "created.\n";
		
		// Connect to server
		cout << "Attempting to connect to " << inet_ntoa(sockAddr.sin_addr)
		     << ":" << SERVER_PORT <<  "... ";
		if (connect(hSocket, reinterpret_cast<sockaddr*>(&sockAddr), sizeof(sockAddr))!=0)
		    throw HRException("could not connect.");
		cout << "connected.\n";
		
		cout << "Sending request... ";

		// send request part 1
		if (send(hSocket, HEAD_REQUEST_PART1, sizeof(HEAD_REQUEST_PART1)-1, 0)==SOCKET_ERROR)
			throw HRException("failed to send data.");

		// send hostname
		if (send(hSocket, pServername, lstrlen(pServername), 0)==SOCKET_ERROR)
			throw HRException("failed to send data.");

		// send request part 2
		if (send(hSocket, HEAD_REQUEST_PART2, sizeof(HEAD_REQUEST_PART2)-1, 0)==SOCKET_ERROR)
			throw HRException("failed to send data.");

		cout << "request sent.\n";
		
		cout <<	"Dumping received data...\n\n";
		// Loop to print all data
		while(true)
		{
			int retval;
			retval = recv(hSocket, tempBuffer, sizeof(tempBuffer)-1, 0);
			if (retval==0)
			{ 
				break; // Connection has been closed
			}
			else if (retval==SOCKET_ERROR)
			{
				throw HRException("socket error while receiving.");
			}
			else
			{
				// retval is number of bytes read
				// Terminate buffer with zero and print as string
				tempBuffer[retval] = 0;
				cout << tempBuffer;
			}
		}
	}
	catch(HRException e)
	{
		cerr << "\nError: " << e.what() << endl;
		bSuccess = false; 
	}

	if (hSocket!=INVALID_SOCKET)
	{
		closesocket(hSocket);
	}
	return bSuccess;
}	

int main(int argc, char* argv[])
{
	int iRet = 1;
	WSADATA wsaData;

	cout << "Initializing winsock... ";

	if (WSAStartup(MAKEWORD(REQ_WINSOCK_VER,0), &wsaData)==0)
	{
		// Check if major version is at least REQ_WINSOCK_VER
		if (LOBYTE(wsaData.wVersion) >= REQ_WINSOCK_VER)
		{
			cout << "initialized.\n";
			
			// Set default hostname:
			const char *pHostname = DEF_SERVER_NAME;
			
			// Set custom hostname if given on the commandline:
			if (argc > 1)
				pHostname = argv[1];
				
			iRet = !RequestHeaders(pHostname);
		}
		else
		{
			cerr << "required version not supported!";
		}

		cout << "Cleaning up winsock... ";

		// Cleanup winsock
		if (WSACleanup()!=0)
		{
			cerr << "cleanup failed!\n";
			iRet = 1;
		}   
		cout << "done.\n";
	}
	else
	{
		cerr << "startup failed!\n";
	}
	return iRet;

	system("pause");
}


Thanks in advance.
Don't use lstrlen. That's MS's stupid clone of the standard strlen function.

Just use strlen()
Mmm, that's very Windows specific, ... but that's not the problem. It's Windows and its Unicode support.

You can switch off unicode by changing your project settings, but more correctly (as WinSock doesn't support that stuff) add this to the top of your program.
1
2
3
#ifdef _UNICODE
#undef _UNICODE
#endif 
I tried both options. The strlen seems to have fixed it, or at least it doesn't complain about it anymore.
Now it complains about other stuff:


1>winsock.obj : error LNK2028: unresolved token (0A000349) "extern "C" int __stdcall WSACleanup(void)" (?WSACleanup@@$$J10YGHXZ) referenced in function "int __cdecl main(int,char * * const)" (?main@@$$HYAHHQAPAD@Z)
1>winsock.obj : error LNK2028: unresolved token (0A00034A) "extern "C" int __stdcall WSAStartup(unsigned short,struct WSAData *)" (?WSAStartup@@$$J18YGHGPAUWSAData@@@Z) referenced in function "int __cdecl main(int,char * * const)" (?main@@$$HYAHHQAPAD@Z)
1>winsock.obj : error LNK2028: unresolved token (0A00034B) "extern "C" int __stdcall closesocket(unsigned int)" (?closesocket@@$$J14YGHI@Z) referenced in function __catch$?RequestHeaders@@$$FYA_NPBD@Z$0
1>winsock.obj : error LNK2028: unresolved token (0A00034D) "extern "C" int __stdcall recv(unsigned int,char *,int,int)" (?recv@@$$J216YGHIPADHH@Z) referenced in function "bool __cdecl RequestHeaders(char const *)" (?RequestHeaders@@$$FYA_NPBD@Z)
1>winsock.obj : error LNK2028: unresolved token (0A00034E) "extern "C" int __stdcall send(unsigned int,char const *,int,int)" (?send@@$$J216YGHIPBDHH@Z) referenced in function "bool __cdecl RequestHeaders(char const *)" (?RequestHeaders@@$$FYA_NPBD@Z)
1>winsock.obj : error LNK2028: unresolved token (0A00034F) "extern "C" int __stdcall connect(unsigned int,struct sockaddr const *,int)" (?connect@@$$J212YGHIPBUsockaddr@@H@Z) referenced in function "bool __cdecl RequestHeaders(char const *)" (?RequestHeaders@@$$FYA_NPBD@Z)
1>winsock.obj : error LNK2028: unresolved token (0A000350) "extern "C" char * __stdcall inet_ntoa(struct in_addr)" (?inet_ntoa@@$$J14YGPADUin_addr@@@Z) referenced in function "bool __cdecl RequestHeaders(char const *)" (?RequestHeaders@@$$FYA_NPBD@Z)
1>winsock.obj : error LNK2028: unresolved token (0A000351) "extern "C" unsigned int __stdcall socket(int,int,int)" (?socket@@$$J212YGIHHH@Z) referenced in function "bool __cdecl RequestHeaders(char const *)" (?RequestHeaders@@$$FYA_NPBD@Z)
1>winsock.obj : error LNK2028: unresolved token (0A000353) "extern "C" unsigned short __stdcall htons(unsigned short)" (?htons@@$$J14YGGG@Z) referenced in function "void __cdecl FillSockAddr(struct sockaddr_in *,char const *,int)" (?FillSockAddr@@$$FYAXPAUsockaddr_in@@PBDH@Z)
1>winsock.obj : error LNK2028: unresolved token (0A000354) "extern "C" struct hostent * __stdcall gethostbyname(char const *)" (?gethostbyname@@$$J14YGPAUhostent@@PBD@Z) referenced in function "unsigned long __cdecl FindHostIP(char const *)" (?FindHostIP@@$$FYAKPBD@Z)
1>winsock.obj : error LNK2019: unresolved external symbol "extern "C" struct hostent * __stdcall gethostbyname(char const *)" (?gethostbyname@@$$J14YGPAUhostent@@PBD@Z) referenced in function "unsigned long __cdecl FindHostIP(char const *)" (?FindHostIP@@$$FYAKPBD@Z)
1>winsock.obj : error LNK2019: unresolved external symbol "extern "C" unsigned short __stdcall htons(unsigned short)" (?htons@@$$J14YGGG@Z) referenced in function "void __cdecl FillSockAddr(struct sockaddr_in *,char const *,int)" (?FillSockAddr@@$$FYAXPAUsockaddr_in@@PBDH@Z)
1>winsock.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall closesocket(unsigned int)" (?closesocket@@$$J14YGHI@Z) referenced in function __catch$?RequestHeaders@@$$FYA_NPBD@Z$0
1>winsock.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall recv(unsigned int,char *,int,int)" (?recv@@$$J216YGHIPADHH@Z) referenced in function "bool __cdecl RequestHeaders(char const *)" (?RequestHeaders@@$$FYA_NPBD@Z)
1>winsock.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall send(unsigned int,char const *,int,int)" (?send@@$$J216YGHIPBDHH@Z) referenced in function "bool __cdecl RequestHeaders(char const *)" (?RequestHeaders@@$$FYA_NPBD@Z)
1>winsock.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall connect(unsigned int,struct sockaddr const *,int)" (?connect@@$$J212YGHIPBUsockaddr@@H@Z) referenced in function "bool __cdecl RequestHeaders(char const *)" (?RequestHeaders@@$$FYA_NPBD@Z)
1>winsock.obj : error LNK2019: unresolved external symbol "extern "C" char * __stdcall inet_ntoa(struct in_addr)" (?inet_ntoa@@$$J14YGPADUin_addr@@@Z) referenced in function "bool __cdecl RequestHeaders(char const *)" (?RequestHeaders@@$$FYA_NPBD@Z)
1>winsock.obj : error LNK2019: unresolved external symbol "extern "C" unsigned int __stdcall socket(int,int,int)" (?socket@@$$J212YGIHHH@Z) referenced in function "bool __cdecl RequestHeaders(char const *)" (?RequestHeaders@@$$FYA_NPBD@Z)
1>winsock.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall WSACleanup(void)" (?WSACleanup@@$$J10YGHXZ) referenced in function "int __cdecl main(int,char * * const)" (?main@@$$HYAHHQAPAD@Z)
1>winsock.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall WSAStartup(unsigned short,struct WSAData *)" (?WSAStartup@@$$J18YGHGPAUWSAData@@@Z) referenced in function "int __cdecl main(int,char * * const)" (?main@@$$HYAHHQAPAD@Z)
1>C:\Users\Main\documents\visual studio 2010\Projects\winsock\Debug\winsock.exe : fatal error LNK1120: 20 unresolved externals
Last edited on
Those are all linker errors. Looks like you are not linking to the socket library.

Whatever tutorial you're using should mention how to add the lib to your project's linker settings.
Alright, I'll try that.

Thanks!
You need to link with ws2_32.lib
Topic archived. No new replies allowed.