【问题标题】:c++ irc server doesnt send pingc ++ irc​​服务器不发送ping
【发布时间】:2019-07-18 01:49:05
【问题描述】:

我试图用 c++ 制作一个简单的 irc 客户端。我正在发送 PASS、NICK 和 USER 消息,但服务器没有向我发送 PING。注册不了。。。

这是代码:

#include <iostream>
#include <string>
#include <WS2tcpip.h>

#pragma comment(lib,"ws2_32.lib")


using namespace std;
string ipadress = "91.217.189.58";
int port = 6667;
WSADATA ws_data;
SOCKET Skt;
int main()
{
    int ws_result = WSAStartup(MAKEWORD(2, 2), &ws_data);
    if (ws_result != 0)
        cout << "socket cannot be initialized\n";
    else
        cout << "Soket initialized!\n";

    Skt = socket(AF_INET, SOCK_STREAM, 0);

    if (Skt == INVALID_SOCKET)
        cout << "socket not created\n";
    else
        cout << "Socket created!\n";

    sockaddr_in hint;
    hint.sin_family = AF_INET;
    hint.sin_port = htons(6667);
    inet_pton(AF_INET, ipadress.c_str(), &hint.sin_addr);

    int connection_result = connect(Skt, (sockaddr*)&hint, sizeof(hint));

    if (connection_result == SOCKET_ERROR)
        cout << "Socket could not connect\n";
    else
        cout << "Socket Connected!\n";
    string channel = "JOIN #dikes\r\n";
        string Pass = "PASS PASSRE";
        string user = "USER guest 0 * :IRCbot\r\n";
        string nick = "NICK botzzz\r\n";

        char buffer[4096];//buffer to recieve messages from irc server

        send(Skt, Pass.c_str(), Pass.size(), 0);

        send(Skt, nick.c_str(), nick.size() , 0);

        send(Skt, user.c_str(), user.size(), 0);

        while (true)
        {
            string Pong = "PONG";

            ZeroMemory(buffer, 4096);
            int bytes_recieved = recv(Skt, buffer, 4096, 0);
            string msg = string(buffer, 0, bytes_recieved);
            cout << msg;
            if (msg == "PING")
            {
                send(Skt, Pong.c_str(), Pong.size() + 1, 0);
            }
            else if (msg == "001")
            {
                send(Skt, channel.c_str(), channel.size(), 0);

            }
        }

这是这段代码的输出:

Soket initialized!
Socket created!
Socket Connected!
:irc.portlane.se 020 * :Please wait while we process your connection.
ERROR :Closing Link: [unknown@176.43.204.170] (Ping timeout)

【问题讨论】:

    标签: c++ sockets protocols irc


    【解决方案1】:
    1. 您的支票不考虑\r\n

    2. 您的支票不考虑the nick parameter on the PING command

    3. 您的PASS 命令没有被\r\n 终止

    4. 您的PONG 响应没有被\r\n 终止

    5. 您假设“接收到的数据”和“行”是一对一的映射。这不能保证。很有可能(甚至可能)一次调用recv 会用从许多 个完整命令/消息的数据填充缓冲区,然后可能是不完整的留言!

      TCP/IP 不知道 IRC 协议;它不关心它的“命令”概念,也不会将数据包分解成那些逐项列出的部分。你必须这样做。 TCP/IP 只是向你传输字节

      您需要在收到字节时将它们添加到辅助缓冲区,然后迭代地解析该缓冲区以提取任何可用的完整行。 (正确执行此操作,它也会处理 #1)

    (但是,我仍然希望在您的输出中看到 ping 请求,因此肯定还有其他问题。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-18
      • 2020-07-31
      • 2016-04-01
      • 1970-01-01
      相关资源
      最近更新 更多