Guest User

Untitled

a guest
Oct 17th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. //TCP Sockets
  2. #ifndef SOCKET_H
  3. #define SOCKET_H
  4.  
  5. #if defined _WIN32
  6. #define _WIN32_WINNT 0x501
  7. #include <winsock2.h>
  8. #include <ws2tcpip.h>
  9. #endif
  10.  
  11. #if defined __linux
  12. #include <sys/types.h>
  13. #include <sys/socket.h>
  14. #include <netdb.h>
  15. #define INVALID_SOCKET -1
  16. #endif
  17.  
  18. #if defined __APPLE__
  19. // Why would you do that?
  20. #endif
  21.  
  22. #include <string>
  23. #include <cstdio>
  24. #include <cstring>
  25. #include <sstream>
  26.  
  27. namespace aaix {
  28.  
  29.     const std::string sendl("\r\n");
  30.  
  31. class Socket
  32. {
  33. public:
  34.     Socket();
  35.     int connect(const std::string host, const std::string port);
  36.     int bindl(const std::string port, int backcon);
  37.     int accept(Socket& s);
  38.     int sock_err(const std::string err_string);
  39.     int get();
  40.     Socket& operator<< (const std::string& ostring);
  41.     Socket& operator<< (const int oint);
  42.     virtual ~Socket();
  43.     std::string recv_string;
  44.     #if defined __linux
  45.     int sock;
  46.     #elif defined __WIN32
  47.     SOCKET sock;
  48.     #endif
  49. protected:
  50.     struct addrinfo *ai;
  51.     std::stringstream ss;
  52.     std::string sendstring;
  53. };
  54.  
  55. Socket::Socket()
  56. {
  57. #if defined _WIN32
  58.     WSADATA wsa;
  59.     int wsa_err = WSAStartup(MAKEWORD(2,2), &wsa);
  60.     if ( wsa_err != 0 )
  61.     {
  62.         std::cout << "Winsock Error: " << wsa_err << std::endl;
  63.         WSACleanup();
  64.     }
  65. #endif
  66. }
  67.  
  68. int Socket::sock_err(const std::string err_string)
  69. {
  70. #if defined _WIN32
  71.     std::cout << err_string << std::endl << WSAGetLastError() << std::endl;
  72.     WSACleanup();
  73. #elif defined __linux
  74.     perror(err_string.c_str());
  75. #endif
  76.     freeaddrinfo(ai);
  77.     return false;
  78. }
  79.  
  80. int Socket::connect(const std::string host, const std::string port)
  81. {
  82.     struct addrinfo hints;
  83.     memset(&hints, 0, sizeof(struct addrinfo));
  84.     hints.ai_family = AF_INET;
  85.     hints.ai_socktype = SOCK_STREAM;
  86.     hints.ai_protocol = IPPROTO_TCP;
  87.     int gai_err = getaddrinfo(host.c_str(), port.c_str(), &hints, &ai);
  88.     if ( gai_err != 0 )
  89.     {
  90.         std::cout << gai_strerror(gai_err) << std::endl;
  91.         return false;
  92.     }
  93.     sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
  94.     if (sock == std::string::npos) sock_err("Socket creation failed");
  95.     if (::connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) sock_err("Connection failed");
  96.     std::cout << "yeah" << std::endl;
  97.     return true;
  98. }
  99.  
  100. int Socket::bindl(const std::string port, int backcon)
  101. {
  102.     struct addrinfo hints;
  103.     memset(&hints, 0, sizeof(struct addrinfo));
  104.     hints.ai_family = AF_INET;
  105.     hints.ai_socktype = SOCK_STREAM;
  106.     hints.ai_protocol = IPPROTO_TCP;
  107.     hints.ai_flags = AI_PASSIVE;
  108.     int gai_err = getaddrinfo(0, port.c_str(), &hints, &ai);
  109.     if ( gai_err != 0 )
  110.     {
  111.         std::cout << gai_strerror(gai_err) << std::endl;
  112.         return false;
  113.     }
  114.     sock = ::socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
  115.     if (sock == std::string::npos) sock_err("Socket creation failed");
  116.     if (::bind(sock, ai->ai_addr, ai->ai_addrlen)) sock_err("Bind failed");
  117.     freeaddrinfo(ai);
  118.     if(listen(sock, backcon)) return false;
  119.     std::cout << "binded" << std::endl;
  120.     return true;
  121. }
  122.  
  123. int Socket::accept(Socket& s)
  124. {
  125.     s.sock = ::accept(sock, 0, 0);
  126.     if (s.sock == INVALID_SOCKET) return false;
  127.     return true;
  128. }
  129.  
  130. // Idea :: Make this into the ">>" operator.
  131. int Socket::get()
  132. {
  133.     char buf[513];
  134.     int brecv = recv(sock, buf, 512, 0);
  135.     if (brecv <= 0) return false;
  136.     recv_string = buf;
  137.     memset(&buf, 0, 513);
  138.     return true;
  139. }
  140.  
  141. Socket::~Socket()
  142. {
  143. #if defined _WIN32
  144.     WSACleanup();
  145. #elif defined __linux
  146.     close(sock);
  147. #endif
  148. }
  149.  
  150. Socket& Socket::operator<< (const std::string& ostring)
  151. {
  152.     if (ostring == sendl)
  153.     {
  154.         ss << ostring;
  155.         sendstring = ss.str();
  156.         if (send(sock, sendstring.c_str(), sendstring.length() + 1, 0) == -1) sock_err("Send failed");
  157.         ss.str("");
  158.         puts(sendstring.c_str()); // Checking output. Will delete later.
  159.     }
  160.     ss << ostring;
  161.     return *this;
  162. }
  163.  
  164. Socket& Socket::operator<< (const int oint)
  165. {
  166.     ss << oint;
  167.     return *this;
  168. }
  169.  
  170. }
  171. #endif // SOCKET_H
Add Comment
Please, Sign In to add comment