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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
| #include <thread> #include <fstream> #include "tcpSocket.h" #include "Clog.h" #include "tcpSocket/MySocket.hpp" #include "WebServer.h" #include "tinyxml2.h" #include "xmlParser.h"
struct SockInfo { sockaddr_in addr; int fd; };
typedef struct { char UserName[32]; int SocketID; }loginPool;
std::vector<loginPool> login_pool_vect;
bool is_login(std::vector<loginPool> &ptr, int socket_id) { for (auto & x : ptr) { if (x.SocketID == socket_id) { return true; } } return false; }
bool login(char *username, char *password, int socket_id) { if ((strcmp(username, "admin") == 0) && (strcmp(password, "123456") == 0)) { loginPool pool_ptr; pool_ptr.SocketID = socket_id; strcpy(pool_ptr.UserName, "admin");
login_pool_vect.push_back(pool_ptr); return true; } else if ((strcmp(username, "admin") == 0) && (strcmp(password, "123456") == 0)) { loginPool pool_ptr; pool_ptr.SocketID = socket_id; strcpy(pool_ptr.UserName, "admin");
login_pool_vect.push_back(pool_ptr); return true; } return false; }
bool logout(std::vector<loginPool> &ptr, int socket_id) { for (auto it = ptr.begin(); it != ptr.end(); it++) { if (it->SocketID == socket_id) { ptr.erase(it); return true; } } return false; }
void ClientPro(void* ptr) { auto* pSock = (MySocket*)ptr; MySocket server_socket = *pSock; server_socket.Send((const char *)"Welcome to CPP WebSocket Server!", 31);
char sIp[20]; UINT nPort; server_socket.GetPeerName(sIp, nPort);
while (true) { char szBuffer[4096] = { 0 }; int sid = pSock->GetSocketID();
int ref = server_socket.Receive(szBuffer, sizeof(szBuffer)); if (ref <= 0) { logout(login_pool_vect, sid); ilog->warn("client :{0}:{1} , [disconnect]",sIp,nPort);
break; } ilog->info("address:{0}:{1},receive command:{2} ",sIp,nPort,szBuffer);
if (strcmp(szBuffer, "login\n") == 0) { char recv_username[32] = { 0 }; char recv_password[32] = { 0 };
pSock->Receive(recv_username, 32, 0); pSock->Receive(recv_password, 32, 0);
ilog->info("account :{0}, password : {1}",recv_username,recv_password); bool login_flag = login(recv_username, recv_password, sid); if (login_flag == TRUE) { ilog->info("user :{0}, login .",recv_username);
pSock->Send("login", sizeof("login"), 0); } else { pSock->Send("account or password incorrect", sizeof("account or password incorrect"), 0); } }
else if (strcmp(szBuffer, "logout\n") == 0) { int login_flag = is_login(login_pool_vect, sid); if (login_flag == TRUE) { ilog->info("user logout.");
logout(login_pool_vect, sid); pSock->Send("user logout.", sizeof("user logout."), 0); } else { ilog->warn("please login account!");
pSock->Send("please login account!", sizeof("please login account!"), 0); } }
else if (strcmp(szBuffer, "list\n") == 0) { int login_flag = is_login(login_pool_vect, sid); if (login_flag == TRUE) { ilog->info("user has login , output current pc file ");
pSock->Send("auth success!", sizeof("auth success!"), 0);
for (int x = 0; x < 10; x++) { char sz[1024] = { 0 }; sprintf(sz, "count -> %d", x); pSock->Send(sz, sizeof(sz), 0); } } else { ilog->warn("please login account!");
pSock->Send("please login account!", sizeof("please login account!"), 0); } } } }
int startSocketServer(){ MySocket sock; if (!sock.Create(8233, SOCK_STREAM, "127.0.0.1")) { return -1; }
char sSevIp[20]; UINT nSevPort; sock.GetSockName(sSevIp, nSevPort); ilog->info("server : {0},{1} ,server is running!",sSevIp,nSevPort);
sock.Listen(5);
char sIp[20]; UINT nPort;
MySocket ptr; while(true) { sock.Accept(ptr, sIp, &nPort); ilog->info("client : {0} :{1} [login]",sIp,nPort);
_beginthread(ClientPro, 0, &ptr); } return 0; }
std::string testR(wsProtocol ws) { return "asd"; }
std::string httpR(requestHttp httpRq) { return "httpR"; }
void XmlNodeGetVal(std::stringstream stringstream, tinyxml2::XMLElement *pElement, const char *string, const char *ip) { ip = pElement->Attribute(string); }
int main() {
startSocketServer();
return 0; }
|