gpt4 book ai didi

c - 非套接字上的套接字操作

转载 作者:太空宇宙 更新时间:2023-11-04 04:57:33 24 4
gpt4 key购买 nike

我试图在套接字上接收数据,但它一直给我这个错误,这是一个 irc 机器人,我的代码:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <time.h>

void remove_string(char * str, char c)
{
char *p1 = str;
while (*p1++) {
if(*p1 == c) {
char *p2 = p1;
while (*p2 && *p2 == c)
++p2;

if (*p2) {
*p1 = *p2;
*p2 = c;
} else {
*p1 = '\0';
break;
}
}
}
}

char *replace_string(char *str, char *orig, char *rep)
{
static char buffer[4096];
char *p;
if (!(p = strstr(str, orig)))
return str;

strncpy(buffer, str, p-str);
buffer[p-str] = '\0';
sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
return buffer;
}

int connect_socket(SOCKET *p, SOCKADDR_IN service)
{
if (connect(*p, (LPSOCKADDR)&service, sizeof(struct sockaddr)) == SOCKET_ERROR) {
fprintf(stderr, "Unable to connect, Error: %d\n", WSAGetLastError());
return -1;
}
return 0;
}

int send_socket(SOCKET *p, const char *buffer)
{
if (send(*p, buffer, strlen(buffer), 0) == SOCKET_ERROR) {
fprintf(stderr, "Unable to send data on the socket, Error: %d\n", WSAGetLastError());
return -1;
}
return 0;
}

int recv_socket(SOCKET *p, char *buffer)
{
if (recv(*p, buffer, sizeof(buffer), 0) == SOCKET_ERROR) {
fprintf(stderr, "Unable to receive data on the socket, Error: %d\n", WSAGetLastError());
return -1;
}
return 0;
}

int find_string(const char *str1, const char *str2)
{
int res = strstr(str1, str2) - str1;
return res < 0 ? -1 : res;
}

typedef struct IRC_DATA {
char *nick;
char *msg;
char *channel;
char *time;
} irc_data;

int main(int argc, char **argv)
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
return -1;

if (argc < 4) {
printf("Usage: %s <server> <port> <channel>", argv[0]);
return -1;
}
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET || sock == SOCKET_ERROR) {
fprintf(stderr, "Unable to initalize socket, Error: %d\n", WSAGetLastError());
return -1;
}
LPHOSTENT host = gethostbyname(argv[1]);
int port = atoi(argv[2]);
SOCKADDR_IN service;
service.sin_family = AF_INET;
service.sin_port = htons(port);
service.sin_addr = *((LPIN_ADDR)*host->h_addr_list);
int tries;
for (tries = 0; connect_socket(&sock, service) != 0; tries++) {
printf("Failing to connect.\n");
if (tries == 3) {
printf("Stopping reconnect...");
return -1;
}
}
printf("Connected\n");
char *_tmp = "USER CBot * * :CBot\r\n";
send_socket(&sock, _tmp);
_tmp = "NICK CBot\r\n";
send_socket(&sock, _tmp);
int joined = 0;
irc_data irc;
char buffer[512];
while (1) {
int p = recv_socket(&sock, buffer);
if (p == -1)
break;

char _time[50];
time_t rawtime;
struct tm* timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(_time, sizeof(_time), "[%H:%M:%S]", timeinfo);
strcpy(irc.time, _time);
if (strncmp(buffer, "PING", 4) == 0) {
printf("Received ping\n");
char pong[100];
if (sscanf(buffer, "PING :%s", pong) > 0) {
char buff[120];
sprintf(buff, "PONG :%s\r\n", pong);
send_socket(&sock, buff);
if (joined == 0) {
char join[120];
sprintf(join, "JOIN %s\r\n", argv[3]);
send_socket(&sock, join);
joined = 1;
}
}
} else if (strstr(buffer, "PRIVMSG") != NULL) {
remove_string(buffer, '\r\n');
int pos = find_string(buffer, " :");
int len = strlen(buffer)-pos;
char msg[len];
memcpy(msg, &buffer[pos], len);
irc.msg = msg;
printf("Msg: %s\n", irc.msg);
printf("%s %s\n", irc.time, buffer);
}
}
return 0;
}

最佳答案

快速观察,像您所做的那样重用 _tmp 并不是一个好主意。 (创建一个 _tmp[512] 并将您的值打印到其中)

关于c - 非套接字上的套接字操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4219581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com