Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. #include "Communicator.h"
  2. #include <thread>
  3. #include <iostream>
  4. #include <exception>
  5. #include <string>
  6.  
  7. #define PORT 4242
  8.  
  9. Communicator::Communicator(RequestHandlerFactory* handlerFactory) : m_handlerFactory(handlerFactory)
  10. {
  11.     // notice that we step out to the global namespace
  12.     // for the resolution of the function socket
  13.  
  14.     // this server use TCP. that why SOCK_STREAM & IPPROTO_TCP
  15.     // if the server use UDP we will use: SOCK_DGRAM & IPPROTO_UDP
  16.     m_socket = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  17.  
  18.     if (m_socket == INVALID_SOCKET)
  19.         throw std::exception(__FUNCTION__ " - socket");
  20. }
  21.  
  22. Communicator::~Communicator()
  23. {
  24.     try
  25.     {
  26.         // the only use of the destructor should be for freeing
  27.         // resources that was allocated in the constructor
  28.         ::closesocket(m_socket);
  29.     }
  30.     catch (...) {}
  31. }
  32.  
  33. void Communicator::bindAndListen()
  34. {
  35.     struct sockaddr_in sa = { 0 };
  36.  
  37.     sa.sin_port = htons(PORT); // port that server will listen for
  38.     sa.sin_family = AF_INET;   // must be AF_INET
  39.     sa.sin_addr.s_addr = INADDR_ANY;    // when there are few ip's for the machine. We will use always "INADDR_ANY"
  40.  
  41.                                         // again stepping out to the global namespace
  42.                                         // Connects between the socket and the configuration (port and etc..)
  43.     if (::bind(m_socket, (struct sockaddr*)&sa, sizeof(sa)) == SOCKET_ERROR)
  44.         throw std::exception(__FUNCTION__ " - bind");
  45.  
  46.     // Start listening for incoming requests of clients
  47.     if (::listen(m_socket, SOMAXCONN) == SOCKET_ERROR)
  48.         throw std::exception(__FUNCTION__ " - listen");
  49.     std::cout << "Listening on port " << PORT << std::endl;
  50.  
  51.     while (true)
  52.     {
  53.         // the main thread is only accepting clients
  54.         // and add then to the list of handlers
  55.         std::cout << "Waiting for client connection request" << std::endl;
  56.        
  57.         // this accepts the client and create a specific socket from server to this client
  58.         SOCKET client_socket = ::accept(m_socket, NULL, NULL);
  59.  
  60.         if (client_socket == INVALID_SOCKET)
  61.             throw std::exception(__FUNCTION__);
  62.  
  63.         std::cout << "Client accepted. Server and client can speak" << std::endl;
  64.  
  65.         // the function that handle the conversation with the client
  66.         std::thread t(&Communicator::startThreadForNewClient, this, client_socket);
  67.     }
  68. }
  69.  
  70. void Communicator::handleRequests()
  71. {
  72.  
  73. }
  74.  
  75. void Communicator::startThreadForNewClient(SOCKET socket)
  76. {
  77.     try
  78.     {
  79.         std::string s = "Welcome! What is your name (4 bytes)? ";
  80.         send(socket, s.c_str(), s.size(), 0);  // last parameter: flag. for us will be 0.
  81.  
  82.         char m[5];
  83.         recv(socket, m, 4, 0);
  84.         m[4] = 0;
  85.         std::cout << "Client name is: " << m << std::endl;
  86.  
  87.         s = "Bye";
  88.         send(socket, s.c_str(), s.size(), 0);
  89.  
  90.         // Closing the socket (in the level of the TCP protocol)
  91.         closesocket(socket);
  92.     }
  93.     catch (const std::exception& e)
  94.     {
  95.         closesocket(socket);
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement