PHP socket Listening loop

22,971

Solution 1

set_time_limit (0);

$address = '46.49.41.188';

$port = 7777;
$con = 1;
$word = "";

$sock = socket_create(AF_INET, SOCK_STREAM, 0);
$bind = socket_bind($sock, $address, $port);

socket_listen($sock);

while ($con == 1)
{
    $client = socket_accept($sock);
    $input = socket_read($client, 2024);

    if ($input == 'exit') 
    {
        $close = socket_close($sock);
        $con = 0;
    }

    if($con == 1)
    {
        $word .= $input;
    }
}

echo $word;

If request will be exit listening will closed. That method was tested :) and it works.

Solution 2

How about:

function listen(){
// Set the ip and port we will listen on
$address = 'XX.XX.XX.XXX';
$port = XXXX;
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
$bind = socket_bind($sock, $address, $port);
// Start listening for connections
socket_listen($sock);
/* Accept incoming requests and handle them as child processes */
$client = socket_accept($sock);
// Read the input from the client – 1024 bytes
$input = socket_read($client, 2024);
// Strip all white spaces from input
echo $input;
// Close the master sockets
$close = socket_close($sock);
var_dump($close);
listen();
}
set_time_limit (0);
listen();
Share:
22,971
Dest
Author by

Dest

I like new creative ideas in everything, even in programming. So i'm trying do my best and innovate something. I work in two company at the same time but also taking private projects from other companies (frequently orders are about websites but I also have done other kind of solutions). I have my own engine (Which is written in native PHP as main language) for websites and building websites on it.

Updated on September 22, 2020

Comments

  • Dest
    Dest over 3 years

    With the following code, I can receive 1 request and write it:

    function listen()
    {
        // Set time limit to indefinite execution
        set_time_limit (0);
    
        // Set the ip and port we will listen on
        $address = 'XX.XX.XX.XXX';
        $port = XXXX;
    
        // Create a TCP Stream socket
        $sock = socket_create(AF_INET, SOCK_STREAM, 0);
    
        // Bind the socket to an address/port
        $bind = socket_bind($sock, $address, $port);
    
        // Start listening for connections
        socket_listen($sock);
    
        /* Accept incoming requests and handle them as child processes */
        $client = socket_accept($sock);
    
        // Read the input from the client – 1024 bytes
        $input = socket_read($client, 2024);
    
        // Strip all white spaces from input
        echo $input;
    
        // Close the master sockets
        $close = socket_close($sock);
    
        var_dump($close);
    }
    
    listen();
    

    But it close automatically listening once it received 1 packet. I need to keep receiving packets until an exit or any close command is received.

    How should I go about changing the code above to make this function into a loop?

  • Dest
    Dest over 11 years
    at first thanks for the answer :)... i will try this example but problem is that: socket_close can't close socket quickly. i have one example how to make my code into loop. i will write that code.