Advertisement
Guest User

Socket

a guest
Jul 20th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include "Socket.h"
  2.  
  3.  
  4. // Link with ws2_32.lib
  5. #pragma comment(lib, "Ws2_32.lib")
  6.  
  7. Socket::Socket()
  8. {
  9.     listen_socket = INVALID_SOCKET;
  10.     bound = false;
  11. }
  12.  
  13. Socket::Socket(std::string IP_address_param, uint16_t port_param)
  14. {
  15.     IP_address = IP_address_param;
  16.     port = port_param;
  17.     listen_socket = INVALID_SOCKET;
  18.     bound = false;
  19. }
  20.  
  21.  
  22. Socket::~Socket()
  23. {
  24.  
  25. }
  26.  
  27.  
  28. bool Socket::operator<(const Socket& other) const
  29. {
  30.     if (IP_address < other.IP_address)
  31.         true;
  32.     if (IP_address > other.IP_address)
  33.         return false;
  34.     else    // If they have same IP addresses
  35.     {
  36.         if (port < other.port)
  37.             return true;
  38.         else
  39.             return false;
  40.     }
  41. }
  42.  
  43.  
  44. bool Socket::operator==(const Socket& other) const
  45. {
  46.     if (IP_address == other.IP_address)
  47.     {
  48.         if (port == other.port)
  49.             return true;
  50.     }
  51.  
  52.     return false;
  53. }
  54.  
  55.  
  56.  
  57. std::ostream& operator<<(std::ostream& os, const Socket& sock)
  58. {
  59.     os << sock.IP_address << ":" << sock.port;
  60.     return os;
  61. }
  62.  
  63. int Socket::close_socket()
  64. {
  65.     int result = closesocket(listen_socket);
  66.     return result;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement