Php Socket programming host error - php

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 )

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

Related

Multiple Socket Connection Using PHP

I have made a socket connection using php which is working perfectly below is the code for single socket but not working with multiple connections only accepts one connection at a time
error_reporting(E_ALL);
// sudo lsof -t -i:10000
// php -f server.php
/* 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.5.155';
$port = 3490;
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, 1024) === 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_BINARY_READ))) {
/*echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
break 2;*/
}
if (!$buf = trim($buf)) {
continue;
}
if ($buf == 'quit') {
break 2;
}
if ($buf == 'shutdown') {
socket_close($msgsock);
break 2;
}
$talkback = "PHP: You said '$buf'.\n";
if($buf != '')
{
$req_get = $talkback;
$file = time();
$fp = fopen('data/'.$file.'.txt', 'a+');
fwrite($fp, $req_get);
fclose($fp);
socket_write($msgsock, $talkback, strlen($talkback));
}
//echo "$buf\n";
} while (true);
socket_close($msgsock);
} while (true);
socket_close($sock);
I need a help to just make it work with multiple connection with clients.
After that i need to make entry into database from client sent data.
You can't listen many sockets using blocking socket connections at the same time. What you need is non-blocking socket connections with socket_select method instead of socket_accept.
My suggestion is to use some kind of PHP event-loop implementations (like reactphp).
Anyway, if you still want to implement it yourself, you can check the example of stream_select usage in reactphp/event-loop StreamSelectLoop implementation.

implement chat service using socket programming in php

I wanted to implement a chat service in PHP for which I am using socket programming.
Below are the client and server file I found to implement it:
Server.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);
?>
Client.php:
<?php
error_reporting(E_ALL);
echo "<h2>TCP/IP Connection</h2>\n";
/* Get the port for the WWW service. */
$service_port = getservbyname('www', 'tcp');
/* Get the IP address for the target host. */
$address = gethostbyname('www.example.com');
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
echo "OK.\n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
echo "OK.\n";
}
$in = "HEAD / HTTP/1.1\r\n";
$in .= "Host: www.example.com\r\n";
$in .= "Connection: Close\r\n\r\n";
$out = '';
echo "Sending HTTP HEAD request...";
socket_write($socket, $in, strlen($in));
echo "OK.\n";
echo "Reading response:\n\n";
while ($out = socket_read($socket, 2048)) {
echo $out;
}
echo "Closing socket...";
socket_close($socket);
echo "OK.\n\n";
?>
But I am not able to understand one thing in this process.I will be hosting this application to a remote server and both the files will reside in that server.Now the line socket_connect() in client.php would try to connect with the IP address I pass to this function as a parameter,and,for the application to work I need both the server.php and client.php files.
Now, let us assume that the client.php sends a request to a particular user X,now for the user X to accept that request,he should also have a server.php file to accept the HTTP request made,but the server.php is on the server where the application is hosted.So how the user X's machine will be able to accept the request?
Can anyone please tell how this will be handled or please correct me if I misunderstood the process?

Can't Create Client Server Socket Program in Php

