0
votes

I am new to sockets . I have taken reference from code from google but it doesn't seem to work .
I am posting server and client php files. Please identify the issue .

Server.php

<?php
$host = "xxx.xxx.xxx.xxx/myfolder/server.php"; //host
$port = 9000; //port

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) ;
$result = socket_bind($socket, $host, $port);
$result = socket_listen($socket,5);

$spawn = socket_accept($socket);

$input = socket_read($spawn , 1024);

$output = strrev($input)."n";

socket_write($spawn, $output , strlen($output));

socket_close($spawn);
socket_close($socket);
?>

And here's the Client.php

<?
 $host    = "xxx.xxx.xxx.xxx/myfolder/server.php";
 $port    = 9000;

$socket = socket_create(AF_INET, SOCK_STREAM, 0) ;

$result = socket_connect($socket, $host, $port) ;

socket_write($socket, $message, strlen($message)) ;

$result = socket_read ($socket, 1024) ;
echo "Reply From Server  :".$result;

socket_close($socket);
?>

after having both the above files on my public directory on my hosting.

I first run the command : php -q /var/www/html/myfolder/server.php
but i get this on my cmd shell :

$ php -q /var/www/html/myfolder/server.php
PHP Warning: socket_bind(): Host lookup failed [-10001]: Unknown host in /var/www/html/myfolder/server.php on line 13
Unable to bind socket at server

(and yes port 9000 is open )

1
What are you trying to accomplish? And are you sure you want to use PHP as your server listening on a socket for incoming connections? Though PHP can do this, it is not the most suitable environment for this. For example, you will need to keep your server process (PHP script) running all time while you want to accept connections. This can be done by running a cronjob that every few checks if the previous process is still living, etc.nl-x
i just need to work this out using php only ... can u suggest me a solution to my problemNikhil
Did you take a look at the answer provided by Latheesan Kanes? It comes down to that a host is an IP address or a hostname that resolves to an IP address. But it is not a URL. Socket connections do not neccessarily use HTTP.nl-x

1 Answers

3
votes

When you create a socket server, you don't specify the host as the full URL to your script; that's why the bind is failing.

Take a look at this sample: http://www.php.net/manual/en/sockets.examples.php

#!/usr/local/bin/php -q
<?php
error_reporting(E_ALL);

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting
 * as it comes in. */
ob_implicit_flush();

$address = '192.168.1.53';
$port = 10000;

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, 5) === false) {
    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

do {
    if (($msgsock = socket_accept($sock)) === false) {
        echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
        break;
    }
    /* Send instructions. */
    $msg = "\nWelcome to the PHP Test Server. \n" .
        "To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
    socket_write($msgsock, $msg, strlen($msg));

    do {
        if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
            echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
            break 2;
        }
        if (!$buf = trim($buf)) {
            continue;
        }
        if ($buf == 'quit') {
            break;
        }
        if ($buf == 'shutdown') {
            socket_close($msgsock);
            break 2;
        }
        $talkback = "PHP: You said '$buf'.\n";
        socket_write($msgsock, $talkback, strlen($talkback));
        echo "$buf\n";
    } while (true);
    socket_close($msgsock);
} while (true);

socket_close($sock);
?>