Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************************************************************
- * Connection.cc
- *
- * Fri Mar 7 16:13:20 2008
- * Copyright 2008 Cain
- * Darkdemun@gmail.com
- ****************************************************************************/
- #include "Connection.h"
- #include <time.h>
- #ifndef CONNECT
- #define CONNECT 1
- Connection::Connection()
- {
- };
- Connection::Connection(Serv server)
- {
- cout << "tryin..." << endl;
- Connect(server);
- };
- Connection::~Connection()
- {
- };
- int Connection::Connect(Serv server) // write errors to a file
- {
- sockfd = socket(PF_INET, SOCK_STREAM, 0);
- cout << "trying......." << endl;
- he = gethostbyname(server.GetServer().c_str());
- dest_addr.sin_family = AF_INET;
- dest_addr.sin_port = htons(server.GetPort());
- dest_addr.sin_addr.s_addr = ((struct in_addr *) he->h_addr)->s_addr;
- memset(dest_addr.sin_zero, '\0', sizeof dest_addr.sin_zero);
- if(connect(sockfd, (struct sockaddr *)&dest_addr, sizeof dest_addr) == -1)
- {
- perror("socket"); //write errors to a file
- return -1;
- }
- string init = "PASS "+server.GetPass()+"\r\nSERVER " +server.GetSName()+ " 2 Services by Cain_";
- cout << init << endl;
- SendStr(init);
- connected = true;
- return 0;
- };
- int Connection::SendStr(string msg)
- {
- msg = msg+"\r\n";
- char *buf = (char*) msg.c_str();
- int total = 0; // how many bytes we've sent
- int bytesleft = strlen(buf); // how many we have left to send
- int n;
- while(total < strlen(buf))
- {
- n = send(sockfd, buf+total, bytesleft, 0);
- if (n == -1) { break; }
- total += n;
- bytesleft -= n;
- }
- return 0;
- };
- int Connection::RecvStr(string * buffer)
- {
- char* buf = (char*) malloc(1024);
- char get[2];
- buf[0] = '\0';
- int bytesread = 0;
- while(bytesread = recv(sockfd,get,1,0))
- {
- if(get[0] == '\n')
- {
- *buffer = string(buf);
- return 1;
- }
- else
- {
- get[1] = '\0';
- strcat(buf,get);
- }
- }
- return 0;
- };
- bool Connection::IsConnected() const
- {
- return connected;
- };
- string Connection::GetTime() const
- {
- time_t rawtime;
- time(&rawtime);
- char *time;
- sprintf(time, "%d", rawtime);
- string ttime = string(time);
- return ttime;
- };
- #endif
Add Comment
Please, Sign In to add comment