VC++ Winsock2 错误 10049。尝试构建 IRC 机器人

发布于 2024-12-02 16:28:21 字数 1222 浏览 0 评论 0原文

我正在尝试将 IRC 机器人从 Python 移植到 C++,但在使用 Winsock2 时遇到了一些问题。我对 C/C++ 中的套接字相当陌生,大部分代码都是从各种教程中拼凑而成的。我不断收到错误 10049 并且不知所措。任何帮助将不胜感激。谢谢!

port_ 设置为 6667,host_ 为“irc.rizon.net”

WSADATA wsaData;
int starterr = WSAStartup(MAKEWORD(2,2), &wsaData);
if (starterr != 0) {
    std::cout << "Error: " << WSAGetLastError() << " occurred!" << std::endl;
    WSACleanup();
    return 1;
}
std::cout << "WSAStartup Successful!" << std::endl;
socketfd_ = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (socketfd_ == INVALID_SOCKET) {
    std::cout << "Error: " << WSAGetLastError() << " occurred!" << std::endl;
    WSACleanup();
    return 1;
}
std::cout << "Socket Creation Successful!" << std::endl;

sockaddr_in anews;
anews.sin_port = htons(port_);
anews.sin_addr.s_addr = inet_addr(host_.c_str());
anews.sin_family = AF_INET;
if (connect(socketfd_,(sockaddr*)&anews, sizeof(anews)) == SOCKET_ERROR) {
    std::cout << "Error: " << WSAGetLastError() << " occurred!" << std::endl;
    WSACleanup();
    return 1;
}
std::cout << "Socket has connected successfuly!" << std::endl;
return 0;

I'm trying to port my IRC bot from Python to C++ and I'm running into some issues with Winsock2. I'm fairly new to sockets in C/C++ and most of this code was pieced together from various tutorials. I keep getting error 10049 and am at a loss. Any help would be appreciated greatly. Thanks!

port_ is set to 6667 and host_ is "irc.rizon.net"

WSADATA wsaData;
int starterr = WSAStartup(MAKEWORD(2,2), &wsaData);
if (starterr != 0) {
    std::cout << "Error: " << WSAGetLastError() << " occurred!" << std::endl;
    WSACleanup();
    return 1;
}
std::cout << "WSAStartup Successful!" << std::endl;
socketfd_ = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (socketfd_ == INVALID_SOCKET) {
    std::cout << "Error: " << WSAGetLastError() << " occurred!" << std::endl;
    WSACleanup();
    return 1;
}
std::cout << "Socket Creation Successful!" << std::endl;

sockaddr_in anews;
anews.sin_port = htons(port_);
anews.sin_addr.s_addr = inet_addr(host_.c_str());
anews.sin_family = AF_INET;
if (connect(socketfd_,(sockaddr*)&anews, sizeof(anews)) == SOCKET_ERROR) {
    std::cout << "Error: " << WSAGetLastError() << " occurred!" << std::endl;
    WSACleanup();
    return 1;
}
std::cout << "Socket has connected successfuly!" << std::endl;
return 0;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愁杀 2024-12-09 16:28:21

inet_addr() 采用“xxxx”形式的点分 IP 地址,您将向其传递主机名。

您可以使用 gethostbyname():

hostent* host;
char* ip;

...

// Get the local host information
host= gethostbyname(host_.c_str());
ip= inet_ntoa(*(struct in_addr *)*host->h_addr_list);

sockaddr_in anews;
anews.sin_port = htons(port_);
anews.sin_addr.s_addr = inet_addr(ip);
anews.sin_family = AF_INET;

...

或者更简单的方法是使用 getaddrinfo():

struct addrinfo *ai;

if(getaddrinfo(host_.c_str(), "6667", NULL, &ai) != 0)
    return 1;

socketfd_ = socket(ai->ai_family, SOCK_STREAM, 0);
if (socketfd_ == INVALID_SOCKET) {
    freeaddrinfo(ai);
    return 1
}

if (connect(socketfd_, ai->ai_addr, (int)ai->ai_addrlen) == SOCKET_ERROR) {
    closesocket(socketfd_);
    freeaddrinfo(ai);
    return 1;
}

...

inet_addr() takes a dotted IP address of the form "x.x.x.x" you are passing it the host name.

You can use gethostbyname():

hostent* host;
char* ip;

...

// Get the local host information
host= gethostbyname(host_.c_str());
ip= inet_ntoa(*(struct in_addr *)*host->h_addr_list);

sockaddr_in anews;
anews.sin_port = htons(port_);
anews.sin_addr.s_addr = inet_addr(ip);
anews.sin_family = AF_INET;

...

Or an easier route would be to use getaddrinfo():

struct addrinfo *ai;

if(getaddrinfo(host_.c_str(), "6667", NULL, &ai) != 0)
    return 1;

socketfd_ = socket(ai->ai_family, SOCK_STREAM, 0);
if (socketfd_ == INVALID_SOCKET) {
    freeaddrinfo(ai);
    return 1
}

if (connect(socketfd_, ai->ai_addr, (int)ai->ai_addrlen) == SOCKET_ERROR) {
    closesocket(socketfd_);
    freeaddrinfo(ai);
    return 1;
}

...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文