socketio.hpp 875 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef SOCKETIO_HPP
  2. #define SOCKETIO_HPP
  3. #include <string>
  4. #include <cstring>
  5. #include <vector>
  6. #include <iostream>
  7. #include <exception>
  8. #include <cmath>
  9. #include <stdexcept>
  10. #include <sys/socket.h>
  11. #include <netinet/in.h>
  12. class cppsocket{
  13. private:
  14. struct sockaddr_in serv_addr;
  15. int sock;
  16. std::vector<char> buffer;
  17. public:
  18. const static std::size_t buffersize = 256;
  19. cppsocket(sockaddr_in _serv_addr, int _sock);
  20. cppsocket();
  21. cppsocket(const std::string& addr, unsigned int PORT);
  22. void close();
  23. int socket_id();
  24. void write(const std::string& message);
  25. void write(const std::vector<char>& message);
  26. std::vector<char> receive();
  27. };
  28. class server_socket{
  29. private:
  30. int port_;
  31. int server_fd;
  32. struct sockaddr_in address;
  33. socklen_t addrlen;
  34. public:
  35. int port();
  36. server_socket(int _port);
  37. cppsocket accept_connection();
  38. };
  39. #endif