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)) 
  • What is net ? - Cerbo
  • socket class namespace - Jony Kook
  • since the try block many errors and superfluous. First, in the try block, remove the loop, because after the first loop, exceptions are already generated, and you in catch process them with only one operation, namely close the file ... therefore catch (std :: runtyme_error) will be at least more logical, but in your For example, it generally does not make sense to generate an exception, but simply if (! pipe) to close the file and exit (1); since you are not trying to catch exceptions that are generated repeatedly in the catch block, you do not call exec in the try block, well, etc. - AR Hovsepyan
  • It corrected, but the problem with the busy port did not solve ... - Jony Kook
  • one
    I do not know what net but in the basic functions for working with sockets there is a flag "close on exec" (FD_CLOEXEC), which is placed on the socket using fcntl() . It should be exposed on all sockets that the exec process should not have to coordinate - Mike

0