PHP sockets unable to accept connection from the printer port - php

Issue at Hand: Intercepting a print request and modifying the data within the job to add content to it.
Solution so far: Here's the solution that has worked for the windows xp machines
1. Redirect the default printer to a raw TCP/IP port say 9100.
2. Write a basic socket server which listens on the port 9100 and accepts connections when they occur.
3. Read from the socket and modify the content before writing to a virtual printer which redirects to actual port that the default printer was on.
Problem: Steps 1 and 2 do not seem to work on windows 7.. Can someone please help..?
Here is the socket server code(very basic)...
<?php
// set some variables
$host = "127.0.0.1";
$port = 9100;
// don't timeout!
set_time_limit(0);
if(($sock = socket_create(AF_INET, SOCK_STREAM, 0)) < 0)
{
echo "failed to create socket: ".socket_strerror($sock)."\n";
exit();
}
if(($ret = socket_bind($sock, $host, $port)) < 0)
{
echo "failed to bind socket: ".socket_strerror($ret)."\n";
exit();
}
if( ( $ret = socket_listen( $sock, 0 ) ) < 0 )
{
echo "failed to listen to socket: ".socket_strerror($ret)."\n";
exit();
}
socket_set_nonblock($sock);
echo "waiting for clients to connect\n";
while (true)
{
$connection = #socket_accept($sock);
if ($connection === false)
{
usleep(100);
}elseif ($connection > 0)
{
//handle_client($sock, $connection);
}else
{
echo "error: ".socket_strerror($connection);
die;
}
}
So at this point, when a print job happens to a default printer, the server should accept the connection but that is not the case. A basic socket client which performs a socket_create, socket_connect and sends a socket_write works fine.. But the same thing does not happen when a print job to the same port is being sent. It works fine on windows XP. Can someone please help?

Found that on windows 7 IPV6 sockets are used by printers by default. If you run a microsoft fixit to prefer IPV4 over IPV6 or create a IPV6 socket it should work. – user2693294

Related

socket_listen(): unable to listen on socket [22]

