PHP Socket Program hangs - php

I have a socket server program written in PHP and run by Fedora 21-apache, listening on a port.
<?php
set_time_limit (0);
$address = '1.2.3.4';
$port = "19000";
$con = 1;
$word = "";
$sock = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$bind = socket_bind($sock, $address, $port) or die("Could not bind to socket\n");
socket_listen($sock);
while ($con == 1)
{
$client = socket_accept($sock);
$input = socket_read($client, 50);
if ($input == 'exit')
{
$close = socket_close($sock);
$con = 0;
}
else {
$input = trim($input) . "," . date('Y-m-d H:i:s') . "\n";
$file= "/home/xyz/data/" . "uls_" . date("Ymd_His") . ".dat";
file_put_contents($file, $input);
}
}
?>
The client is actually a device. Currently there is only one device running.
As per the documentation provided by the device,
"It acts as a TCP client and opens a TCP socket session to the Server. The
device then sends a message and disconnects the socket session. Failed connections force retries"
Now I am able to get data from this client. The problem is that after say 4-5 Hours client is not able to push data.The server socket hangs.
The netstat -taun command shows following.
tcp 0 0 1.2.3.4:19000 0.0.0.0:* LISTEN
tcp 0 0 1.2.3.4:19000 3.4.5.6:20721 ESTABLISHED
Sometimes more than one client connections could be seen.
I can confirm that the client is still running during this time. I tried to connect to this server socket through another client socket script.
The result says, client request made but server socket did not respond.
If restart the web server, and run the server script again, everyrthing works normal for sometime.
Could anyone help me identify the problem.

I ran with the same probleme, i fixed it by closing the resource built by socket_accept()
while ($con == 1)
{
$client = socket_accept($sock);
$input = socket_read($client, 50);
if ($input == 'exit')
{
$close = socket_close($sock);
$con = 0;
}
else {
$input = trim($input) . "," . date('Y-m-d H:i:s') . "\n";
$file= "/home/xyz/data/" . "uls_" . date("Ymd_His") . ".dat";
file_put_contents($file, $input);
}
// #param2 - 1 => block writing in socket ...
socket_shutdown ($client , 1) ;
socket_close ($client ) ;
}

Related

PHP sockets, can't connect - Loop?

