|
-
October 29th, 2008, 03:43 PM
#1
IRC Bot Connection Error
Basically I'm just trying to make a client program to connect to an IRC server atm but I can't figure out why this won't work. WSAGetLastError continously returns the error 10047 at the call to connect() when I enter "irc.gamesurge.net 6667" as the first and second parameter.
10047:
WSAEAFNOSUPPORT
0x273F
An address incompatible with the requested protocol was used.
Here's the code:
Code:
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <cstring>
#include <Mstcpip.h>
using namespace std;
WSADATA wsaData;
int main(int argc, char * argv[]) {
int wsaReturnVal = WSAStartup(MAKEWORD(1, 1), &wsaData);
if (wsaReturnVal) {
printf("Error number: %d", wsaReturnVal);
exit(1);
}
struct addrinfo hints, *res = NULL;
SOCKET sockfd;
memset(&hints, 0, sizeof(addrinfo));
memset(&res, 0, sizeof(addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
getaddrinfo(argv[1], argv[2], &hints, &res);
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sockfd == SOCKET_ERROR) {
printf("connect error %d", WSAGetLastError());
exit(1);
}
if (connect(sockfd, (struct sockaddr *)res, sizeof(addrinfo)) == SOCKET_ERROR) {
printf("connect error %d", WSAGetLastError());
exit(1);
}
printf("Connected to IRC server...");
closesocket(sockfd);
if (WSACleanup()) {
printf("cleanup %d", WSAGetLastError());
exit(1);
}
}
-
October 29th, 2008, 05:57 PM
#2
Re: IRC Bot Connection Error
Code:
int main(int argc, char * argv[]) {
// ...
struct addrinfo *res = NULL;
// ...
getaddrinfo(argv[1], argv[2], &hints, &res);
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
// ...
}
You are using res without ever allocating any memory for it.
Is there any reason why you declared it as a pointer? If you declare it a struct, then memory will be allocated for it automatically on the stack.
-
October 29th, 2008, 11:30 PM
#3
Re: IRC Bot Connection Error
Are you sure? I mean, from the examples I've seen online, it should automatically be allocated. And the reason it's a pointer is because getaddrinfo wants a pointer to a linked list, not just a struct. And I did declare it as a struct :/ A pointer to a struct. It's a linked list when used appropriately.
Last edited by pobri19; October 30th, 2008 at 05:20 AM.
-
October 30th, 2008, 10:02 AM
#4
Re: IRC Bot Connection Error
OK, it seems that getaddrinfo() allocates the memory for you. But your code must thereafter call freeaddrinfo(), as described by the documentation and the sample code at http://msdn.microsoft.com/en-us/libr...20(VS.85).aspx
As for your error on connect(), why did you cast the res variable to a (struct sockaddr *), when it already is one?
Try printing out the values of res to see if they are sensible. The actual values will probably tell you why the call is failing.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|