Hello
I have a problem with this code. I'm trying to connect to local dns server in order to get the ip of the hostname( that user would enter). For now, i'm just trying to connect to the local dns server, and this code gives me following 8 errors..

client.obj : error LNK2001: unresolved external symbol __imp__closesocket@4
client.obj : error LNK2001: unresolved external symbol __imp__connect@12
client.obj : error LNK2001: unresolved external symbol __imp__htons@4
client.obj : error LNK2001: unresolved external symbol __imp__inet_addr@4
client.obj : error LNK2001: unresolved external symbol __imp__socket@12
client.obj : error LNK2001: unresolved external symbol __imp__WSACleanup@0
client.obj : error LNK2001: unresolved external symbol __imp__WSAStartup@8
Debug/client.exe : fatal error LNK1120: 7 unresolved externals
Error executing link.exe.

The code goes here

#define WIN32_LEAN_AND_MEAN
#pragma comment(lib, "wininet.lib")
#include <winsock2.h>
#include <windows.h>
#include <iostream.h>
#include <conio.h>
#include <string>

using namespace std;

void main()
{
	WSADATA wsadat;
	WORD rVersion;
	rVersion = MAKEWORD(2,0);
	
	if(WSAStartup(rVersion, &wsadat) != NO_ERROR)
	{
		cout<<"WSA initialization failed.\n";
		WSACleanup();
		return;
	}
	
	SOCKET client;
	client = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if(client == INVALID_SOCKET)
	{
		cout<<"Error creating socket.\n";
		WSACleanup();
		return;
	}

	char *ip= "208.67.222.222";
	cout<<ip;
	SOCKADDR_IN server;
	server.sin_family= AF_INET;
	server.sin_addr.s_addr= inet_addr(ip);
	server.sin_port= htons(53);

	if(connect(client, (SOCKADDR*) &server, sizeof(server)) == SOCKET_ERROR)
	{
		cout<<"Connection cannot be established. \n";
		WSACleanup();
		return;
	}

	
	closesocket(client);
	WSACleanup();
	
	return;
}

I don't know what to do... Kindly help me out, I need to submit my assignment by Friday......
Regards

Recommended Answers

All 6 Replies

You're not linking to the correct library. I'm not 100% sure, but adding: #pragma comment(lib, "wsock32.lib") should do the trick

You're not linking to the correct library. I'm not 100% sure, but adding: #pragma comment(lib, "wsock32.lib") should do the trick

Thanks for your reply. Well I added your mentioned library and it gives me this poppycock...

Loaded 'ntdll.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\wsock32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\ws2_32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\msvcrt.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\ws2help.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\advapi32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\rpcrt4.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\shimeng.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\mswsock.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\hnetcfg.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\gdi32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\user32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\imm32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\lpk.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\usp10.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\wshtcpip.dll', no matching symbolic information found.
The thread 0x850 has exited with code 0 (0x0).
The program 'C:\Documents and Settings\Yasir\Desktop\Debug\client.exe' has exited with code 0 (0x0)

Now what??

What compiler are you using?

What compiler are you using?

I don't know what's my compiler :S

I don't know what's my compiler :S

Turbo, Visual Studio, Gcc, Mingw?

Turbo, Visual Studio, Gcc, Mingw?

I'm using visual c++ 6, i guess i uses vc compiler :-|

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.