2

I have a Symfony command that runs on startup with supervisor on a debian 9 computer. It opens a TCP Listener Socket to receive messages from many devices on a port.

After some time working (between 15 - 20 hours), it stops working and its blocks the TCP port.

The issue occurs on a Debian 9 computer, running PHP 7.3 and Apache2.

This is the code to open the TCP socket:

protected function execute(InputInterface $input, OutputInterface $output)
{
    try {
        $this->io = new SymfonyStyle($input, $output);
        $this->logger->info('Start TCP socket: Setup websocket server for detections on port 8086...');

        $now = new \DateTime();
        $this->io->title('Start server ' . $now->format('d-m-Y G:i:s') . '...');

        $server = socket_create_listen(8086);
        socket_getsockname($server, $addr, $port);
        if (!$server) {
            $message = 'Start TCP socket: Ko. Could not create socket.';
            $this->io->error($message);
            $this->logger->critical($message);
            die($message);
        } else {
            $message = 'Start TCP socket: Ok. TCP socket opened on: ' . $addr . ':' . $port . '.';
            $this->io->success($message);
            $this->logger->info($message);

            while ($c = socket_accept($server)) {
                socket_getpeername($c, $raddr, $rport);
                $this->io->writeln("Received Connection from $raddr:$rport\n");

                $data = '';
                while ($bytes = socket_recv($c, $r_data, 128, MSG_WAITALL)) {
                    $data .= $r_data;
                }

                //Process data here

                socket_close($c);

                $message = 'Finish processing data. Total Data Received: ' . strlen($data) . PHP_EOL;
                $this->io->writeln($message);
                $this->logger->info($message);
            }
        }
        fclose($server);
    } catch (Exception $exception) {
        $message = 'Start TCP socket: Ko. Exception catched. Error detail: ' . $exception->getMessage();
        $this->logger->critical($message);
        $this->io->error($message);
    }
}

When the socket stops the connection, I write this command in the console:

sudo netstat -np | grep :8086

It shows this output:

tcp 0 1 172.25.1.14:8086 88.0.111.77:47794 FIN_WAIT1 -

How can I avoid this issue and try to restart the service or do not block the port?

In general, the code that @fyrie has provided, works well. But sometimes, if I run this command on a terminal:

netstat -ap | grep :8086

the port is listening but I can see a lot of clients connected all the time and the port is not able to receive more connections. I am not sure if the clients have an issue while sending data (the clients are cameras developed by a third party). This is the result of the command executed before:

