/* Sockets Class v 0.1b by: Lovepump (aka Bob K), March 2004 http://home.cogeco.ca/~lovepump Thi is not to be used in any prohibited manner. Feel free to modify it or improve it. Kindly send all changes to lovepump@beer.com! Thanks Lovepump (Bob) */ #include #include #include #include #include #include #include"/home/bkrouse/ss/sockets.h" //#define TEST //Comment or un-comment this to include a main() section below. Sockets::Sockets () { sport = -1; saddress = "N/A"; shandle = -1; } Sockets::~Sockets () {} void Sockets::Show() { std::cout << "\nSocket address: " << saddress << "\n"; std::cout << "Socket port: " << sport << "\n"; std::cout << "Socket handle: " << shandle << "\n"; std::cout << "\n"; } bool Sockets::Init (std::string addr, int prt, std::string socktype) { shandle = -1; saddress = addr; sport = prt; /* if(socktype == "INET") { Sock.sin_family = AF_INET; } else if (socketype == "RAW") { Sock.sin_family = AF_RAW; } else { std::cout << "Init: invalid socket type\n"; return false; } */ Sock.sin_port = htons(sport); Sock.sin_addr.s_addr = inet_addr(saddress.c_str()); memset(&(Sock.sin_zero),'/0',8); shandle = socket(AF_INET, SOCK_STREAM, 0); if (shandle == -1) return false; return true; } bool Sockets::Bind (int sock_num) { // To be implemented as part of Server Side v0.2b } bool Sockets::Connect() { int i; if (shandle == -1) { std::cout << "Connect: Socket handle not intialized. Try 'Init'\n"; return false; } i = connect(shandle, (sockaddr*)&Sock, sizeof(Sock)); if (i == -1) { return false; } return true; } bool Sockets::Send(std::string buf) { //Straight forward stuff. Take the string and send it. retval = send(shandle, buf.c_str(), buf.size(), 0); if(retval != buf.length()) return false; // Send false if fail or partial. return true; } bool Sockets::SendFile(std::string filename) { long fsize; std::string buffer; std::fstream sfile; sfile.open(filename.c_str(), std::ios::in|std::ios::binary); // Open the file if(!sfile.is_open()) { std::cout << "Unable to open file: " << filename << "\n"; // Fail if unable, i.e. does not exist return false; } sfile.seekg(0, std::ios::end); // Goto end of file fsize = sfile.tellg(); // Get file size sfile.seekg(0, std::ios::beg); // Get back to start char* buf; buf = new char[fsize+1]; sfile.read(buf, fsize); // Should have whole file in buf now sfile.close(); buffer = *buf; // Got the file in a String, now Send it... Send(buffer); } bool Sockets::Recv(std::string& rec) { char buf[RECBUFFER]; // RECBUFFER set in the header file sockets.h int ok; ok=recv(shandle, buf, RECBUFFER, 0); if(ok != -1) // -1 on error { rec = buf; return true; } return false; } bool Sockets::Close(int shand) {} Sockets& Sockets::operator << (std::string buf) { // Just a convenient send. Note that errors are not returned from Send() Send(buf); // via the << operator! } //Any test code can go here. To include this "main()" uncomment the DEFINE TEST statement at the beginning #ifdef TEST int main() { std::string addr; int prt; std::cout << "\nAddress: "; std::cin >> addr; std::cout << "\nPort: "; std::cin >> prt; std::cout << "\n"; Sockets S; std::string sen = "GET //index"; if(S.Init(addr, prt) == false) { std::cout << "Failed to get socket handle."; exit(1); } if(S.Connect() == false) { std::cout << "Failed to connect.\n"; exit(1); } std::string got; got.clear(); if(S.Recv(got)) std::cout << got.c_str() << "\n"; } #endif