Endil

cn-lab4-server

Dec 12th, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.77 KB | None | 0 0
  1. // cn-lab4-server.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <set>
  7. #include <sstream>
  8. #include <string>
  9. #include <thread>
  10. #include <vector>
  11. #include <winsock2.h>
  12.  
  13. #pragma comment(lib, "ws2_32.lib")
  14.  
  15. using namespace std;
  16.  
  17. vector<thread> client_list;
  18. set<SOCKET> open_socks;
  19.  
  20. int client_process(SOCKET client_sock, const int client_id) {
  21.     SECURITY_DESCRIPTOR sd;
  22.     InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
  23.     SetSecurityDescriptorDacl(&sd, true, 0, false);
  24.     SECURITY_ATTRIBUTES sa;
  25.     sa.lpSecurityDescriptor = &sd;
  26.     sa.nLength = sizeof(sa);
  27.     sa.bInheritHandle = true;
  28.     HANDLE newstdin, newstdout, read_stdout, write_stdin;
  29.     CreatePipe(&newstdin, &write_stdin, &sa, 0);
  30.     CreatePipe(&read_stdout, &newstdout, &sa, 0);
  31.     STARTUPINFO si;
  32.     GetStartupInfo(&si);
  33.     si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
  34.     si.wShowWindow = SW_HIDE;
  35.     si.hStdOutput = newstdout;
  36.     si.hStdError = newstdout;
  37.     si.hStdInput = newstdin;
  38.     PROCESS_INFORMATION pi;
  39.     int len = 0;
  40.     char buffer[65536];
  41.     auto cp = CreateProcess(L"C:\\Study\\CN\\cn-lab4-server-rp\\Debug\\cn-lab4-server-rp.exe", 0, 0, 0, true, CREATE_NEW_CONSOLE, 0, 0, &si, &pi);
  42.     if (cp == 0)
  43.         cout << GetLastError() << endl;
  44.     WSAPROTOCOL_INFO protocol_info;
  45.     WSADuplicateSocket(client_sock, pi.dwProcessId, &protocol_info);
  46.     //WriteFile(write_stdin, &client_sock, sizeof(client_sock), 0, 0);
  47.     string s;
  48.     WriteFile(write_stdin, s.c_str(), s.length(), 0, 0);
  49.     WriteFile(write_stdin, &protocol_info, sizeof(protocol_info), 0, 0);
  50.     system("pause");
  51.     /*for (int request_id = 1;; request_id++) {
  52.         char buffer[65536];
  53.         long len = recv(client_sock, &buffer[0], sizeof(buffer) - 1, 0);
  54.         if (len == -1 || len == 0)
  55.             break;
  56.         buffer[len] = '\n';
  57.         if (buffer == "&close")
  58.             break;
  59.         cout << "Received request " << request_id << " from client #" << client_id << endl;
  60.         WriteFile(write_stdin, &buffer, len + 1, 0, 0);
  61.         len = 0;
  62.         while (!len) {
  63.             Sleep(100);
  64.             auto ret = PeekNamedPipe(read_stdout, 0, 0, 0, (LPDWORD)&len, 0);
  65.             if (ret == false)
  66.                 len = -1;
  67.         }
  68.         ReadFile(read_stdout, buffer, len + 1, 0, 0);
  69.         buffer[len - 1] = 0;
  70.         send(client_sock, buffer, len, 0);
  71.         cout << "Sent answer for request " << request_id << " to client #" << client_id << endl;
  72.     }
  73.     cout << "Client " << client_id << " was closed" << endl;
  74.     WriteFile(write_stdin, "&close", sizeof("&close"), 0, 0);
  75.     CloseHandle(read_stdout);
  76.     CloseHandle(write_stdin);
  77.     TerminateProcess(pi.hProcess, 0);
  78.     CloseHandle(pi.hProcess);
  79.     closesocket(client_sock);
  80.     open_socks.erase(client_sock);*/
  81.     return 0;
  82. }
  83.  
  84. int port_listen(const SOCKET &main_sock) {
  85.     for (int client_id = 1;; client_id++) {
  86.         sockaddr_in addr_client;
  87.         int client_addr_size = sizeof(addr_client);
  88.         SOCKET client_sock = accept(main_sock, (sockaddr *)&addr_client, &client_addr_size);
  89.         if (client_sock == INVALID_SOCKET) {
  90.             auto wsa_error = WSAGetLastError();
  91.             if (wsa_error != 10004)
  92.                 cout << "Accept error " << wsa_error << endl;
  93.             return 1;
  94.         }
  95.         cout << "Client " << client_id << " connected" << endl;
  96.         client_list.emplace_back(client_process, client_sock, client_id);
  97.         //open_socks.insert(client_sock);
  98.     }
  99.     return 0;
  100. }
  101.  
  102. int main()
  103. {
  104.     cout << "cn-lab4 var10 server start" << endl;
  105.     WSAData wsadata;
  106.     WSAStartup(0x0202, &wsadata);
  107.     cout << "Windows Sockets API initialized" << endl;
  108.     SOCKET main_sock;
  109.     main_sock = socket(AF_INET, SOCK_STREAM, 0);
  110.     if (main_sock == INVALID_SOCKET) {
  111.         cout << "Main socket create error " << WSAGetLastError() << endl;
  112.         WSACleanup();
  113.         return 1;
  114.     }
  115.     cout << "Main socket created successfully" << endl;
  116.     sockaddr_in addr_local;
  117.     addr_local.sin_family = AF_INET;
  118.     addr_local.sin_addr.S_un.S_addr = 0;
  119.     addr_local.sin_port = htons(5004);
  120.     if (::bind(main_sock, (sockaddr *)&addr_local, sizeof(addr_local))) {
  121.         cout << "Bind error " << WSAGetLastError() << endl;
  122.         closesocket(main_sock);
  123.         WSACleanup();
  124.         return 2;
  125.     }
  126.     cout << "Port " << ntohs(addr_local.sin_port) << " bound successful" << endl;
  127.     if (listen(main_sock, 16))
  128.     {
  129.         cout << "Listen error " << WSAGetLastError() << endl;
  130.         closesocket(main_sock);
  131.         WSACleanup();
  132.         return 3;
  133.     }
  134.     cout << "Listening port 5004" << endl;
  135.     thread listen(port_listen, main_sock);
  136.     string s;
  137.     do {
  138.         cin >> s;
  139.     } while (s != "stop" && s != "exit");
  140.  
  141.     closesocket(main_sock);
  142.     for (auto client_sock : open_socks) {
  143.         send(client_sock, "&stop", sizeof("&stop"), 0);
  144.         closesocket(client_sock);
  145.     }
  146.     if (listen.joinable())
  147.         listen.join();
  148.     for (unsigned int i = 0; i < client_list.size(); i++)
  149.         if (client_list.at(i).joinable())
  150.             client_list.at(i).join();
  151.     WSACleanup();
  152.     cout << "cn-lab4 var10 server was stopped" << endl;
  153.     return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment