PHP Socket doesn't working on Server - php

I have a domain (ad-box.deslab.vn). I want to PHP socket to communicate with clients. However, After I run program, it has error:
****Warning: socket_bind(): unable to bind address [22]: Invalid argument in /home/vietchip/public_html/deslab.vn/ad-box/index.php on line 27
socket_bind() failed: reason: Invalid argument socket_bind() failed: reason: Invalid argument****
Link: ad-box.deslab.vn/index.php
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 = "181.224.157.142";
$port = 10000;
// Create Socket
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($sock === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo socket_strerror(socket_last_error($sock));
exit;
}
// Bind socket to port
socket_bind($sock, $address, $port);
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
// Start listening for connection
socket_listen($sock, 5); // Maximum is 5 connection
if (socket_listen($sock, 5) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
// Handling connection from client
do {
$msgsock = socket_accept($sock); // msgsock is a client connect to webserver
if ($msgsock === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
break;
}
$request ="PHP said : you are sent " . socket_read($msgsock, 2048, PHP_BINARY_READ);
socket_write($msgsock, $request, strlen($request));
socket_close($msgsock); // Close connect of client
} while (true);
socket_close($sock); // Close socket of server
?>
Please help me fix it.
Thanks.

Related

socket_connect is working, but not socket_bind showing warring

With my code, I can connect properly but socket bind is not working please help me.
Showing this error:
Warning: socket_bind(): unable to bind address [99]: Cannot assign requested address in /opt/lampp/htdocs/testingServer/socket.php on line 35
socket_bind() failed: reason: Cannot assign requested address
Warning: socket_listen(): unable to listen on socket [22]: Invalid argument in /opt/lampp/htdocs/testingServer/socket.php on line 39
socket_listen() failed: reason: Invalid argument
Here's the code :
$address = '192.168.0.250';
$port = 8050;
/* Get the port for the WWW service. */
// echo $port = getservbyname('www', 'tcp');
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
echo "<pre/>";
print_r($sock);
echo "Socket created"; echo "<br/>";
}
// check ip and port showing online or offline
if (!$socket = #fsockopen($address, $port, $errno, $errstr, 3)) {
echo "Offline!";
} else {
echo "Online!";
fclose($socket);
}
$result = socket_connect($sock, $address, $port) ;
var_dump($result);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " .
socket_strerror(socket_last_error($sock)) . "\n";
}
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
if (socket_listen($sock,3) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
$msg = "\nWelcome to the PHP Test Server. \n" .
"To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
socket_write($sock, $msg, strlen($msg));
socket_close($sock);

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

php socket_accept stalled

Hello i made a php socket server to get data from plc, plc is configured as tcp socket client.
I've a seriuous problem, if local net go down seems that function socket_accept stalled, plc don'be able to send me data.
If i restart my server plc reconnect correctly.
Can someone hel me?
My server code:
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 = ipserver;
$port = 10001;
if (($sock = socket_create(AF_INET, SOCK_STREAM, getprotobyname("TCP"))) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
exit;
}
if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo socket_strerror(socket_last_error($sock));
exit;
}
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
exit;
}
if (socket_listen($sock, 5) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
exit;
}
while(true)
{
//$remote_fd = socket_accept($sock);
if (($remote_fd = socket_accept($sock)) === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
break ;
}
do {
$recv = "";
if (false === ($recv = socket_read($remote_fd, 128, PHP_BINARY_READ))) {
echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($remote_fd)) . "\n";
#break 2;
}
if($recv != "") {
echo $recv."\n";
inserisci_letture("t_letture",trim($recv));
if($sent=socket_write($remote_fd,"1",1)===false)
{
echo "socket_write() failed: reason: " . socket_strerror(socket_last_error($remote_fd)) . "\n";
}
}
}
while($recv != "");
}
socket_shutdown($sock);
socket_close($sock);
?>
Listen on 0.0.0.0 instead of a specific interface, so that the socket is not closed if the network (the interface) does down. This is a special address that listens on all interfaces.

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

Socket not working PHP

Here is my 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 = 'localhost';
$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;
}
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);
?>
and now the errors
Warning: socket_bind() [function.socket-bind]: unable to bind address [0]: Only one usage of each socket address (protocol/network address/port) is normally permitted. in C:\wamp\www\socket\socket.php on line 18
socket_bind() failed: reason: Only one usage of each socket address (protocol/network address/port) is normally permitted.
Warning: socket_listen() [function.socket-listen]: unable to listen on socket [0]: An invalid argument was supplied. in C:\wamp\www\socket\socket.php on line 22
socket_listen() failed: reason: An invalid argument was supplied.
Warning: socket_accept() [function.socket-accept]: unable to accept incoming connection [0]: An invalid argument was supplied. in C:\wamp\www\socket\socket.php on line 27
socket_accept() failed: reason: An invalid argument was supplied.
I searched on google but nothing useful.
What is the problem?
PHP also offers stream_socket_server and other stream_socket_* functions.
I found these to be more developer friendly.
Example code from php.net:
$socket = stream_socket_server("tcp://localhost:8000", $errno, $errstr);
if (!$socket) {
echo "$errstr ($errno)<br />\n";
} else {
while ($conn = stream_socket_accept($socket)) {
fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') . "\n");
fclose($conn);
}
fclose($socket);
}
'localhost' isn't a valid address as socket_bind doesn't accept DNS names, use the equivalent IP address '127.0.0.1'.
More info
That means you already have an open socket on your computer on that port.
Try switching to another unused port.
On Windows (which is what you seem to be working on), you can see the list of open sockets from the command line:
netstat -an
If you want to know which processes are listening to those ports, try this instead:
netstat -ban
use this code before binding:
if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo socket_strerror(socket_last_error($socket));
exit;
}
for reference http://www.php.net/manual/en/function.socket-bind.php
You can also check http://www.php.net/manual/en/function.socket-set-option.php for details

Categories