Endil

cn-lab1-server-mt

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