How to run PHP Port Listener Script with WAMP not PHP CLI?

609 Views Asked by At

I hope that I do not ask a spam question because I can not find any question about it. I write a socket port listener to listen an specific IP:PORT using PHP to receives TCP Packets from GPS Tracking Devices. When I run script with this command:

$php.exe -q My/Script/Files/Path.PHP

everything is okay and my Server port listener works successfully and I receive data from devices but when I want open that with my browser (wamp -> localhost) it's said that

Warning: socket_bind(): unable to bind address [10049]: The requested address is not valid in its context.

How I can Solve This Problem? Best Regards.

Edit One:
My Server Script Code:

<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();

$address = '*.*.*.*'; //I put Here My System Local IPV4 Address (163.*.*.*)
$port = *****; //I put Here My Server Opened Port Number and I turned off the firewall to test

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}

if (socket_bind($sock, $address, $port) === false) {
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

if (socket_listen($sock, 1) === false) {
    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

if ($msgsock = socket_accept($sock)) === false) {
        echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
 }    
if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
            echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
 }
 $buf = trim($buf);
 $clientResponse = explode(',', $buf);
 foreach($clientResponse as $key=>$value) {
     echo $key . ' : ' . $value . "<br />";
}
socket_close($msgsock);
socket_close($sock);
?>

As I said before:
When I run this script with PHP CLI, everything is okay and my device sends TCP Packet and My server receives TCP Packet and show that information. But when I run it By wamp with it local IP 127.0.0.1, it gave me that error.

till here:

  • I have a VPS Server
  • I have a Static IP (Public IP)
  • I have an Open TCP port with off firewall
  • I have a local IP
  • I have a wamp with IP:Port -> 127.0.0.1:80

Edit Two:
My Script Port: 41260

1

There are 1 best solutions below

0
On

I google some keywords and I find this docs : (Bind) in Apache's official Site. And I decided to share the result to other users.