Hello! I wrote a php game server control script in php.
I decided to write a server for it in c ++.
Everything seems to be not bad, except for one, when I start the game server through the PU and stop my server, the game starts to hang on the server port.
And in order to restart the management server, you need to kill the game server.
Help solve the problem.
Exec function:
string exec(const char *cmd) { char buffer[2048]; std::string result = ""; FILE *pipe = popen(cmd, "r"); if (!pipe) throw std::runtime_error("popen() failed!"); try { while (!feof(pipe)) { if (fgets(buffer, 2048, pipe) != NULL) result += buffer; } } catch (...) { pclose(pipe); throw; } pclose(pipe); return result; } Server startup:
server = new net::serversocket(8888); while (true) { net::socket *client = server->accept(); string readMessage = ""; string sendMessage = ""; client->read(readMessage); std::vector <std::string> command = split(readMessage, '|'); int startI = 0; if (command[0] == "start") { int forkStartServer = fork(); if(forkStartServer < 0){ }else if (forkStartServer == 0){ setsid(); forkStartServer = fork(); if(forkStartServer < 0){ }else if (forkStartServer == 0){ exec(command[1].c_str()); } } sendMessage = "start 1"; client->send(sendMessage); client->close(); } } The ss -p command: After stopping the management server and when the game server is running, it is filled with such records
tcp CLOSE-WAIT 1 0 127.0.0.1:8888 127.0.0.1:35044 users:(("gs",pid=17536,fd=13),("glinkd",pid=17535,fd=13),("gdeliveryd",pid=17526,fd=13),("gfactiond",pid=17524,fd=13),("gacd",pid=17523,fd=13),("java",pid=17521,fd=13),("gamedbd",pid=17517,fd=13),("uniquenamed",pid=17504,fd=13),("logservice",pid=17497,fd=13))
net? - Cerbonetbut in the basic functions for working with sockets there is a flag "close on exec" (FD_CLOEXEC), which is placed on the socket usingfcntl(). It should be exposed on all sockets that the exec process should not have to coordinate - Mike