svalivarthi
svalivarthi

Reputation: 21

PHP sockets unable to accept connection from the printer port

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?

Upvotes: 0

Views: 1178

Answers (1)

Armali
Armali

Reputation: 19395

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

Upvotes: 0

Related Questions