Hello i'm trying to learn and build a socket script in PHP this is my code:
Client:
include_once('server.php');
$host = "localhost";
$port = 1025;
$message = "Hello Server";
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n";
}
// connect to server
socket_bind($socket, $host);
if (socket_connect($socket, $host, $port) === false) {
echo "socket_connect() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n";
}
else { echo "Connected"; }
Server:
// Variables
$host = "localhost";
$port = 1025;
// No timeout
set_time_limit(0);
// Create socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Can't create socket..");
if ($socket === false) {
echo "Unable to create socket. Error: {$errno} : {$errstr}\n";
die();
}
// Bind Socket
if (socket_bind($socket, $host) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n";
}
else { echo "Connected to {$host}:{$port}"; }
// Socket listen
if (socket_listen($socket, 5) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n";
}
if (socket_accept($socket) === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n";
}
// Socket close
socket_close($socket);
But i'm missing something that i cant figure out and i dont know what, When im trying to connect from the client it just loads nothing happens, Can anyone here point me in the right direction and tell me what im doing wrong?
On your client, you do not want socket_bind(). That is for opening up a connection for other systems to connect to; in short, to become a server.
Our client script:
<?php // client.php
$host = "127.0.0.1"; // connect _does_ do DNS lookups, unlike bind, which is mentioned below, but for simplicity of the example I'm explicitly naming the IP address.
$port = 1025;
$message = "Hello Server";
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
if ($socket === false) {
// Using die() here because we really don't want to continue on while in an error condition.
die('socket_create() failed: reason: ' . socket_strerror(socket_last_error($socket));
}
// connect to server
if (socket_connect($socket, $host, $port) === false) {
die('socket_connect() failed: reason: ' . socket_strerror(socket_last_error($socket));
}
socket_write($socket, $message);
socket_close($socket);
In your server, you should listen on 0.0.0.0 (or a specific IP address), and not specify a domain name. 0.0.0.0 means to listen to all IP addresses, and any specific IP address means to only listen on the one interface that is configured for that address.
For example, if your server has 2 NICs, one assigned to a public address, say 1.2.3.4, and another assigned to a local address, 192.168.0.2. You also always have your loopback, 127.0.0.1 for free just by having TCP/IP.
If you want to restrict access to your server script to only other hosts on your network, you would set your listen address to 192.169.0.2. Even though your server is accessible through the public IP of 1.2.3.4, this script will simply not listen to any of that traffic.
If you want to restrict access to your server script to only other processes running on that machine, you need to use 127.0.0.1. Using "localhost" will not keep anyone out, because bind does not perform any DNS lookups (and PHP's socket_bind() is only a thin wrapper around the BSD Sockets based system call to bind). It doesn't even look at the hosts file. Thus, any non-IP string will be cast to an integer, usually resulting in it becoming 0, and that 0 will be converted to 0.0.0.0, which will allow access from all interfaces, despite you thinking that you're only listening to localhost traffic.
When you accept a connection, it creates a new socket resource, which I've named $clientSocket in the example below. Don't get confused between the sockets created when a new host connects and your listening socket; they're very similar, but with a very important distinction: When your listening socket has a new message, it is always saying that there is a new host connecting, so you should accept. If it is a client socket, then you'll be using read or recv. (I prefer recv because of the finer control, but I use read in the example below to more clearly show the process rather than adding confusion by having references... which is also why I'm not going to show select.)
Our server script:
<?php //server.php
$listen_address = "0.0.0.0";
$port = 1025;
$maxBuffer = 1024; // bytes, not characters.
// No timeout
//set_time_limit(0); // Shouldn't be necessary in CLI.
// Create socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
die('Unable to create socket: ' . $socket_strerror(socket_last_error($socket)) . PHP_EOL);
}
// Bind Socket
if (socket_bind($socket, $listen_address, $port) === false) {
die('socket_bind() failed: ' . socket_strerror(socket_last_error($socket)) . PHP_EOL);
}
else {
echo "Connected to {$listen_address}:{$port}\n";
}
// Accept our client connection
// Typically, this would be where we'd put a loop with socket_select,
// but for our example, since we'll be exiting as soon as we have our
// first packet, we'll listen, accept, read, then close.
if (socket_listen($socket) === false) {
die('socket_listen() failed: ' . socket_strerror(socket_last_error($socket)) . PHP_EOL);
}
$clientSocket = socket_accept($socket);
if ($clientSocket === false) {
die('socket_accept() failed: ' . socket_strerror(socket_last_error($socket)) . PHP_EOL);
}
// Because the contents of a packet could be longer than our
// buffer size, it's advisable to peek at the socket to see
// if it would block before saying that our message is complete.
// In our example, we're receiving 12 bytes out of our maximum 1024,
// so I leave handling large packets as an exercise for the developer.
$message = socket_read($clientSocket, $maxBuffer);
var_dump($message);
// Socket close
socket_close($clientSocket);
socket_close($socket);
And finally, here's how to run the mess above.
Note that I'm going to have both the server and client run in the same TTY, I'll have the server run as a background process (use the & modifier), but I will not be redirecting their output, so both scripts will be spitting their output into the same terminal.
myhost.mydomain$ php ./server.php &
[8] 76399
Connected to 0.0.0.0:1025
myhost.mydomain$ php ./client.php
string(12) "Hello Server"
[8]+ Done php ./server.php
Your implementation of the server is wrong. Your server should have a loop which is always active listening to connections.
Server
# Create Socket
$socket = socket_create(AF_INET6, SOCK_STREAM, SOL_TCP);
# Bind a host and a port to the socket
socket_bind($socket, $host, $port);
# Listen for connection in the socket you just created
socket_listen($socket);
# This is the heart of the server, without it, it can not serve.
while(true)
{
# Accept Client connection if client socket connects
$client = socket_accept($socket);
# Logic #todo Reponse to client
print "Connected." // For testing purposes, see if client actually connected
# Close client connection
socket_close($client);
}
To connect from your client to the server use socket_connect, documentation here.

PHP script hangs up during socket programming

my simple php script simply hangs up when i see in firebug
<?php
require 'FirePHPCore/fb.php';
ob_start();
session_start();
FB::log('Log message');
//FB::info('Info message');
//FB::warn('Warn message');
//FB::error('Error message');
FB::info('i m inside');
$fileCount = $_POST['count'];
$data = "i-" . $fileCount;
FB::info('data for server ' . $data);
$address = '127.0.0.1';
$port = 5555;
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
socket_connect($socket, $address, $port);
FB::info('socket connect ');
$len = strlen($data);
$status = socket_sendto($socket, $data, $len, 0, $address, $port);
$input = socket_read($socket, 1024);
echo $input;
socket_close($socket);
FB::log('i m deadout..');
What is the reason behind this problem please help?
thanks. The server (java) running on same machine.
I found out this problem when i saw firebug it simply displays loading icon.
And when i remove the socket programming part it works properly.
php socket connections are opened in blocking mode by default and you need to set using socket_set_nonblock. For an example;
$this->soc = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_set_nonblock($this->soc);
or
$socket = socket_create_listen(1223);
socket_set_nonblock($socket);
socket_accept($socket);

Open asynchronous sockets in PHP

I am writing a port scanner in PHP that supports small ranges (e.g., ports 21-25). The ports and IP to be scanned are sent to the server via AJAX, and then PHP attempts to open a socket on each of the ports. If it succeeds, the port is open, if it times out, the port is closed.
Currently, despite sending all of the AJAX requests at the same time for ports 21-25, each socket is only opened after the last one closes. So, port 21 is checked, the socket is closed, and then port 22 is checked, and so on. What I want is for all ports to be checked concurrently, so I'd be opening several sockets at once.
I've tried:
$fp = #fsockopen($ip,$port,$errno,$errstr,2);
socket_set_nonblock($fp);
But this doesn't work, as I'm setting non-block AFTER the socket has already been opened and is waiting for a response. Is what I'm trying to do possible in PHP?
Use different functions: socket_create() and socket_connect() instead of fsockopen(). This works:
$socks = array();
for ($port = 21; $port <= 25; $port++) {
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_nonblock($sock);
#socket_connect($sock, 'localhost', $port);
$socks[$port] = $sock;
}
$startTime = microtime(true);
while ($socks && microtime(true) - $startTime < 3) {
$null = null;
$write = $socks;
socket_select($null, $write, $null, 1);
foreach ($write as $port => $sock) {
$desc = "$port/tcp";
$errno = socket_get_option($sock, SOL_SOCKET, SO_ERROR);
if ($errno == 0) {
echo "$desc open\n";
} elseif ($errno == SOCKET_ECONNREFUSED) {
echo "$desc closed\n";
} elseif ($errno == SOCKET_ETIMEDOUT) {
echo "$desc filtered\n";
} else {
$errmsg = socket_strerror($errno);
echo "$desc error $errmsg\n";
}
unset($socks[$port]);
socket_close($sock);
}
}
foreach ($socks as $port => $sock) {
$desc = "$port/tcp";
echo "$desc filtered\n";
socket_close($sock);
}

PHP Socket Disconnecting Randomly

I'm using a PHP Socket to listen on port 6000 for incoming connections and its working perfectly 99% percent of the time but 1% of the time the client is getting a connection error when sending a request to the server. I created a different script to ping the socket on port 6000 every second in an infinite loop and write the result to a log file so I can see if its breaking, and out of 78,000 Successful pings, 23 Failed.
There must be some small logic error with my code which is causing this. If anyone has any ideas its much appreciated.
Socket:
if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "Socket created \n";
// Bind the source address
if( !socket_bind($sock, "0.0.0.0" , 6000) )
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n");
}
echo "Socket bind OK \n";
if(!socket_listen ($sock , 10))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not listen on socket : [$errorcode] $errormsg \n");
}
echo "Socket listen OK \n";
echo "Waiting for incoming connections... \n";
//start loop to listen for incoming connections
while (true)
{
//Accept incoming connection - This is a blocking call
$client = socket_accept($sock);
//read data from the incoming socket
$input = "";
$input = socket_read($client, 10000000);
if ($input != "")
{
// do my logic here with $input
}
}
socket_close($sock);
EDIT: No, I'm not using CMD to ping. This is my PHP script which is doing the pinging:
<?php
$host = '0.0.0.0';
$port = 6000;
$waitTimeoutInSeconds = 1;
while(true)
{
if($fp = fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds))
{
$file = 'log.txt';
$current = file_get_contents($file);
$today = date("Y-m-d_H:i:s");
$current .= $today . " - SUCCESS\n";
file_put_contents($file, $current);
}
else
{
$file = 'log.txt';
$current = file_get_contents($file);
$today = date("Y-m-d_H:i:s");
$current .= $today . " - FAILED\n";
file_put_contents($file, $current);
}
fclose($fp);
sleep(1);
}
?>
For an actual transaction, the client is only connected for a split second while it sends through an xml request in raw text, then it does some logic which takes less than a second. Since its failing on the ping test though, that means my listener is breaking for a second for one reason or another does it not?
I'm not sure if I understand correctly.
You say about 0,03% of your pings failed. Is this your problem? If these are real pings (ping.exe from cmd.exe) then it has nothing to do with your logic. It's the network. (Is the host on WIFI?)
If you are convinced it's your logic:
If someone connects, how long is he connected? And what happens to new requests while connected to the previous client? I think you may find your answer here.
Also try to do a test with a client that continuously connects and sends dummy data, in stead of pinging. And log what responses the client is receiving.

