rendered paste body#include <iostream>#include <cstdlib>#include <sys/socket.h>#include <arpa/inet.h>#include <unistd.h>#include <netinet/in.h>#include <netdb.h>#define size 45#define buf_size 4096using namespace std;class client{ public: void prisijungti(char *c, int port); char * ns(char *addr); void die(char *msg); void quit(char *msg); void user(char *nick, char *usr, char *name); int parse(int msg); int rasti(char *c); int fill(char *c, char *d); private: int sock;};void client::prisijungti(char *c, int port){ struct sockaddr_in ircsrv; unsigned int srvlen; if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) die("failed to create socket"); memset(&ircsrv, 0, sizeof(ircsrv)); ircsrv.sin_family = AF_INET; ircsrv.sin_addr.s_addr = inet_addr(ns(c)); ircsrv.sin_port = htons(port); if (connect(sock, (struct sockaddr *) &ircsrv, sizeof(ircsrv)) < 0) die("failed connect to server");}char * client::ns(char *addr){ struct hostent *host; struct in_addr h_addr; if ((host = gethostbyname(addr)) == NULL) die("Can't get hostname"); h_addr.s_addr = *( (unsigned long *) host-> h_addr_list[0] ); return (inet_ntoa(h_addr));}void client::die(char *msg){ perror(msg); exit(1);}void client::quit(char *msg){ int len=0; char *sent = (char *) malloc(sizeof(char) * size); strcpy(sent,"QUIT :"); strcat(sent,msg); strcat(sent, "\n"); len = strlen(sent); if (send(sock, sent, len,0) != len) die("send error");}void client::user(char *nick, char *usr, char *name){ int len=0; char *cmd = (char *) malloc(sizeof(char) * size); char *cmd1 = (char *) malloc(sizeof(char) * size); strcpy(cmd, "NICK "); strcat(cmd,nick );strcat(cmd, " \r\n"); strcpy(cmd1, "USER "); strcat(cmd1,usr); strcat(cmd1," \"test\" \"test\" :"); strcat(cmd1,name); strcat(cmd1, "\r\n"); len = strlen(cmd); if (send(sock, cmd, len,0) != len) die("send error"); len = strlen(cmd1); if (send(sock, cmd1, len,0) != len) die("send error");}int client::fill(char *c, char *d){ int i=0; int j=0; while (c[i] != ':' && c[i] != '\0') i++; if (c[i] == ':') { i++; while (c[i] != '\0') { d[j] = c[i]; i++; j++; } d[j] = '\0'; return 1; } return 0;}int client::parse(int msg){ int bytes=0; int tmp=0; string buffer; string s, p; char tmpc[4096], sent[100]; int j=0, len=0; for (;;) { bytes = 0; if ((bytes = recv(sock, tmpc , 4095, 0)) < 1) die("failed recieve bytes form server"); if (bytes > 0) { tmpc[bytes] = 0; buffer += tmpc; if (msg == 1) cout << buffer; if (buffer.find("PING :") != string::npos) { s = buffer.substr(buffer.find(":"),buffer.find("\r")); p = p + "PONG " + s + "\r\n"; for (j=0; j < p.length() && j < sizeof(sent); j++) sent[j] = p[j]; len = strlen(sent); if (send(sock, sent, len,0) != len) die("send error"); for (j=0; j < sizeof(sent); j++) sent[j] = ' '; p=""; } } buffer= ""; for (j=0; j < 4096; j++) tmpc[j] = ' '; }}int client::rasti(char *c){ char * p = (char *) malloc(sizeof(char) * 4); int i=0; int j=0; int t=0; strcpy(p, "PING"); while (c[i] != '\0') { t = 0; while (c[i] == p[j]) { t++; j++; i++; } if (t== 4) { free(p); return t; } j = 0; i++; } return 0;}