Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CN-lab1-server.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <winsock2.h>
- #include <iostream>
- #include <thread>
- #include <string>
- #include <vector>
- #pragma comment(lib, "ws2_32.lib")
- using namespace std;
- vector<thread > clients;
- vector<SOCKET > socks;
- int client_process(SOCKET sock_client, int id) {
- while (true) {
- char recv_buff[1024];
- int len = recv(sock_client, &recv_buff[0], sizeof(recv_buff) - 1, 0);
- if (len == -1) {
- cout << "Client " << id << " socket closed" << endl;
- return 0;
- }
- recv_buff[len] = '\0';
- string s = recv_buff, is_pal = "1";
- if (s == "&close")
- break;
- cout << "Request received from client " << id << endl;
- for (int i = 0; i <= s.length() / 2; i++) {
- if (s[i] != s[s.length() - 1 - i]) {
- is_pal[0] = '0';
- break;
- }
- }
- send(sock_client, is_pal.c_str(), is_pal.length(), 0);
- cout << "Answer sent to client " << id << endl;
- }
- closesocket(sock_client);
- cout << "Client " << id << " disconnected" << endl;
- return 0;
- }
- int port_listen(SOCKET sock) {
- for (int id = 1;; id++) {
- sockaddr_in addr_client;
- int addr_client_size = sizeof(addr_client);
- SOCKET sock_client = accept(sock, (sockaddr *)&addr_client, &addr_client_size);
- if (sock_client == INVALID_SOCKET) {
- int wsa_error = WSAGetLastError();
- if (wsa_error != 10004)
- cout << "Accept error " << WSAGetLastError() << endl;
- return 0;
- }
- cout << "Client " << id << " connected" << endl;
- clients.push_back(thread(client_process, sock_client, id));
- socks.push_back(sock_client);
- }
- return 0;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- cout << "cn-lab1 var10 server start" << endl;
- WSAData wsadata;
- WSAStartup(0x0202, &wsadata);
- SOCKET sock_main;
- sock_main = socket(AF_INET, SOCK_STREAM, 0);
- if (sock_main == INVALID_SOCKET) {
- cout << "Create socket 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(5001);
- if (::bind(sock_main, (sockaddr *) &addr_local, sizeof(addr_local))) {
- cout << "Bind error " << WSAGetLastError() << endl;
- closesocket(sock_main);
- WSACleanup();
- return 2;
- }
- cout << "Port 5001 bound" << endl;
- if (listen(sock_main, 16))
- {
- cout << "Listen error " << WSAGetLastError() << endl;
- closesocket(sock_main);
- WSACleanup();
- return 3;
- }
- cout << "Listening port 5001" << endl;
- thread listen(port_listen, ref(sock_main));
- string s;
- do {
- cin >> s;
- } while (s != "stop" && s != "exit");
- closesocket(sock_main);
- if (listen.joinable())
- listen.join();
- for (int i = 0; i < socks.size(); i++) {
- closesocket(socks[i]);
- if (clients[i].joinable())
- clients[i].join();
- }
- WSACleanup();
- cout << "cn-lab1 var10 server stopped" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment