Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: Socket.cpp
- * Author: cedric
- *
- * Created on March 24, 2012, 4:01 PM
- */
- #define DEBUG 0
- #include "Socket.h"
- using namespace std;
- Socket::Socket()
- {
- #if DEBUG == 1
- cout<< "Socket created" << endl;
- cout<< "//constructor called" << endl;
- #endif
- }
- Socket::Socket(int port)
- {
- this->port = port;
- }
- Socket::Socket(std::string ip)
- {
- this->ip = ip;
- }
- Socket::Socket(string ip, int port)
- {
- this->port = port;
- this->ip = ip;
- }
- Socket::~Socket(void)
- {
- #if DEBUG == 1
- cout<< "Socket destructed" << endl;
- cout<<"//Destructor called" << endl;
- #endif
- }
- void Socket::setIP(std::string ip)
- {
- this->ip = ip;
- }
- void Socket::setPort(int port)
- {
- this->port = port;
- }
- void Socket::setMessage(std::string message)
- {
- this->message = message;
- }
- string Socket::getMessage()
- {
- return recivedMessage;
- }
- int Socket::createClient(void)
- {
- if(this->port < 0) {
- cout<< "Fatal Error!\nNo Port.\nPlease use 'createClient(std::string, int)'" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::createClient(void)" << endl;
- exit(0);
- #endif
- return 0;
- }
- if(!(this->port < 9999)) {
- cout<< "Fatal Error!\nPort must be beteween 1 and 9999" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::createClient(void)" << endl;
- exit(0);
- #endif
- return 0;
- }
- #if DEBUG == 1
- cout<< "Port Valide" << endl;
- #endif
- socketfd = socket(AF_INET, SOCK_STREAM, 0);
- if(socketfd < 0) {
- cout<< "Error while opening Socket" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::createClient(void)" << endl;
- exit(0);
- #endif
- return 0;
- }
- #if DEFINE == 1
- cout<< "Socket succesfully created";
- #endif
- server = gethostbyname(this->ip.c_str());
- if(server == NULL) {
- cout<< "Error, no such host" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::createClient(void)" << endl;
- exit(0);
- #endif
- return 0;
- }
- #if DEFINE == 1
- cout<< "Host sucessfully found";
- #endif
- bcopy((char *)server->h_addr, (char *) &serv_addr.sin_addr.s_addr, server->h_length);
- serv_addr.sin_port = htons(this->port);
- connectfd = connect(socketfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
- if(connectfd < 0) {
- cout<< "Error while connecting" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::createClient(void)" << endl;
- exit(0);
- #endif
- return 0;
- }
- #if DEBUG == 1
- cout<< "Connecction succesfully created" << endl;
- cout<< "Client Socket sucessfully created." << endl;
- #endif
- return 1;
- }
- int Socket::createServer(void)
- {
- if(this->port < 0) {
- cout<< "Fatal Error!\nNo Port.\nPlease use 'createServer(int)'" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::createServer(void)" << endl;
- exit(0);
- #endif
- return 0;
- }
- if(!(this->port < 9999)) {
- cout<< "Fatal Error!\nPort must be beteween 1 and 9999" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::createServer(void)" << endl;
- exit(0);
- #endif
- return 0;
- }
- #if DEBUG == 1
- cout<< "Port Valide" << endl;
- #endif
- socketfd = socket(AF_INET, SOCK_STREAM, 0);
- if(socketfd < 0) {
- cout<< "Error while opening socket" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::createServer(void)" << endl;
- exit(0);
- #endif
- return 0;
- }
- #if DEBUG == 1
- cout<< "Opening socket successfull" << endl;
- #endif
- bzero((char *) &serv_addr, sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_addr.s_addr = INADDR_ANY;
- serv_addr.sin_port = htons(this->port);
- bindfd = bind(socketfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
- if(bindfd < 0) {
- cout<< "Error on binding" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::createServer(void)" << endl;
- exit(0);
- #endif
- return 0;
- }
- #if DEBUG == 1
- cout<< "Binding successfull" << endl;
- #endif
- listen(socketfd, 5);
- clilen = sizeof(cli_addr);
- newSocket = accept(socketfd, (struct sockaddr *) &cli_addr, &clilen);
- if(newSocket < 0) {
- cout<< "Error on accpet" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::createServer(void)" << endl;
- exit(0);
- #endif
- return 0;
- }
- #if DEBUG == 1
- cout<< "Appect succesfull" << endl;
- cout<< "Server Socket succesfully created" << endl;
- #endif
- return 1;
- }
- int Socket::createClient(std::string ip, int port)
- {
- this->ip = ip;
- this->port = port;
- if(!(this->port < 9999) || this->port < 0) {
- cout<< "Fatal Error!\nPort must be beteween 1 and 9999" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::createClient(std::string, int)" << endl;
- exit(0);
- #endif
- return 0;
- }
- #if DEBUG == 1
- cout<< "Port Valide" << endl;
- #endif
- socketfd = socket(AF_INET, SOCK_STREAM, 0);
- if(socketfd < 0) {
- cout<< "Error while opening Socket" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::createClient(std::string, int)" << endl;
- exit(0);
- #endif
- return 0;
- }
- #if DEFINE == 1
- cout<< "Socket succesfully created";
- #endif
- server = gethostbyname(this->ip.c_str());
- if(server == NULL) {
- cout<< "Error, no such host" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::createClient(std::string, int)" << endl;
- exit(0);
- #endif
- return 0;
- }
- #if DEFINE == 1
- cout<< "Host sucessfully found";
- #endif
- bcopy((char *)server->h_addr, (char *) &serv_addr.sin_addr.s_addr, server->h_length);
- serv_addr.sin_port = htons(this->port);
- connectfd = connect(socketfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
- if(connectfd < 0) {
- cout<< "Error while connecting" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::createClient(std::string, int)" << endl;
- exit(0);
- #endif
- return 0;
- }
- #if DEBUG == 1
- cout<< "Connecction succesfully created" << endl;
- cout<< "Client Socket sucessfully created." << endl;
- #endif
- return 1;
- }
- int Socket::createServer(int port)
- {
- this->port = port;
- if(!(this->port < 9999) || this->port < 0) {
- cout<< "Fatal Error!\nPort must be beteween 1 and 9999" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::createClient(int)" << endl;
- exit(0);
- #endif
- return 0;
- }
- #if DEBUG == 1
- cout<< "Port Valide" << endl;
- #endif
- socketfd = socket(AF_INET, SOCK_STREAM, 0);
- if(socketfd < 0) {
- cout<< "Error while opening socket" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::createClient(int)" << endl;
- exit(0);
- #endif
- return 0;
- }
- #if DEBUG == 1
- cout<< "Opening socket successfull" << endl;
- #endif
- bzero((char *) &serv_addr, sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_addr.s_addr = INADDR_ANY;
- serv_addr.sin_port = htons(this->port);
- bindfd = bind(socketfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
- if(bindfd < 0) {
- cout<< "Error on binding" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::createClient(int)" << endl;
- exit(0);
- #endif
- return 0;
- }
- #if DEBUG == 1
- cout<< "Binding successfull" << endl;
- #endif
- listen(socketfd, 5);
- clilen = sizeof(cli_addr);
- newSocket = accept(socketfd, (struct sockaddr *) &cli_addr, &clilen);
- if(newSocket < 0) {
- cout<< "Error on accpet" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::createClient(int)" << endl;
- exit(0);
- #endif
- return 0;
- }
- #if DEBUG == 1
- cout<< "Appect succesfully done" << endl;
- cout<< "Server Socket succesfully created" << endl;
- #endif
- return 1;
- }
- int Socket::writeMessage(void)
- {
- if(message == "0") {
- cout<< "Fatal Error\nNo message.\nPlease use 'writeMessage(std::string)'" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::writeMessage(void)" << endl;
- exit(0);
- #endif
- return 0;
- } else {
- writefd = write(socketfd, message.c_str(), strlen(message.c_str())); //write message
- if(writefd < 0) {
- cout<< "Error while sending" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::writeMessage(void)" << endl;
- exit(0);
- #endif
- return 0;
- }
- return 1;
- }
- }
- int Socket::writeMessage(std::string message)
- {
- this->message = message;
- writefd = write(socketfd, this->message.c_str(), strlen(this->message.c_str()));
- if(writefd < 0) {
- cout<< "Error while sending" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::writeMessage(std::string)" << endl;
- exit(0);
- #endif
- return 0;
- }
- return 1;
- }
- int Socket::readMessage(void)
- {
- readfd = read(readfd, buffer, 255);
- if(readfd < 0) {
- cout<< "Error while reading" << endl;
- #if DEBUG == 1
- cout<< "Error in Socket::readMessage(void)" << endl;
- exit(0);
- #endif
- return 0;
- }
- recivedMessage = buffer;
- return 1;
- }
Add Comment
Please, Sign In to add comment