socket_read fails after 2 attempts - php

I am trying to connect to a TCP socket server using php. First 2 connections and send it is successfull but after that it can not read, hangs at socket_read.
<?php
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
if (socket_connect($sock, '127.0.0.1', 10000) === false) {
echo "socket_connect() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
do {
$stdin = fopen('php://stdin', 'r');
$msg = trim(fgets($stdin));
socket_write($sock, $msg, strlen($msg));
if (false === ($buf = socket_read($sock, 2048, PHP_NORMAL_READ))) {
echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
echo "response: $buf \n";
} while (true);
Server is functioning properly there are no problems on that end. Any suggestions anyone ?

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

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?

Read socket created using PHP code by Node.js for real time notification

I have created a socket using php by this 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 = 5000;
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);
?>
It is working fine in the case php. But i want to read the message sent through cmd in node Js so that we could notify the client for the message. But i tried to much by using socket.io but did not get success. Please help.
Thanks in advance.

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

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.

Categories