Valhalla Legends Forums Archive | C/C++ Programming | Strange Winsock2 Errors

AuthorMessageTime
Yegg
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.

[code]#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;
}[/code]

[code]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[/code]

Any help is appreciated.
August 8, 2005, 3:12 PM
Quarantine
You need to include the winsock library in your project.
August 8, 2005, 4:32 PM
UserLoser.
The following doesn't work and will always return bad:
[code]
inet_addr("asia.battle.net");
[/code]

See msdn, 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."
August 8, 2005, 4:46 PM
Myndfyr
To resolve an internet URL, you want gethostbyname.
August 8, 2005, 5:41 PM
warz
You may also need to do the following two things before you can use your socket.

[code]
WSADATA SocketData;
WSAStartup(MAKEWORD( 2, 2 ), &SocketData);
[/code]
August 8, 2005, 7:13 PM
Yegg
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.
August 8, 2005, 7:40 PM
Quarantine
No need, perhaps someone can learn from this.

Good luck.
August 8, 2005, 8:14 PM

Search