I am working on Client Server Socket Program for chat like application, but it is giving following error message
Warning: socket_bind(): unable to bind address [98]: Address already in use.... Could not bind to address on line 15
I have seen various tutorials, but i want Server to listen to client request continuously. I have seen port number it is open also but still the same message. I am stuck at this point for several days could not get proper solutions. Please help to solve it thanks.....
Server.php:
<?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 = 'XXX.XX.XX.XXX';
$port = 15213;
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);
?>
Client.php
<?php
error_reporting(E_ALL);
echo "<h2>TCP/IP Connection</h2>\n";
/* Get the port for the WWW service. */
$service_port = getservbyname('www', 'tcp');
/* Get the IP address for the target host. */
$address = gethostbyname('www.example.com');
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
echo "OK.\n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
echo "OK.\n";
}
$in = "HEAD / HTTP/1.1\r\n";
$in .= "Host: www.example.com\r\n";
$in .= "Connection: Close\r\n\r\n";
$out = '';
echo "Sending HTTP HEAD request...";
socket_write($socket, $in, strlen($in));
echo "OK.\n";
echo "Reading response:\n\n";
while ($out = socket_read($socket, 2048)) {
echo $out;
}
echo "Closing socket...";
socket_close($socket);
echo "OK.\n\n";
?>
In both php files you create a Socket on the same port. That is the problem.
The Server has to create the Socket while the client has to use the Socket (not creating it, too)
Two applications can not create the same socket and listen on the same port.
EDIT:
I have tested your script with xampp at localhost. so IP was 127.0.0.1. The Server listened properly on port 15213 and the client connected properly on this port.
That the port was open i saw with the xampp controll panel.
If you use your scripts like you posted here, then you have to replace in Server.php
$address = 'XXX.XX.XX.XXX';
with
$address = '127.0.0.1';
and in Client.php
$address = gethostbyname('www.example.com');
with
$address = gethostbyname('localhost');
or
$address = '127.0.0.1';

What difference with socket in windows using php?

I have wrote one app in php which uses sockets. Suddenly there was a need to run it on windows, before this it was only on linux with no problems.
Currently problem is in socket_recv function which is used like $bytes = #socket_recv($socket, $data, 2048, MSG_DONTWAIT);. First of all on the windows there isn't any MSG_DONTWAIT constant as i get nocite about it. I found a small fix for it like:
if (!defined('MSG_DONTWAIT'))
define('MSG_DONTWAIT', 0x40);
Then it says:
Warning: socket_recv(): unable to read from socket [0]: The operation completed
successfully.
After it i decided to ask may be there is some difference with work with sockets on Windows and Linux?
I believe there is a difference when you create socket in windows as opposed to linux.
Try something like this:
<?php
// Init
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$address = '127.0.0.1';
$port = 10000;
// On Windows we need to use AF_INET
$domain = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' ? AF_INET : AF_UNIX);
// Create socket
if (($sock = socket_create($domain, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
// Bind socket to port
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
// start listening
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);
?>

Why does TCP work, when UDP does not?

The code:
<?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 = '127.0.0.1';
$port = 11100;
if (($sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UP)) === 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;
}
do {
$out = socket_read($msgsock, 2048);
if (!empty($out)) {
if ($out == 'quit') {
break;
}
elseif ($out == 'shutdown') {
socket_write($msgsock, 'plc down', 8);
socket_close($msgsock);
break 2;
}
else {
switch ($out) {
case "KABBE": $response = "Kabbe te!"; break;
case "SZOPJ": $response = "Szopjal te!"; break;
default: $response = "Ismeretlen parancs";
}
socket_write($msgsock, $response, strlen($response));
break;
}
}
} while (true);
socket_close($msgsock);
} while (true);
socket_close($sock);
?>
It works with TCP:
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
but with UDP it's not working:
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
The errors:
Warning: socket_listen() [function.socket-listen]: unable to listen on socket [0]: The attempted operation is not supported for the type of object referenced. in C:\wamp\www\socket\socket.php on line 22
socket_listen() failed: reason: The attempted operation is not supported for the type of object referenced.
Warning: socket_accept() [function.socket-accept]: unable to accept incoming connection [0]: The attempted operation is not supported for the type of object referenced. in C:\wamp\www\socket\socket.php on line 27
socket_accept() failed: reason: The attempted operation is not supported for the type of object referenced.
Because TCP is connection oriented and UDP is not, and there are different APIs for UDP sockets. Have a look at socket_recvfrom and socket_sendto.
I've fixed it by editing Growl class from
socket_sendto($sck, $data, strlen($data), 0x100, $this->address, $this->port);
to
socket_sendto($sck, $data, strlen($data), 0x0, $this->address, $this->port);

Categories