Endil

cn-lab1-server

Sep 23rd, 2015
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.36 KB | None | 0 0
  1. // CN-lab1-server-mt.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <winsock2.h>
  6. #include <iostream>
  7. #include <thread>
  8. #include <string>
  9. #include <vector>
  10. #include <set>
  11.  
  12. #pragma comment(lib, "ws2_32.lib")
  13.  
  14. using namespace std;
  15.  
  16. vector<thread> clients;
  17. set<SOCKET> socks;
  18.  
  19. int client_process(const SOCKET client_sock, const int client_id) {
  20.     for (int request_id = 1;; request_id++) {
  21.         char recv_buff[1024];
  22.         int len = recv(client_sock, &recv_buff[0], sizeof(recv_buff) - 1, 0);
  23.         if (len == -1) {
  24.             cout << "Client " << client_id << " was closed" << endl;
  25.             return 1;
  26.         }
  27.         recv_buff[len] = '\0';
  28.         string s = recv_buff, ans = "palindrome";
  29.         if (s == "&close")
  30.             break;
  31.         cout << "Received request " << request_id << " from client " << client_id << endl;
  32.         for (unsigned int i = 0; i <= s.length() / 2; i++) {
  33.             if (s[i] != s[s.length() - 1 - i]) {
  34.                 ans = "not palindrome";
  35.                 break;
  36.             }
  37.         }
  38.         send(client_sock, ans.c_str(), ans.length(), 0);
  39.         cout << "Sent answer for request " << request_id << " to client " << client_id << endl;
  40.     }
  41.     closesocket(client_sock);
  42.     socks.erase(client_sock);
  43.     cout << "Client " << client_id << " disconnected" << endl;
  44.     return 0;
  45. }
  46.  
  47. int port_listen(const SOCKET &main_sock) {
  48.     for (int client_id = 1;; client_id++) {
  49.         sockaddr_in addr_client;
  50.         int client_addr_size = sizeof(addr_client);
  51.         SOCKET client_sock = accept(main_sock, (sockaddr *)&addr_client, &client_addr_size);
  52.         if (client_sock == INVALID_SOCKET) {
  53.             int wsa_error = WSAGetLastError();
  54.             if (wsa_error != 10004)
  55.                 cout << "Accept error " << wsa_error << endl;
  56.             return 1;
  57.         }
  58.         cout << "Client " << client_id << " connected" << endl;
  59.         clients.push_back(thread(client_process, client_sock, client_id));
  60.         socks.insert(client_sock);
  61.     }
  62.     return 0;
  63. }
  64.  
  65. int _tmain(int argc, _TCHAR* argv[])
  66. {
  67.     cout << "cn-lab1 var10 server start" << endl;
  68.     WSAData wsadata;
  69.     WSAStartup(0x0202, &wsadata);
  70.     cout << "WinSock2 initialized" << endl;
  71.     SOCKET main_sock;
  72.     main_sock = socket(AF_INET, SOCK_STREAM, 0);
  73.     if (main_sock == INVALID_SOCKET) {
  74.         cout << "Main socket create error " << WSAGetLastError() << endl;
  75.         WSACleanup();
  76.         return 1;
  77.     }
  78.     cout << "Main socket created successfully" << endl;
  79.     sockaddr_in local_addr;
  80.     local_addr.sin_family = AF_INET;
  81.     local_addr.sin_addr.S_un.S_addr = 0;
  82.     local_addr.sin_port = htons(5001);
  83.     if (::bind(main_sock, (sockaddr *) &local_addr, sizeof(local_addr))) {
  84.         cout << "Bind error " << WSAGetLastError() << endl;
  85.         closesocket(main_sock);
  86.         WSACleanup();
  87.         return 2;
  88.     }
  89.     cout << "Port " << ntohs(local_addr.sin_port) << " bound successful" << endl;
  90.     if (listen(main_sock, 16))
  91.     {
  92.         cout << "Listen error " << WSAGetLastError() << endl;
  93.         closesocket(main_sock);
  94.         WSACleanup();
  95.         return  3;
  96.     }
  97.     cout << "Listening port 5001" << endl;
  98.     thread listen(port_listen, main_sock);
  99.     string s;
  100.     do {
  101.         cin >> s;
  102.     } while (s != "stop" && s != "exit");
  103.     closesocket(main_sock);
  104.     if (listen.joinable())
  105.         listen.join();
  106.     for (auto client_sock : socks) {
  107.         send(client_sock, "&stop", sizeof("&stop"), 0);
  108.         closesocket(client_sock);
  109.     }
  110.     for (unsigned int i = 0; i < clients.size(); i++)
  111.         if (clients[i].joinable())
  112.             clients[i].join();
  113.     WSACleanup();
  114.     cout << "cn-lab1 var10 server stopped" << endl;
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment