Nikhil
Nikhil

Reputation: 467

Php Socket programming host error

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 )

Upvotes: 1

Views: 8967

Answers (1)

Latheesan
Latheesan

Reputation: 24116

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);
?>

Upvotes: 3

Related Questions