为什么我收不到服务器的消息?
Why am I not receiving messages from the server?
我正在按照官方文档尝试连接到 twitch:https://dev.twitch.tv/docs
但是我无法连接,每个函数 returns 都成功,但我仍然没有在 while(1)
循环中收到任何消息。
#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;
}
我正在使用这些标志调用 .exe:“irc.chat.twitch.tv”6667
我正在使用 JOIN
加入随机聊天,oauth 令牌是在这里生成的:https://twitchapps.com/tmi/#access_token
我希望能够加入聊天并使用所有 IRC 功能,例如 NAMES
或 PRIVMSG
我做错了什么?
Twitch IRC guide says they "generally follow RFC1459".
那个文件依次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.
您发送的消息字符串不会被 CR-LF 终止,因此在您发送 CR-LF 之前服务器不会对它们做任何处理。
您需要确保您的消息根据规范正确终止。
您的套接字读取代码也需要适应这一点 - 您不能只读取多个字节并希望它们是一条消息。
我正在按照官方文档尝试连接到 twitch:https://dev.twitch.tv/docs
但是我无法连接,每个函数 returns 都成功,但我仍然没有在 while(1)
循环中收到任何消息。
#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;
}
我正在使用这些标志调用 .exe:“irc.chat.twitch.tv”6667
我正在使用 JOIN
加入随机聊天,oauth 令牌是在这里生成的:https://twitchapps.com/tmi/#access_token
我希望能够加入聊天并使用所有 IRC 功能,例如 NAMES
或 PRIVMSG
我做错了什么?
Twitch IRC guide says they "generally follow RFC1459".
那个文件依次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.
您发送的消息字符串不会被 CR-LF 终止,因此在您发送 CR-LF 之前服务器不会对它们做任何处理。
您需要确保您的消息根据规范正确终止。
您的套接字读取代码也需要适应这一点 - 您不能只读取多个字节并希望它们是一条消息。