Advertisement
fimas

server.cpp

Jan 19th, 2014
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netdb.h>
  9. #include <unistd.h>
  10. #include <sys/time.h>
  11. #include <sys/wait.h>
  12. #include <signal.h>
  13. #include <sys/poll.h>
  14.  
  15. class Socket
  16. {
  17.   public:
  18.     Socket(std::string port)
  19.     {
  20.         prepareAddrinfo(port);
  21.         sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  22.  
  23.         if (sockfd == -1)
  24.         {
  25.             perror("socket");
  26.             exit(1);
  27.         }
  28.         else
  29.         {
  30.             printf("Created socket\n");
  31.         }
  32.  
  33.         if (setNonBlocking() < 0)
  34.         {
  35.             perror("socket");
  36.         }
  37.         else
  38.         {
  39.             printf("Set socket to non-blocking mode\n");
  40.         }
  41.     }
  42.  
  43.     Socket(int sock)
  44.     {
  45.         if (sock < 0)
  46.         {
  47.             perror("socket");
  48.             exit(1);
  49.         }
  50.  
  51.         sockfd = sock;
  52.     }
  53.  
  54.     ~Socket()
  55.     {
  56.         close(sockfd);
  57.     }
  58.  
  59.     void prepareAddrinfo(std::string port)
  60.     {
  61.         memset(&hints, 0, sizeof hints);
  62.         hints.ai_family = AF_UNSPEC;
  63.         hints.ai_socktype = SOCK_STREAM;
  64.         hints.ai_flags = AI_PASSIVE;
  65.  
  66.         getaddrinfo(NULL, port.c_str(), &hints, &res);
  67.     }
  68.  
  69.     int getSocket()
  70.     {
  71.         return sockfd;
  72.     }
  73.  
  74.     int Bind()
  75.     {
  76.         return bind(sockfd, res->ai_addr, res->ai_addrlen);
  77.     }
  78.  
  79.     int Listen(int connections = 1)
  80.     {
  81.         return listen(sockfd, connections);
  82.     }
  83.  
  84.     int Accept()
  85.     {
  86.         return accept(sockfd, NULL, NULL);
  87.     }
  88.  
  89.     int Send(std::string data)
  90.     {
  91.         return send(sockfd, data.c_str(), data.length(), 0);
  92.     }
  93.  
  94.     std::string Recv(int block = 1024)
  95.     {
  96.         char *buffer;
  97.         buffer = new char[block];
  98.         memset(buffer, 0, block);
  99.         recv(sockfd, buffer, block, 0);
  100.         std::string msg = buffer;
  101.         delete buffer;
  102.         return msg;
  103.     }
  104.  
  105.     int setNonBlocking()
  106.     {
  107.         return setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&blocking,
  108.                           sizeof(blocking));
  109.     }
  110.  
  111.   private:
  112.     int sockfd, blocking;
  113.     struct addrinfo hints, *res;
  114. };
  115.  
  116. class Server
  117. {
  118.   public:
  119.     Server()
  120.     {
  121.         printf("Starting server\n");
  122.  
  123.         srvSock = new Socket("8080");
  124.         srvSock->Bind();
  125.         srvSock->Listen();
  126.  
  127.         this->mainLoop();
  128.     }
  129.  
  130.      ~Server()
  131.     {
  132.         delete srvSock;
  133.         printf("Cleaned up\n");
  134.     }
  135.  
  136.     virtual int mainLoop() = 0;
  137.  
  138.   protected:
  139.     Socket * srvSock;
  140. };
  141.  
  142. class ChatServer:public Server
  143. {
  144.   public:
  145.     int mainLoop()
  146.     {
  147.         return 0;
  148.     }
  149. };
  150.  
  151. int main()
  152. {
  153.     ChatServer *s;
  154.     s = new ChatServer();
  155.     delete s;
  156.  
  157.     printf("Bye :)\n");
  158.  
  159.     return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement