开发者

Socket operation on nonsocket

开发者 https://www.devze.com 2023-01-26 07:40 出处:网络
I\'m trying to receive data on a socket, and it keeps giving me this error, it\'s an irc bot, my code:

I'm trying to receive data on a socket, and it keeps giving me this error, it's an irc bot, my code:

#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;
}


quick observation, it's not a good idea to reuse _tmp like you have done. (create a _tmp[512] and snprintf your values into it)


1.- In the function recv_socket you are going to receive only 4 bytes (or maybe 8 bytes if you are compiling a 64bits code), because you are using sizeof and not the size of the buffer. 2.- What error are you getting ?.

0

精彩评论

暂无评论...
验证码 换一张
取 消