root@server:~# netstat -ap | grep :8086
tcp      129      0 0.0.0.0:8086            0.0.0.0:*               LISTEN      5884/php7.3         
tcp   252549      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49038 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:43126         CLOSE_WAIT  -                   
tcp        1      0 localhost:8086          localhost:41882         CLOSE_WAIT  -                   
tcp   255702      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53158 ESTABLISHED -                   
tcp   264640      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:42138 ESTABLISHED -                   
tcp   256282      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53159 ESTABLISHED -                   
tcp   254661      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:42144 ESTABLISHED -                   
tcp   260514      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:42129 ESTABLISHED -                   
tcp   257008      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53190 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:43354         CLOSE_WAIT  -                   
tcp        1      0 localhost:8086          localhost:42784         CLOSE_WAIT  -                   
tcp   259529      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53169 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:42102         CLOSE_WAIT  -                   
tcp        0      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:42059 ESTABLISHED 5884/php7.3         
tcp   261888      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49037 ESTABLISHED -                   
tcp   255166      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53182 ESTABLISHED -                   
tcp   255728      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53175 ESTABLISHED -                   
tcp   253363      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53162 ESTABLISHED -                   
tcp   263064      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:42145 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:44878         CLOSE_WAIT  -                   
tcp   262305      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49045 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:43458         CLOSE_WAIT  -                   
tcp   257966      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53160 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:45306         CLOSE_WAIT  -                   
tcp   258642      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53166 ESTABLISHED -                   
tcp   261952      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49032 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:46714         CLOSE_WAIT  -                   
tcp   261847      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:42140 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:44990         CLOSE_WAIT  -                   
tcp   255966      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53161 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:45194         CLOSE_WAIT  -                   
tcp   259702      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49048 ESTABLISHED -                   
tcp   264566      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53188 ESTABLISHED -                   
tcp   264653      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49050 ESTABLISHED -                   
tcp   261917      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49039 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:46444         CLOSE_WAIT  -                   
tcp        1      0 localhost:8086          localhost:46120         CLOSE_WAIT  -                   
tcp   260054      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:42130 ESTABLISHED -                   
tcp   264006      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53157 ESTABLISHED -                   
tcp   261594      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49042 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:44028         CLOSE_WAIT  -                   
tcp        1      0 localhost:8086          localhost:46572         CLOSE_WAIT  -                   
tcp        1      0 localhost:8086          localhost:42690         CLOSE_WAIT  -                   
tcp        1      0 localhost:8086          localhost:42996         CLOSE_WAIT  -                   
tcp   257838      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53168 ESTABLISHED -                   
tcp   258233      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49033 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:47026         CLOSE_WAIT  -                   
tcp        1      0 localhost:8086          localhost:42344         CLOSE_WAIT  -                   
tcp        1      0 localhost:8086          localhost:45648         CLOSE_WAIT  -                   
tcp        1      0 localhost:8086          localhost:47144         CLOSE_WAIT  -                   
tcp        1      0 localhost:8086          localhost:44754         CLOSE_WAIT  -                   
tcp   263557      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53165 ESTABLISHED -                   
tcp   262822      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53192 ESTABLISHED -                   
tcp   257325      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49036 ESTABLISHED -                   
tcp   256445      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:42128 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:45754         CLOSE_WAIT  -                   
tcp        1      0 localhost:8086          localhost:41998         CLOSE_WAIT  -                   
tcp        1      0 localhost:8086          localhost:43226         CLOSE_WAIT  -                   
tcp        1      0 localhost:8086          localhost:44140         CLOSE_WAIT  -                   
tcp   256478      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53178 ESTABLISHED -                   
tcp   260293      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53191 ESTABLISHED -                   
tcp   263530      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:42142 ESTABLISHED -                   
tcp   253410      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53183 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:46226         CLOSE_WAIT  -                   
tcp   260734      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:42143 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:43682         CLOSE_WAIT  -                   
tcp        1      0 localhost:8086          localhost:42568         CLOSE_WAIT  -                   
tcp        1      0 localhost:8086          localhost:43906         CLOSE_WAIT  -                   
tcp   261311      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53181 ESTABLISHED -                   
tcp   260133      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49051 ESTABLISHED -                   
tcp   255737      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:42141 ESTABLISHED -                   
tcp   262800      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53164 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:45890         CLOSE_WAIT  -                   
tcp        1      0 localhost:8086          localhost:45412         CLOSE_WAIT  -                   
tcp   254701      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53179 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:46338         CLOSE_WAIT  -                   
tcp   263634      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49049 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:44270         CLOSE_WAIT  -                   
tcp        1      0 localhost:8086          localhost:46002         CLOSE_WAIT  -                   
tcp   261865      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49031 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:46918         CLOSE_WAIT  -                   
tcp   263014      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49056 ESTABLISHED -                   
tcp   260848      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53184 ESTABLISHED -                   
tcp   258259      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53174 ESTABLISHED -                   
tcp   253944      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53185 ESTABLISHED -                   
tcp   263624      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53171 ESTABLISHED -                   
tcp   258515      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:42131 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:43582         CLOSE_WAIT  -                   
tcp   261357      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53156 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:45524         CLOSE_WAIT  -                   
tcp   263874      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:42137 ESTABLISHED -                   
tcp   256794      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:42132 ESTABLISHED -                   
tcp   264694      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53167 ESTABLISHED -                   
tcp   263758      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53186 ESTABLISHED -                   
tcp   262597      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:42133 ESTABLISHED -                   
tcp   259834      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49055 ESTABLISHED -                   
tcp   257029      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:42139 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:42450         CLOSE_WAIT  -                   
tcp        0      1 localhost:49590         localhost:8086          SYN_SENT    11468/php           
tcp   252397      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49044 ESTABLISHED -                   
tcp   258041      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:42134 ESTABLISHED -                   
tcp   256094      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53173 ESTABLISHED -                   
tcp   259581      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49053 ESTABLISHED -                   
tcp   263043      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49040 ESTABLISHED -                   
tcp   254773      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49035 ESTABLISHED -                   
tcp   264142      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53189 ESTABLISHED -                   
tcp   259931      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49047 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:42232         CLOSE_WAIT  -                   
tcp        1      0 localhost:8086          localhost:42900         CLOSE_WAIT  -                   
tcp        1      0 localhost:8086          localhost:44512         CLOSE_WAIT  -                   
tcp   254872      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53180 ESTABLISHED -                   
tcp   255430      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49054 ESTABLISHED -                   
tcp   261974      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49041 ESTABLISHED -                   
tcp   256785      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53187 ESTABLISHED -                   
tcp   260811      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53163 ESTABLISHED -                   
tcp   259840      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:42136 ESTABLISHED -                   
tcp   257809      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:42146 ESTABLISHED -                   
tcp   261468      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49052 ESTABLISHED -                   
tcp   260353      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53177 ESTABLISHED -                   
tcp   255050      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53172 ESTABLISHED -                   
tcp   259158      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53170 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:44394         CLOSE_WAIT  -                   
tcp   261154      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49043 ESTABLISHED -                   
tcp   261499      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49046 ESTABLISHED -                   
tcp   258328      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53176 ESTABLISHED -                   
tcp   252203      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:49034 ESTABLISHED -                   
tcp        1      0 localhost:8086          localhost:43788         CLOSE_WAIT  -                   
tcp        1      0 localhost:8086          localhost:44636         CLOSE_WAIT  -                   
tcp        1      0 localhost:8086          localhost:45084         CLOSE_WAIT  -                   
tcp   261083      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:42135 ESTABLISHED -                   
tcp   252424      0 cpe-172-100-100-54:8086 cpe-172-100-100-1:53193 ESTABLISHED -              

Thanks.

JoakDA
  • 305
  • 1
  • 13
  • @fyrye I have edited my post to show the code for execute function. For server side code I am not allowed to use other technologies different than PHP and Symfony. Thanks for helping me. – JoakDA Jul 11 '19 at 07:37
  • Yes, I am using the database to save the data received. – JoakDA Jul 11 '19 at 08:23
  • @fyrye I am having issues with some clients that get stuck and they could not clase the connection? Please could you help me? Thanks – JoakDA Oct 27 '19 at 20:23
  • Did you ever found a solution ? I have the same problem – Raymond Sep 28 '22 at 12:14

0 Answers0