我正在尝试接收套接字上的数据,它一直给我这个错误,它是一个irc bot,我的代码:
#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;
}
答案 0 :(得分:0)
答案 1 :(得分:0)
1.-在函数recv_socket中,您将只接收4个字节(如果您正在编译64位代码,则可能只接收8个字节),因为您使用的是sizeof而不是缓冲区的大小。 2.-你得到什么错误?。