Home > Blockchain >  Why am I not receiving messages from the server?
Why am I not receiving messages from the server?

Time:11-08

I am trying to connect to twitch following the official docs: https://dev.twitch.tv/docs But I cannot connect, every function returns success but I still do not receive any messages inside of the while(1) loop.

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>

BOOL WriteServerMessage(SOCKET s, const char *msg)
{
    printf("Sending \"%s\"...", msg);
    INT ret = send(s, msg, strlen(msg), 0);
    if(ret < 0)
    {
        printf(" error %lu\n", GetLastError());
        return 0;
    }
    printf(" success!\n");
    return 1;
}

int main(int argc, char **argv)
{
    WSADATA wsaData;
    INT iRetval;

    struct addrinfo *result = NULL;
    struct addrinfo hints;

    struct sockaddr_in *sockaddr_ipv4;

    iRetval = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if(iRetval != 0)
    {
        printf("WSAStartup failed: %d\n", iRetval);
        return 1;
    }

    ZeroMemory(&hints, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    iRetval = getaddrinfo(argv[1], argv[2], &hints, &result);
    if(iRetval != 0)
    {
        printf("getaddrinfo failed with error: %d\n", iRetval);
        WSACleanup();
        return 1;
    }
    sockaddr_ipv4 = (struct sockaddr_in*) result->ai_addr;

    SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
    iRetval = connect(sock, (struct sockaddr*) sockaddr_ipv4, sizeof(*sockaddr_ipv4));
    if(iRetval < 0)
    {
        printf("Bind failed with %u\n", WSAGetLastError());
        printf("Server port: %d\n", sockaddr_ipv4->sin_port);
    }
    WriteServerMessage(sock, "PASS oauth:hbptkiz0ot187euawbnnkvghdhdgk3");
    WriteServerMessage(sock, "NICK kanalmanagerbot");
    WriteServerMessage(sock, "JOIN #h0llylp");
    WriteServerMessage(sock, "PRIVMSG #h0llylp :Hello there");
    char buf[1024];
    while(1)
    {
        if(recv(sock, buf, sizeof(buf), 0) > 0)
        {
            printf("Received: %s\n", buf);
            if(!strcmp(buf, "PING :tmi.twitch.tv"))
                WriteServerMessage(sock, "PONG :tmi.twitch.tv");
        }
        Sleep(10);
    }
    WriteServerMessage(sock, "PART #h0llylp");
    WriteServerMessage(sock, "QUIT");

    closesocket(sock);

    freeaddrinfo(result);
    WSACleanup();
    return 0;
}

I am calling the .exe with these flags: "irc.chat.twitch.tv" 6667

I am using JOIN to join a random chat, the oauth token was generated here: https://twitchapps.com/tmi/#access_token

I want to be able to join a chat and use all the IRC functions like NAMES or PRIVMSG

What am I doing wrong?

CodePudding user response:

The Twitch IRC guide says they "generally follow RFC1459".

That document in turn says

IRC messages are always lines of characters terminated with a CR-LF (Carriage Return - Line Feed) pair, and these messages shall not exceed 512 characters in length, counting all characters including the trailing CR-LF.

The message strings you send are not terminated by CR-LF, so the server does nothing with them until you send CR-LF.

You'll need to make sure your messages are correctly terminated according to the spec.

Your socket-reading code will also need to accommodate this - you can't just read a number of bytes and hope they're exactly a single message.

  • Related