R
R
RusTorg2016-03-30 15:21:05
PHP
RusTorg, 2016-03-30 15:21:05

How do I fix a problem with socket connections?

$socket = socket_create(AF_INET, SOCK_DGRAM, 0);
            socket_set_option($socket,SOL_SOCKET, SO_RCVTIMEO, array("sec"=>2, "usec"=>0));
            $result = socket_connect($socket, $this->ip, $this->port);
    
            if($result <= 0) {
                echo "connect() failed.\nReason: ($result) \n";
            }
                $data = "\xFF\xFF\xFF\xFF\x54\x53\x6F\x75\x72\x63\x65\x20\x45\x6E\x67\x69\x6E\x65\x20\x51\x75\x65\x72\x79\x00";
                socket_write($socket, $data, strlen($data));
    
                    $out = socket_read($socket, 4096);
                    socket_close($socket);
                    if($out == null ){
    	                echo 'data null';
                    }
                    else {
                        $queryData = explode("\x00", substr($out, 6), 5);
                        $server['ip'] = $this->ip;
                        $server['port'] = $this->port;
                        $server['name'] = $queryData[0];
                        $server['map'] = $queryData[1];
                        $server['game'] = $queryData[2];
                        $server['description'] = $queryData[3];
                        $packet = $queryData[4];
                        //$app_id = array_pop(unpack("S", substr($packet, 0, 2)));
                        $server['players'] = ord(substr($packet, 2, 1));
                        $server['playersmax'] = ord(substr($packet, 3, 1));
                        $server['bots'] = ord(substr($packet, 4, 1));
                        $server['dedicated'] = substr($packet, 5, 1);
                        $server['os'] = substr($packet, 6, 1);
                        $server['password'] = ord(substr($packet, 7, 1));
                        $server['vac'] = ord(substr($packet, 8, 1));
                        $this->info = $server;
                               echo "<pre>";
                                print_r($queryData);
                                echo "</pre>";
                    }


This piece of code worked perfectly on my old server (centos + php5 + apache2);
But after migrating to a new one (ubuntu + php5 + apache2), it began to behave very strangely.

Through sockets, it seems like there is a connection to the server (no warnings, no errors), but in response to the request, it does not receive anything.

There is a feeling that he simply cannot connect to external servers. But I don't know how to fix it.

Socket support in php is enabled.

Thanks in advance for your replies!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question