unable to bind address [0]: php error

unable to bind address [0]: Only one usage of each socket address (protocol/network address/port) is normally permitted....
error is given by my php server page. I tried different port numbers as looking from cmd as writing netstat -an. Also I searched on google but no solution. I am using wamp server and working local .
Thanks .
<?php
// don't timeout
//echo phpinfo();
set_time_limit (0);
// set some variables
$host = "127.0.0.1";
$port = 1234;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
echo "Waiting for connections...\n";
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
echo "Received connection request\n";
// write a welcome message to the client
$welcome = "Roll up, roll up, to the greatest show on earth!\n? ";
socket_write($spawn, $welcome, strlen ($welcome)) or die("Could not send connect string\n");
// keep looping and looking for client input
do
{
// read client input
$input = socket_read($spawn, 1024, 1) or die("Could not read input\n");
if (trim($input) != "")
{
echo "Received input: $input\n";
// if client requests session end
if (trim($input) == "END")
{
// close the child socket
// break out of loop
socket_close($spawn);
break;
}
// otherwise...
else
{
// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output . "? ", strlen (($output)+2)) or die("Could not write output\n");
echo "Sent output: " . trim($output) . "\n";
}
}
} while (true);
// close primary socket
socket_close($socket);
echo "Socket terminated\n";
?>
Erm...this is running on a web page? If so, each hit to the page will cause the script to try to bind to port 1234, which ain't gonna happen for any but one at a time. All the others will die.
If it's not, then there are two reasons i can think of right off why binding would fail: either another program is already using the port, or the firewall is blocking it. The latter shouldn't be the case for 127.0.0.1, but i've seen stranger things happen.
The code as posted should work, at least it does here. Are you sure there is no firewalling thing preventing you from opening the socket?
It shouldn't matter much, but when opening the socket, specify the right protocol:
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
If that doesn't help, try a loop to find a listening port that may work; maybe the port is still blocked by your previous attempts.
for ( $port = 1234; $port < 65536; $port++ )
{
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
if ( $result )
{
print "bind succeeded, port=$port\n";
break;
} else {
print "Binding to port $port failed: ";
print socket_strerror(socket_last_error($socket))."\n";
}
}
if ( $port == 65536 ) die("Unable to bind socket to address\n");
If this solves your problem, you may want to do
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
before binding, to tell the system that it should allow reuse of the port.

Categories