I have lots of questions about PHP sockets.
I successfully done the socket_write() part, but not socket_listen()
As my socket_bind() didn't open any port (I am not sure if bind should open port or just bind ip's, but when I write my phone IP it told can't assign requested IP, and when I tried socket_connect it returned error[111], and when I checked with netstat and telnet the port wasn't open) I decided to open port with nc
nc -kl 47832
Then I wrote this in my test.php file
so the code checks if I am with my PC(localhost) it should connect to 127.0.0.1
and if I am with phone (not localhost) it connects to 192.168.43.124 (My PCs IP)
$forloop;
if ($_SERVER["HTTP_HOST"]=="localhost") {
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Err: " . socket_last_error());
socket_connect($socket, "127.0.0.1", 47832) or die("Err: " . socket_last_error());
$result = socket_listen($socket) or die("can't set up listener");
$spawn = socket_accept($socket) or die("err: can't accept");
$input = socket_read($spawn, 1024) or die("Could not read input\n");
$resinput = trim($input);
//echo socket_get_option();
socket_close($spawn);
socket_close($socket);
}
else {
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Err: " . socket_last_error());
socket_connect($socket, $_SERVER['HTTP_HOST'], 47832);
socket_write($socket, "needreload", 10) or die("Could not write output\n");
sleep(1);
socket_close($socket);
}
function whilefunc() {
$GLOBALS['forloop'] = 1;
if (!isset($GLOBALS['forloop'])) {
while ($GLOBALS['forloop'] == 1) {
if ($GLOBALS['input']=="needreload") {
//loadchat();
socket_successful();
}
}
}
}
function socket_successful() {
$GLOBALS['forloop']=0;
echo "<h2>msg recieved:" . $GLOBALS['input'] . "</h2>";
$resinput="";
whilefunc();
}
whilefunc();
Now when I open the page with phone in terminal text "needreload" appears, but
when I open it with PC it says can't set up listener (and that's because I didn't used bind, but if I used bind it would have told can't bind address already in use).
I changed my test.php a lot but still don't know what to do..
So my questions are
How to fix this using socket_bind() (as after I can't socket_connect, cause no port is being opened)
how to fix this (nc port opened method).
I know I wrote my question mixed, but that's because I am also confused.
For me adding this ob_implicit_flush(); to my code, made my socket accessible from telnet and nc.

PHP socket server closes when connect client to server

I'm coding a php socket server script which is running on ubuntu.
It works well, clients can connect/disconnect to this server.
But the server is shutting down silently when a client connect to this server that didn't have any client connected for a long time.
I setted up timeout to 0 (unlimited) but the server is stoping without any errors or messages... like this.
root#exfl:/home/projectap/sec/server_core# php socket_server.php
[exfl.kr] Starting AbsolutePitch socket server...
[2014.06.21 16:50:58] Server is listening. PID: 6442
[2014.06.21 17:28:48] Client [192.168.0.1:8795] connected to the server.
root#exfl:/home/projectap/sec/server_core#
I coded for this server like this.
<?php
set_time_limit(0);
error_reporting(E_ALL);
ini_set("memory_limit","2048M");
ini_set("max_execution_time", "0");
if(posix_getpid() == true) {
echo "[exfl.kr] Starting AbsolutePitch socket server...\n";
} else {
echo getTimeToStr()."Server is already running.\n";
}
$cSock = array();
$socketsInformation = array();
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$bind = socket_bind($socket, "0.0.0.0", "24180");
$listen = socket_listen($socket, 5);
echo getTimeToStr()."Server is listening. PID: ".posix_getpid()."\n";
while(true) {
$sockArr = array_merge(array($socket), $cSock);
if(socket_select($sockArr, $tWrite = null, $tExcept = null, null) > 0) {
foreach($sockArr as $sock){
if($sock == $socket){
$tSock = socket_accept($socket);
socket_getpeername($tSock, $sockIp, $sockPort);
array_push($cSock, $tSock);
array_push($socketsInformation, array("SOCKETINDEX"=>(count($cSock)-1), "IP"=>$sockIp, "USERID"=>"", "STATUS"=>"", "AUTH"=>false));
echo getTimeToStr()."Client [".$sockIp.":".$sockPort."] connected to the server.\n";
} else {
...
}
}
}
}
echo getTimeToStr()."Server closed.\n";
...
?>
When the server stops suddenly, it isn't outputing a "Server closed" message..
Please help.
Sorry for my bad English.
You may want to try socket_set_options() with SO_KEEPALIVE. To know more about your options use the socket_get_options page:
https://www.php.net/manual/en/function.socket-get-option.php
To know more about the usage of socket_set_options() use:
https://www.php.net/manual/en/function.socket-set-option.php

How to handle socket broken pipe [error 32] in php?

I have socket handler class, which is used to communicate to client with specific ip and port with the help of several socket functions. At the very first time when I am using writetosocket() function, it's working perfectly.
But when I am restarting client(with ip and port). And tries to use writetosocket() it returns me broken pipe error with error code 32. but after some successful execution of socket_write function. Means I am getting this error after some time duration, when I am writing data on socket. I read some solutions and tried most common solution where I am using socket_shutdown and socket_close to terminate socket connection properly whenever I am finding client is not responding. And after that I am again calling startconnection, which is giving me new socket. But still I am getting broken pipe error.
function startconnection(){
/* Create a socket in the AF_INET family, using SOCK_STREAM for TCP connection */
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($this->socket === false) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
echo "$errorcode : $errormsg";
return false;
}
else {
echo "Socket successfully created.";
}
/* Accept incoming connections */
$this->result = socket_connect($this->socket, $this->ipaddress, $this->port);
if($this->result === false){
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
echo "$errorcode : $errormsg";
return false;
}
else {
echo "successfully connected to $this->ipaddress, $this->port";
}
return true;
}
function writetosocket($input){
$sent = socket_write($this->socket, $input, strlen($input));
if($sent === false) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
echo "$errorcode : $errormsg";
return false;
}
else {
echo "Message Sent : $input";
}
return true;
}
Help me to understand and resolve this problem so that function can handle broken pipe error.
You are getting that error because the server socket has closed and is no longer listening and the client socket is attempting to send data to the server socket after it has been closed but before the port is free to be used again (while it is in TIME_WAIT).
The Server Socket and the Client Socket both go through different steps before they become available for I/O:
SERVER
socket()
bind()
listen()
accept()
Client
socket()
bind() [optional, see below]
connect() [does an implicit bind on an ephemeral port if not already bound]

Wanting to run a section of php code repeatedly but without losing scope

