1

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 )

3
  • 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. Commented May 12, 2014 at 7:53
  • i just need to work this out using php only ... can u suggest me a solution to my problem Commented May 12, 2014 at 7:59
  • 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. Commented May 12, 2014 at 8:01

1 Answer 1

3

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);
?>
Sign up to request clarification or add additional context in comments.

6 Comments

$ php -q /var/www/html/myfolder/server.php PHP Warning: socket_bind(): unable to bind address [99]: Cannot assign requested address in /var/www/html/myfolder/server.php on line 13 Unable to bind socket at server
this is what i get after doing what u suggested
Try changing the ip to $address = '0.0.0.0'; - I just tested it and it worked.
okk..so the server is up and running but now the client is not working: Could not connect client to server I am keeping host as"xxx.xxx.xxx.xxx/myfolder/server.php"
Did you look at the link I posted? It has the code for the server and client. Try using this client code: php.net/manual/en/sockets.examples.php#example-4676
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.