1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| #include "server.h"
#include <iostream> #include <ostream>
Server::~Server() { close(); } void Server::close() { if (servSock != INVALID_SOCKET) { closesocket(servSock); for (SOCKET client : clientList) closesocket(client); WSACleanup(); } } bool Server::init(const char* ip, unsigned short port) { WSADATA data{}; if (WSAStartup(MAKEWORD(2, 2), &data) == SOCKET_ERROR) { std::cout << "startup error" << std::endl; return false; } servSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (servSock == INVALID_SOCKET) { std::cout << "socket error" << WSAGetLastError() << std::endl; WSACleanup(); return false; } sockaddr_in servAddr{};
servAddr.sin_family = AF_INET; servAddr.sin_port = htons(2345); inet_pton(AF_INET, "127.0.0.1", &servAddr.sin_addr.S_un.S_addr);
if (bind(servSock, (sockaddr*)&servAddr, sizeof(sockaddr)) == SOCKET_ERROR) { std::cout << "bind error" << WSAGetLastError() << std::endl; WSACleanup(); return false; }
if (listen(servSock, 128) == SOCKET_ERROR) { std::cout << "listen error" << std::endl; WSACleanup(); return false; } return true; } bool Server::start() { if (!coreFunc()) { return false; } } bool Server::coreFunc() { assert(servSock != INVALID_SOCKET); while (1) { fd_set fdRead; FD_ZERO(&fdRead); FD_SET(servSock, &fdRead); for (SOCKET client : clientList) { FD_SET(client, &fdRead); } timeval tv{ 1,0 }; int selectRes = select(0, &fdRead, nullptr, nullptr, &tv); if (selectRes > 0) { for (size_t i = 0; i < fdRead.fd_count; ++i) { if (fdRead.fd_array[i] == servSock) { SOCKET clntSock = accept(servSock, nullptr, nullptr); if (clntSock == INVALID_SOCKET) { std::cout << "accept error" << std::endl; break; } clientList.push_back(clntSock); } else { recvMsg(fdRead, fdRead.fd_array[i]); } } } else if (selectRes == 0) { std::cout << "do something else" << std::endl; continue; } else { std::cout << "select func error" << WSAGetLastError() << std::endl; return false; } } return true; } bool Server::recvMsg(fd_set& fdRead, SOCKET& clientSock) { Header header; int recvRes = recv(clientSock, (char*)&header, sizeof(Header), 0); if (recvRes <= 0) { std::cout << "Client disconnected:" << clientSock << std::endl; clientList.erase(std::remove(clientList.begin(), clientList.end(), clientSock), clientList.end()); closesocket(clientSock);
return false; } switch (header.cmd) { case CMD::LOGIN: { Login login; int recvRes2 = recv(clientSock, (char*)&login + sizeof(Header), sizeof(Login) - sizeof(Header), 0); if (recvRes2 <= 0) { std::cout << "recv error" << std::endl; return false; } LoginResult loginResult(10); if (send(clientSock, (char*)&loginResult, sizeof(LoginResult), 0) < 0) { std::cout << "send error" << std::endl; return false; } break; } case CMD::LOGOUT: { Logout logout; int recvRes2 = recv(clientSock, (char*)&logout + sizeof(Header), sizeof(Logout) - sizeof(Header), 0); if (recvRes2 <= 0) { std::cout << "recv error" << std::endl; return false; } LogoutResult logoutResult(10); if (send(clientSock, (char*)&logoutResult, sizeof(LoginResult), 0) < 0) { std::cout << "send error" << std::endl; return false; } break; } case CMD::LOGIN_RESULT: break; case CMD::LOGOUT_RESULT: break; } }
|