So I've been able to use AJAX to repeatedly refresh a div on my website, using the following code:
var $container = $("#content");
var refreshId = setInterval(function()
{
$container.load('toad.php').fadeOut("slow").load('response.php').fadeIn("slow"); ;
}, 2000);
This works fine, toad.php gets executed every 2 seconds and the contents update.
toad.php contains the following code currently:
<?php
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";
if (!socket_connect($sock, '127.0.0.1', 45000)) {
//$errorcode = socket_last_error();
//$errormsg = socket_strerror($errorcode);
echo "No sensor available to connect to.\n";
//die("Could not connect: [$errorcode] $errormsg \n");
}
echo "Connection established \n";
//Now receive reply from server
if (false !== ($bytes = socket_recv($sock, $buf, 1200, MSG_WAITALL))) {
echo "Read $bytes bytes from socket_recv(). Closing socket...";
}
else {
echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
socket_close($sock);
echo $buf . "\n";
echo "OK.\n";
?>
The issue with this is that the socket is created and connected to every single time the div is refreshed.
If I don't have the socket_create and socket_connect functions in toad.php then the socket_recv function doesn't know about the socket and doesn't work.
My question is how can I get the socket receive code to loop and update the contents of the div without having to re create and connect to the socket every time?
Thanks!
If you can rewrite the server your connecting to to use websocket, you could just connect directly from the browser using socket.io.
http://socket.io/
As others have said, maintaining a persistent PHP connection can be tricky, but if running a NodeJS (or similar) script to handle client connections isn't possible you could serve the websockets from that same PHP script.
Try this approach:
http://devzone.zend.com/209/writing-socket-servers-in-php/
Your toad.php script would run on the command line, listening to your existing socket server, then serving the data to it's own socket server which the browser connects to directly with websocket.

Why is my PHP Socket Server hanging?

I created a PHP Socket Server with PHP_NORMAL_READ mode. So, a message to the server is read when it ends with \n or \r. I tried it by connecting to the server with multiple telnet instances, and it works great.
However, when I connect to the server with 1 flash application and 1 telnet application (I first start the flash one), the flash one seems to make the server hang - the server is getting stuck somewhere and no longer receiving data from eg. the telnet client.
Because anyone can code a flash client, this has to be fixed server side. The server's code:
<?php
// config
$timelimit = 60; // amount of seconds the server should run for, 0 = run indefintely
$port = 9000; // the port to listen on
$address = $_SERVER['SERVER_ADDR']; // the server's external IP
$backlog = SOMAXCONN; // the maximum of backlog incoming connections that will be queued for processing
// configure custom PHP settings
error_reporting(1); // report all errors
ini_set('display_errors', 1); // display all errors
set_time_limit($timelimit); // timeout after x seconds
ob_implicit_flush(); // results in a flush operation after every output call
//create master IPv4 based TCP socket
if (!($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) die("Could not create master socket, error: ".socket_strerror(socket_last_error()));
// set socket options (local addresses can be reused)
if (!socket_set_option($master, SOL_SOCKET, SO_REUSEADDR, 1)) die("Could not set socket options, error: ".socket_strerror(socket_last_error()));
// bind to socket server
if (!socket_bind($master, $address, $port)) die("Could not bind to socket server, error: ".socket_strerror(socket_last_error()));
// start listening
if (!socket_listen($master, $backlog)) die("Could not start listening to socket, error: ".socket_strerror(socket_last_error()));
//display startup information
echo "[".date('Y-m-d H:i:s')."] SERVER CREATED (MAXCONN: ".SOMAXCONN.").\n"; //max connections is a kernel variable and can be adjusted with sysctl
echo "[".date('Y-m-d H:i:s')."] Listening on ".$address.":".$port.".\n";
$time = time(); //set startup timestamp
// init read sockets array
$read_sockets = array($master);
// continuously handle incoming socket messages, or close if time limit has been reached
while ((!$timelimit) or (time() - $time < $timelimit)) {
$changed_sockets = $read_sockets;
socket_select($changed_sockets, $write = null, $except = null, null);
foreach($changed_sockets as $socket) {
if ($socket == $master) {
if (($client = socket_accept($master)) < 0) {
continue;
} else {
array_push($read_sockets, $client);
}
} else {
$data = #socket_read($socket, 1024, PHP_NORMAL_READ); //read a maximum of 1024 bytes until a new line has been sent
if ($data === false) { //the client disconnected
$index = array_search($socket, $read_sockets);
unset($read_sockets[$index]);
socket_close($socket);
} elseif ($data = trim($data)) { //remove whitespace and continue only if the message is not empty
echo "we received: ".$data."\n\n";
//handleData($data, $socket);
}
}
}
}
socket_close($master); //close the socket
echo "[".date('Y-m-d H:i:s')."] SERVER CLOSED.\n";
//function to write to the flash client
function flash_write($socket, $msg) {
socket_write($socket, $msg.chr(0x0));
}
?>
Does anyone know what may cause this? I tried changing the timeout on the socket_select from none to 0 (instant return), but that didn't seem to change anything.
Could you post the source of the flash client? That would show what the problem is?
Are you sure the last thing you send from the flash client is a \n ?
Otherwise the server would block on socket_read() as the flash client socket can be read without blocking (triggered socket_select()), but doesn't send the ending \n.
One thing to help you debug: error_reporting(1) does not enable the display of all errors. Look at the documentation at http://us3.php.net/manual/en/function.error-reporting.php. You need something like error_reporting(E_ALL).

Categories