Connecting to an IRC server, getting Ping timeout without receiving PING message

584 views Asked by At

I'm trying my hands at network programming for the first time, implementing a small IRC bot using the SFML network functionality.

The connection gets established, but from there on I can't do much else. Trying to receive any data from the server yields nothing, until I get the "Ping timeout" message after a few seconds.

Removing all or some of the receive() calls in the loginOnIRC function doesn't do any good.

Trying to connect via telnet with the exact same messages works. Here I get a PING message right after sending my NICK message.

Am I missing something?

My code is as follows

#include <iostream>
#include <string>

#include <SFML/Network.hpp>

#define ARRAY_LEN(x) (sizeof(x)/sizeof(*x))

void receive(sf::TcpSocket* sck)
{
    char rcvData[100];
    memset(rcvData, 0, ARRAY_LEN(rcvData));
    std::size_t received;
    if (sck->receive(rcvData, ARRAY_LEN(rcvData), received) != sf::Socket::Done)
    {
        std::cout << "oops" << std::endl;
    }
    std::cout << "Received " << received << " bytes" << std::endl;
    std::cout << rcvData << std::endl;
}

int establishConnection(sf::TcpSocket* sck)
{
    sf::Socket::Status status = sck->connect("irc.euirc.net", 6667, sf::seconds(5.0f));
    if (status != sf::Socket::Done)
    {
        std::cout << "Error on connect!" << std::endl;
        return 1;
    }
    std::cout << "Connect was successful!" << std::endl;
    return 0;
}

int loginOnIRC(sf::TcpSocket* sck)
{
    receive(sck); // We get a Ping timeout here

    std::string data{ "NICK NimBot" };
    if(sck->send(data.c_str(), data.length()) != sf::Socket::Done)
    {
        std::cout << "Error on sending " << data << std::endl;
        return 1;
    }

    receive(sck);

    data = "USER NimBot * * :Nimelrians Bot";
    if (sck->send(data.c_str(), data.length()) != sf::Socket::Done)
    {
        std::cout << "Error on sending " << data << std::endl;
        return 1;
    }

    receive(sck);

    data = "JOIN #nimbottest";
    if (sck->send(data.c_str(), data.length()) != sf::Socket::Done)
    {
        std::cout << "Error on sending " << data << std::endl;
        return 1;
    }
    return 0;
}

int main()
{
    sf::TcpSocket sck{};
    establishConnection(&sck); // works
    loginOnIRC(&sck);
    while(true)
    {
        char data[100];
        memset(data, 0, ARRAY_LEN(data));
        std::size_t received;
        sf::Socket::Status rcvStatus = sck.receive(data, ARRAY_LEN(data), received);
        if (rcvStatus != sf::Socket::Done)
        {
            std::cout << "oops" << std::endl;
            if (rcvStatus == sf::Socket::Disconnected)
            {
                break;
            }
        }

        std::cout << "Received " << received << " bytes" << std::endl;
        std::cout << data << std::endl;
    }

    return 0;
}
0

There are 0 answers