server.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "server.hpp"
  2. #include "client.hpp"
  3. double const &Server::getVersion() const {
  4. return this->version;
  5. }
  6. void Server::setVersion(double const &version){
  7. this->version = version;
  8. }
  9. string const &Server::getName() const{
  10. return this->name;
  11. }
  12. void Server::setName(string const &name){
  13. this->name = name;
  14. }
  15. string const &Server::getIP() const{
  16. return this->ip;
  17. }
  18. void Server::setIP(string const &ip){
  19. this->ip = ip;
  20. }
  21. u_short const &Server::getPort() const{
  22. return this->port;
  23. }
  24. void Server::setPort(int const &port){
  25. this->port = htons(port);
  26. }
  27. int const &Server::getMaxQueue() const{
  28. return this->maxQueue;
  29. }
  30. void Server::setMaxQueue(int const &maxQueue){
  31. this->maxQueue = maxQueue;
  32. }
  33. SOCKET &Server::getListenSocket(){
  34. return this->listenSocket;
  35. }
  36. void Server::setListenSocket(SOCKET const &listenSocket){
  37. this->listenSocket = listenSocket;
  38. }
  39. vector<Client *> &Server::getClients(){
  40. return this->clients;
  41. }
  42. void Server::setClients(vector<Client *> const clients){
  43. this->clients = clients;
  44. }
  45. SOCKADDR_IN &Server::getServerAddr(){
  46. return this->serverAddr;
  47. }
  48. WSADATA &Server::getWSAData(){
  49. return wsaData;
  50. }
  51. Server::Server(): Server("Test-Server", 1.0, "127.0.0.1", 7070){}
  52. Server::Server(string const &name, double const &version, string const &ip, int const &port){
  53. this->setVersion(version);
  54. this->setName(name);
  55. this->setIP(ip);
  56. this->setPort(port);
  57. this->setMaxQueue(5);
  58. }
  59. Server::~Server(){
  60. this->stop();
  61. }
  62. Server::operator string(){
  63. return this->getName() + ", v" + to_string(this->getVersion());
  64. }
  65. void Server::start(){
  66. cout << "Server is starting..." << endl;
  67. if(WSAStartup(MAKEWORD(2,2),(LPWSADATA)&(this->getWSAData())) != 0){
  68. cout << "WSAStartup() Error: " << WSAGetLastError() << endl;
  69. exit(1);
  70. }
  71. memset(&serverAddr,0,sizeof(SOCKADDR_IN));
  72. this->getServerAddr().sin_family = AF_INET;
  73. this->getServerAddr().sin_port = this->getPort();
  74. this->getServerAddr().sin_addr.s_addr = inet_addr(this->getIP().c_str());
  75. this->setListenSocket(socket(AF_INET, SOCK_STREAM, IPPROTO_TCP));
  76. if(this->getListenSocket() == INVALID_SOCKET) {
  77. cout << "listenSocket Error: " << WSAGetLastError() << endl;
  78. WSACleanup();
  79. exit(EXIT_FAILURE);
  80. }
  81. cout << "Listen socket created..." << endl;
  82. if(bind(this->getListenSocket(),(SOCKADDR*)&(this->getServerAddr()),sizeof(SOCKADDR_IN)) == SOCKET_ERROR){
  83. cout << "bind() Error: " << WSAGetLastError() << endl;
  84. WSACleanup();
  85. exit(EXIT_FAILURE);
  86. }
  87. cout << "IP & Port binded..." << endl;
  88. if(listen(this->getListenSocket(), this->getMaxQueue()) == SOCKET_ERROR){
  89. cout << "listen() Error: " << WSAGetLastError() << endl;
  90. WSACleanup();
  91. exit(EXIT_FAILURE);
  92. }
  93. cout << "Server started and listen now!" << endl;
  94. cout << "Waiting for clients..." << endl;
  95. for(;;){
  96. Client *client = new Client();
  97. int size = sizeof(client->getSocketInfo());
  98. SOCKET clientSocket = accept(this->getListenSocket(), (sockaddr *)&client->getSocketInfo(), &size);
  99. cout << "New incoming connection..." << endl;
  100. if(clientSocket == INVALID_SOCKET){
  101. cout << "socket accept() Error: " << WSAGetLastError() << endl;
  102. continue;
  103. }
  104. cout << "Socket is valid, starting client thread..." << endl;
  105. this->getClients().push_back(client);
  106. client->setSocket(clientSocket);
  107. client->setWorkThread(new thread(&Client::startWorkThread, client, this));
  108. }
  109. }
  110. void Server::stop(){
  111. for(auto it = this->getClients().begin(); it != this->getClients().end(); it++){
  112. delete *it;
  113. }
  114. closesocket(this->getListenSocket());
  115. WSACleanup();
  116. }