Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // cn-lab4-server.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <thread>
- #include <string>
- #include <vector>
- #include <set>
- #include <windows.h>
- #pragma comment(lib, "ws2_32.lib")
- using namespace std;
- vector<thread> client_list;
- set<SOCKET> open_socks;
- int client_process(const SOCKET client_sock, const int client_id) {
- SECURITY_DESCRIPTOR sd;
- InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
- SetSecurityDescriptorDacl(&sd, true, 0, false);
- SECURITY_ATTRIBUTES sa;
- sa.lpSecurityDescriptor = &sd;
- sa.nLength = sizeof(sa);
- sa.bInheritHandle = true;
- HANDLE newstdin, newstdout, read_stdout, write_stdin;
- CreatePipe(&newstdin, &write_stdin, &sa, 0);
- CreatePipe(&read_stdout, &newstdout, &sa, 0);
- STARTUPINFO si;
- GetStartupInfo(&si);
- si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
- si.wShowWindow = SW_HIDE;
- si.hStdOutput = newstdout;
- si.hStdError = newstdout;
- si.hStdInput = newstdin;
- PROCESS_INFORMATION pi;
- 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);
- if (cp == 0)
- cout << GetLastError() << endl;
- for (int request_id = 1;; request_id++) {
- char recv_buff[8192];
- auto len = recv(client_sock, &recv_buff[0], sizeof(recv_buff) - 1, 0);
- if (len == -1) {
- cout << "Client " << client_id << " was closed" << endl;
- WriteFile(write_stdin, "&close", sizeof("&close"), 0, 0);
- break;
- }
- recv_buff[len] = '\0';
- //len++;
- if (recv_buff == "&close") {
- WriteFile(write_stdin, "&close", sizeof("&close"), 0, 0);
- break;
- }
- cout << "Received request " << request_id << " from client #" << client_id << endl;
- WriteFile(write_stdin, recv_buff, len, 0, 0);
- string ans;
- ReadFile(read_stdout, &ans, len, 0, 0);
- send(client_sock, ans.c_str(), ans.length(), 0);
- cout << "Sent answer for request " << request_id << " to client #" << client_id << endl;
- }
- CloseHandle(read_stdout);
- CloseHandle(write_stdin);
- CloseHandle(pi.hProcess);
- closesocket(client_sock);
- open_socks.erase(client_sock);
- cout << "Client " << client_id << " disconnected" << endl;
- return 0;
- }
- int port_listen(const SOCKET &main_sock) {
- for (int client_id = 1;; client_id++) {
- sockaddr_in addr_client;
- int client_addr_size = sizeof(addr_client);
- SOCKET client_sock = accept(main_sock, (sockaddr *)&addr_client, &client_addr_size);
- if (client_sock == INVALID_SOCKET) {
- auto wsa_error = WSAGetLastError();
- if (wsa_error != 10004)
- cout << "Accept error " << wsa_error << endl;
- return 1;
- }
- cout << "Client " << client_id << " connected" << endl;
- client_list.emplace_back(client_process, client_sock, client_id);
- open_socks.insert(client_sock);
- }
- return 0;
- }
- int main()
- {
- cout << "cn-lab4 var10 server start" << endl;
- WSAData wsadata;
- WSAStartup(0x0202, &wsadata);
- cout << "Windows Sockets API initialized" << endl;
- SOCKET main_sock;
- main_sock = socket(AF_INET, SOCK_STREAM, 0);
- if (main_sock == INVALID_SOCKET) {
- cout << "Main socket create error " << WSAGetLastError() << endl;
- WSACleanup();
- return 1;
- }
- cout << "Main socket created successfully" << endl;
- sockaddr_in addr_local;
- addr_local.sin_family = AF_INET;
- addr_local.sin_addr.S_un.S_addr = 0;
- addr_local.sin_port = htons(5004);
- if (::bind(main_sock, (sockaddr *)&addr_local, sizeof(addr_local))) {
- cout << "Bind error " << WSAGetLastError() << endl;
- closesocket(main_sock);
- WSACleanup();
- return 2;
- }
- cout << "Port " << ntohs(addr_local.sin_port) << " bound successful" << endl;
- if (listen(main_sock, 16))
- {
- cout << "Listen error " << WSAGetLastError() << endl;
- closesocket(main_sock);
- WSACleanup();
- return 3;
- }
- cout << "Listening port 5004" << endl;
- thread listen(port_listen, main_sock);
- string s;
- do {
- cin >> s;
- } while (s != "stop" && s != "exit");
- closesocket(main_sock);
- if (listen.joinable())
- listen.join();
- for (auto sock : open_socks) {
- send(sock, "&stop", sizeof("&stop"), 0);
- closesocket(sock);
- }
- for (unsigned int i = 0; i < client_list.size(); i++)
- if (client_list.at(i).joinable())
- client_list.at(i).join();
- WSACleanup();
- cout << "cn-lab4 var10 server stopped" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement