Guest User

Untitled

a guest
Jul 16th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.51 KB | None | 0 0
  1. /*
  2.  * File:   Socket.cpp
  3.  * Author: cedric
  4.  *
  5.  * Created on March 24, 2012, 4:01 PM
  6.  */
  7.  
  8. #define DEBUG 0
  9.  
  10. #include "Socket.h"
  11.  
  12. using namespace std;
  13.  
  14. Socket::Socket()
  15. {
  16. #if DEBUG == 1
  17.     cout<< "Socket created" << endl;
  18.     cout<< "//constructor called" << endl;
  19. #endif
  20. }
  21.  
  22. Socket::Socket(int port)
  23. {
  24.     this->port = port;    
  25. }
  26.  
  27. Socket::Socket(std::string ip)
  28. {
  29.     this->ip = ip;
  30. }
  31.  
  32. Socket::Socket(string ip, int port)
  33. {
  34.     this->port = port;
  35.     this->ip = ip;
  36. }
  37.  
  38. Socket::~Socket(void)
  39. {
  40. #if DEBUG == 1
  41.     cout<< "Socket destructed" << endl;
  42.     cout<<"//Destructor called" << endl;
  43. #endif
  44. }
  45.  
  46. void Socket::setIP(std::string ip)
  47. {
  48.     this->ip = ip;
  49. }
  50.  
  51. void Socket::setPort(int port)
  52. {
  53.     this->port = port;
  54. }
  55.  
  56. void Socket::setMessage(std::string message)
  57. {
  58.     this->message = message;
  59. }
  60.  
  61. string Socket::getMessage()
  62. {
  63.     return recivedMessage;
  64. }
  65.  
  66. int Socket::createClient(void)
  67. {
  68.     if(this->port < 0) {
  69.         cout<< "Fatal Error!\nNo Port.\nPlease use 'createClient(std::string, int)'" << endl;
  70. #if DEBUG == 1
  71.         cout<< "Error in Socket::createClient(void)" << endl;
  72.         exit(0);
  73. #endif
  74.         return 0;
  75.     }
  76.    
  77.     if(!(this->port < 9999)) {
  78.         cout<< "Fatal Error!\nPort must be beteween 1 and 9999" << endl;
  79. #if DEBUG == 1
  80.         cout<< "Error in Socket::createClient(void)" << endl;
  81.         exit(0);
  82. #endif
  83.         return 0;
  84.     }
  85.    
  86. #if DEBUG == 1
  87.     cout<< "Port Valide" << endl;
  88. #endif
  89.    
  90.     socketfd = socket(AF_INET, SOCK_STREAM, 0);
  91.     if(socketfd < 0) {
  92.         cout<< "Error while opening Socket" << endl;
  93. #if DEBUG == 1
  94.         cout<< "Error in Socket::createClient(void)" << endl;
  95.         exit(0);
  96. #endif        
  97.         return 0;
  98.     }
  99. #if DEFINE == 1
  100.     cout<< "Socket succesfully created";
  101. #endif
  102.    
  103.     server = gethostbyname(this->ip.c_str());
  104.     if(server == NULL) {
  105.         cout<< "Error, no such host" << endl;
  106. #if DEBUG == 1
  107.         cout<< "Error in Socket::createClient(void)" << endl;
  108.         exit(0);
  109. #endif        
  110.         return 0;
  111.     }
  112. #if DEFINE == 1
  113.     cout<< "Host sucessfully found";
  114. #endif
  115.    
  116.     bcopy((char *)server->h_addr, (char *) &serv_addr.sin_addr.s_addr, server->h_length);
  117.    
  118.     serv_addr.sin_port = htons(this->port);
  119.     connectfd = connect(socketfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
  120.     if(connectfd < 0) {
  121.         cout<< "Error while connecting" << endl;
  122. #if DEBUG == 1
  123.         cout<< "Error in Socket::createClient(void)" << endl;
  124.         exit(0);
  125. #endif        
  126.         return 0;
  127.     }
  128.    
  129. #if DEBUG == 1
  130.     cout<< "Connecction succesfully created" << endl;
  131.     cout<< "Client Socket sucessfully created." << endl;
  132. #endif
  133.  
  134.     return 1;
  135. }
  136.  
  137. int Socket::createServer(void)
  138. {
  139.     if(this->port < 0) {
  140.         cout<< "Fatal Error!\nNo Port.\nPlease use 'createServer(int)'" << endl;
  141. #if DEBUG == 1
  142.         cout<< "Error in Socket::createServer(void)" << endl;
  143.         exit(0);
  144. #endif        
  145.         return 0;
  146.     }
  147.    
  148.     if(!(this->port < 9999)) {
  149.         cout<< "Fatal Error!\nPort must be beteween 1 and 9999" << endl;
  150. #if DEBUG == 1
  151.         cout<< "Error in Socket::createServer(void)" << endl;
  152.         exit(0);
  153. #endif        
  154.         return 0;
  155.     }
  156. #if DEBUG == 1
  157.     cout<< "Port Valide" << endl;
  158. #endif
  159.    
  160.     socketfd = socket(AF_INET, SOCK_STREAM, 0);
  161.     if(socketfd < 0) {
  162.         cout<< "Error while opening socket" << endl;
  163. #if DEBUG == 1
  164.         cout<< "Error in Socket::createServer(void)" << endl;
  165.         exit(0);
  166. #endif        
  167.         return 0;
  168.     }
  169. #if DEBUG == 1
  170.     cout<< "Opening socket successfull" << endl;
  171. #endif
  172.    
  173.     bzero((char *) &serv_addr, sizeof(serv_addr));
  174.    
  175.     serv_addr.sin_family = AF_INET;
  176.     serv_addr.sin_addr.s_addr = INADDR_ANY;
  177.     serv_addr.sin_port = htons(this->port);
  178.    
  179.     bindfd = bind(socketfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
  180.     if(bindfd < 0) {
  181.         cout<< "Error on binding" << endl;
  182. #if DEBUG == 1
  183.         cout<< "Error in Socket::createServer(void)" << endl;
  184.         exit(0);
  185. #endif        
  186.         return 0;
  187.     }
  188. #if DEBUG == 1
  189.     cout<< "Binding successfull" << endl;
  190. #endif
  191.    
  192.     listen(socketfd, 5);
  193.     clilen = sizeof(cli_addr);
  194.     newSocket = accept(socketfd, (struct sockaddr *) &cli_addr, &clilen);
  195.     if(newSocket < 0) {
  196.         cout<< "Error on accpet" << endl;
  197. #if DEBUG == 1
  198.         cout<< "Error in Socket::createServer(void)" << endl;
  199.         exit(0);
  200. #endif        
  201.         return 0;
  202.     }
  203. #if DEBUG == 1
  204.     cout<< "Appect succesfull" << endl;
  205.     cout<< "Server Socket succesfully created" << endl;
  206. #endif
  207.    
  208.     return 1;
  209. }
  210.  
  211. int Socket::createClient(std::string ip, int port)
  212. {
  213.     this->ip = ip;
  214.     this->port = port;
  215.    
  216.     if(!(this->port < 9999) || this->port < 0) {
  217.         cout<< "Fatal Error!\nPort must be beteween 1 and 9999" << endl;
  218. #if DEBUG == 1
  219.         cout<< "Error in Socket::createClient(std::string, int)" << endl;
  220.         exit(0);
  221. #endif        
  222.         return 0;
  223.     }
  224. #if DEBUG == 1
  225.     cout<< "Port Valide" << endl;
  226. #endif
  227.    
  228.     socketfd = socket(AF_INET, SOCK_STREAM, 0);
  229.     if(socketfd < 0) {
  230.         cout<< "Error while opening Socket" << endl;
  231. #if DEBUG == 1
  232.         cout<< "Error in Socket::createClient(std::string, int)" << endl;
  233.         exit(0);
  234. #endif        
  235.         return 0;
  236.     }
  237. #if DEFINE == 1
  238.     cout<< "Socket succesfully created";
  239. #endif
  240.    
  241.     server = gethostbyname(this->ip.c_str());
  242.     if(server == NULL) {
  243.         cout<< "Error, no such host" << endl;
  244. #if DEBUG == 1
  245.         cout<< "Error in Socket::createClient(std::string, int)" << endl;
  246.         exit(0);
  247. #endif        
  248.         return 0;
  249.     }
  250. #if DEFINE == 1
  251.     cout<< "Host sucessfully found";
  252. #endif
  253.    
  254.     bcopy((char *)server->h_addr, (char *) &serv_addr.sin_addr.s_addr, server->h_length);
  255.    
  256.     serv_addr.sin_port = htons(this->port);
  257.     connectfd = connect(socketfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
  258.     if(connectfd < 0) {
  259.         cout<< "Error while connecting" << endl;
  260. #if DEBUG == 1
  261.         cout<< "Error in Socket::createClient(std::string, int)" << endl;
  262.         exit(0);
  263. #endif        
  264.         return 0;
  265.     }
  266.    
  267. #if DEBUG == 1
  268.     cout<< "Connecction succesfully created" << endl;
  269.     cout<< "Client Socket sucessfully created." << endl;
  270. #endif
  271.  
  272.     return 1;
  273. }
  274.  
  275. int Socket::createServer(int port)
  276. {
  277.     this->port = port;
  278.    
  279.     if(!(this->port < 9999) || this->port < 0) {
  280.         cout<< "Fatal Error!\nPort must be beteween 1 and 9999" << endl;
  281. #if DEBUG == 1
  282.         cout<< "Error in Socket::createClient(int)" << endl;
  283.         exit(0);
  284. #endif        
  285.         return 0;
  286.     }
  287. #if DEBUG == 1
  288.     cout<< "Port Valide" << endl;
  289. #endif
  290.            
  291.     socketfd = socket(AF_INET, SOCK_STREAM, 0);
  292.     if(socketfd < 0) {
  293.         cout<< "Error while opening socket" << endl;
  294. #if DEBUG == 1
  295.         cout<< "Error in Socket::createClient(int)" << endl;
  296.         exit(0);
  297. #endif        
  298.         return 0;
  299.     }
  300. #if DEBUG == 1
  301.     cout<< "Opening socket successfull" << endl;
  302. #endif
  303.    
  304.     bzero((char *) &serv_addr, sizeof(serv_addr));
  305.    
  306.     serv_addr.sin_family = AF_INET;
  307.     serv_addr.sin_addr.s_addr = INADDR_ANY;
  308.     serv_addr.sin_port = htons(this->port);
  309.    
  310.     bindfd = bind(socketfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
  311.     if(bindfd < 0) {
  312.         cout<< "Error on binding" << endl;
  313. #if DEBUG == 1
  314.         cout<< "Error in Socket::createClient(int)" << endl;
  315.         exit(0);
  316. #endif        
  317.         return 0;
  318.     }
  319. #if DEBUG == 1
  320.     cout<< "Binding successfull" << endl;
  321. #endif
  322.    
  323.     listen(socketfd, 5);
  324.     clilen = sizeof(cli_addr);
  325.     newSocket = accept(socketfd, (struct sockaddr *) &cli_addr, &clilen);
  326.     if(newSocket < 0) {
  327.         cout<< "Error on accpet" << endl;
  328. #if DEBUG == 1
  329.         cout<< "Error in Socket::createClient(int)" << endl;
  330.         exit(0);
  331. #endif        
  332.         return 0;
  333.     }
  334. #if DEBUG == 1
  335.     cout<< "Appect succesfully done" << endl;
  336.     cout<< "Server Socket succesfully created" << endl;
  337. #endif
  338.    
  339.     return 1;
  340. }
  341.  
  342. int Socket::writeMessage(void)
  343. {
  344.     if(message == "0") {
  345.         cout<< "Fatal Error\nNo message.\nPlease use 'writeMessage(std::string)'" << endl;
  346. #if DEBUG == 1
  347.         cout<< "Error in Socket::writeMessage(void)" << endl;
  348.         exit(0);
  349. #endif        
  350.         return 0;
  351.     } else {
  352.         writefd = write(socketfd, message.c_str(), strlen(message.c_str())); //write message
  353.         if(writefd < 0) {
  354.             cout<< "Error while sending" << endl;
  355. #if DEBUG == 1
  356.         cout<< "Error in Socket::writeMessage(void)" << endl;
  357.         exit(0);
  358. #endif            
  359.             return 0;
  360.         }
  361.        
  362.         return 1;
  363.     }
  364. }
  365.  
  366. int Socket::writeMessage(std::string message)
  367. {
  368.     this->message = message;
  369.    
  370.     writefd = write(socketfd, this->message.c_str(), strlen(this->message.c_str()));
  371.     if(writefd < 0) {
  372.         cout<< "Error while sending" << endl;
  373. #if DEBUG == 1
  374.         cout<< "Error in Socket::writeMessage(std::string)" << endl;
  375.         exit(0);
  376. #endif        
  377.         return 0;
  378.     }
  379.    
  380.     return 1;
  381. }
  382.  
  383. int Socket::readMessage(void)
  384. {
  385.     readfd = read(readfd, buffer, 255);
  386.     if(readfd < 0) {
  387.         cout<< "Error while reading" << endl;
  388. #if DEBUG == 1
  389.         cout<< "Error in Socket::readMessage(void)" << endl;
  390.         exit(0);
  391. #endif        
  392.         return 0;
  393.     }
  394.     recivedMessage = buffer;
  395.    
  396.     return 1;
  397. }
Add Comment
Please, Sign In to add comment