Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: Yegg on August 08, 2005, 10:12 AM

Title: Strange Winsock2 Errors
Post by: Yegg on August 08, 2005, 10:12 AM
I've trying to get used to using Winsock2.h, however when I try my code out I receive strange errors. Here is the code, below it are the error messages.

#include <winsock2.h>
#include <stdio.h>

int main() {
SOCKET MainSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(MainSocket == INVALID_SOCKET) {
printf ("Error connecting socket!\n");
return 0;
}
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("asia.battle.net");
clientService.sin_port = htons(6112);
if(connect(MainSocket, (SOCKADDR*) &clientService, sizeof(clientService)) == SOCKET_ERROR) {
printf("Failed to connect!\n");
WSACleanup();
return 0;
}
printf("Connected to server.\n");
WSACleanup();
char buf[] = "\x03\x04CleanSlateBot\npasswordhere\n";
send(MainSocket, buf, sizeof(buf), 0);

return 0;
}


Text1.obj : error LNK2001: unresolved external symbol __imp__send@16
Text1.obj : error LNK2001: unresolved external symbol __imp__WSACleanup@0
Text1.obj : error LNK2001: unresolved external symbol __imp__connect@12
Text1.obj : error LNK2001: unresolved external symbol __imp__htons@4
Text1.obj : error LNK2001: unresolved external symbol __imp__inet_addr@4
Text1.obj : error LNK2001: unresolved external symbol __imp__socket@12
Debug/test.exe : fatal error LNK1120: 6 unresolved externals


Any help is appreciated.
Title: Re: Strange Winsock2 Errors
Post by: Warrior on August 08, 2005, 11:32 AM
You need to include the winsock library in your project.
Title: Re: Strange Winsock2 Errors
Post by: UserLoser. on August 08, 2005, 11:46 AM
The following doesn't work and will always return bad:

inet_addr("asia.battle.net");


See msdn (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/inet_addr_2.asp), it says "The inet_addr function converts a string containing an (Ipv4) Internet Protocol dotted address into a proper address for the IN_ADDR structure."
Title: Re: Strange Winsock2 Errors
Post by: MyndFyre on August 08, 2005, 12:41 PM
To resolve an internet URL, you want gethostbyname.
Title: Re: Strange Winsock2 Errors
Post by: warz on August 08, 2005, 02:13 PM
You may also need to do the following two things before you can use your socket.


WSADATA SocketData;
WSAStartup(MAKEWORD( 2, 2 ), &SocketData);
Title: Re: Strange Winsock2 Errors
Post by: Yegg on August 08, 2005, 02:40 PM
Thanks guys but I've already got the problems solved. I added ws2_32.lib to my project and I realized that inet_addr() will only accept the actual server ip. I thought I deleted my post but I guess Im not allowed to.
Title: Re: Strange Winsock2 Errors
Post by: Warrior on August 08, 2005, 03:14 PM
No need, perhaps someone can learn from this.

Good luck.