Connect to listening netcat using PHP - php

I have a netcat server, running from command line:
netcat -l -q0 -p 9999 -c "tesseract stdin stdout -l spa"
I can connect to this server using netcat client:
cat /tmp/autogenerado.jpg | netcat 127.0.0.1 9999
So, I send a jpeg file to another server, and receive the OCR'ed data in client.
I want to do this in PHP:
$fp = fsockopen("tcp://127.0.0.1:9999", $errno, $errstr);
if (!$fp) {
echo "{$errno}: {$errstr}\n";
die();
}
fwrite($fp, $jpeg);
$contents = stream_get_contents($fp);
fclose($fp);
return $contents;
But the script hangs on stream_get_contents. I think it is because the netcat server doesn't know when the PHP client has finished sending data.
Is it possible to send an EOF ?

You need to shutdown the socket for writing, something like this:
<?php
$stream = stream_socket_client("tcp://whatever:9999", $errno, $errstr);
if (!$stream) {
echo "{$errno}: {$errstr}\n";
die();
}
fwrite($stream, $jpeg);
stream_socket_shutdown($stream, STREAM_SHUT_WR); /* This is the important line */
$contents = stream_get_contents($stream);
fclose($stream);
return $contents;
?>

For reference, final working code is:
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$result = socket_connect($sock, "10.187.252.35", 9999);
socket_send($sock,$bdata,strlen($bdata),MSG_EOR):
socket_shutdown ($sock,1);
while ($out = socket_read($sock, 2048)) {
echo $out;
}
socket_close($sock);

Related

send and get data from udpsocket

I'm working on a Server list viewer
and somebody told me that i should use UdpSocket to send data to the master server and receive data from it (in this case for MW2-IW4 game)
So this is the command or the socket you use
"\xff\xff\xff\getservers IW4 <MASTERSERVERPORTHERE> full empty\x00"
so i tried to work with this code
<?php
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$msg = "\xff\xff\xff\getservers IW4 <MASTERSERVERPORT> full empty\x00";
$len = strlen($msg);
socket_sendto($sock, $msg, $len, 0, 'MASTERSERVERIP' , 1223);
socket_close($sock);
?>
but didn't receive any data , just getting the number 36 ...
PS:
i don't know much things about sockets.
i figured out how to do it a while ago and remembered this post, if anybody is still interested, this is how you do it:
<?php
$ip = "127.0.0.1";
$port = 1337;
$socket = fsockopen('udp://'.$ip, $port);
stream_set_blocking($socket, 0);
stream_set_timeout($socket, 1); //1 = timeout
fwrite($socket, "YOUR UDP COMMAND HERE\n");
$time=time()+1; //1=timeout
$returned = "";
while($time > time()) {
$returned .= fgets($socket);
}
echo($returned); // the returned value
#fclose($socket); //we are done, so better close it.
?>
Just use fsockopen it's a lot easier,
$fp = fsockopen("udp://127.0.0.1", 13, $errno, $errstr);
if (!$fp) echo "ERROR: $errno - $errstr<br />\n";
fwrite($fp, "\n");
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);

PHP UDP fsockopen fread (RCON)

I want to get a message back after succesfully connected with an UDP address via fsockopen. Connection with a RCON ModManager (game server).
What to expect?
If I use Telnet:
Telnet 31.204.131.9 15502 , I see:
ModManager Rcon v8.5
Digest seed: iJrrQAkv
Now in PHP:
<?php
$_ip = '31.204.131.9' ;
$_port = '15502';
if (($socket = fsockopen ('udp://'.$_ip, $_port, $errno, $errstr, 30))) {
// till here it works, got connected
// Digest seed?
if(fwrite($socket, "GET / HTTP/1.1\r\n")) { // writing works }
echo fread($socket, 1024); // NOTHING
fclose($socket);
}
?>
You can try it.. IP and Port valid. Thnx in advance!!
When you use telnet you use tcp protocol, not udp. Try
<?php
$_ip = '31.204.131.9' ;
$_port = '15502';
if (($socket = fsockopen ($_ip, $_port, $errno, $errstr, 30))) {
// till here it works, got connected
// Digest seed?
if(fwrite($socket, "GET / HTTP/1.1\r\n")) { echo "writing works\n"; }
echo fread($socket, 1024); // NOTHING
fclose($socket);
}

using telnet via PHP on localhost(127.0.0.1) over port 11300

I have problem to use telnet via PHP on localhost(127.0.0.1) over port 11300
I haven't been using this before, so help would be great.
Here is the code:
function sendToSocket($host, $port, $out){
if(!function_exists('fsockopen'))
return 'f() doesnt exist !';
$response = "";
$fp = fsockopen($host, $port, $errno, $errstr);
if(!$fp){
$response .= "$errstr ($errno)<br/>";
}else{
fwrite($fp, $out);
$response .= fgets($fp);
fclose($fp);
}
if($response)
return $response;
else
return "ERROR";
}
echo sendToSocket('127.0.0.1', 11300, 'stats');
I got the "ERROR", thats means that fgets($fp); doesn't work for me.
When typing in Command Line: telnet 127.0.0.1 11300 everything is OK, so then I can type command "stats" in order to get the result. I am using Ubuntu.
Where is the mistake?
How to get result like result in Command Line ?
This script reads only one line from the response.
$response .= fgets($fp);
have to be replaced with something like
while (!feof($fp)) {
$response .= fgets($fp);
}
I corrected code to:
echo sendToSocket('127.0.0.1', 11300, "stats\r\n");
and it's working, everything is OK.
Note: the code doesn't work if you send single quote as output.

php socket server disconnecting

I'm trying to set up a socket server in php that stays open. This example taken from php.net will close after it receives connection...even after I comment out socket_close($spawn)
<?
// set some variables
$host = "192.168.1.109";
$port = 1234;
// don't timeout!
set_time_limit(0);
// 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");
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming
connection\n");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
$input = trim($input);
// reverse client input and send back
//$output = $input . "\n";
$output = strrev($input) . "\n";
echo $input;
socket_write($spawn, $output, strlen ($output)) or die("Could not write
output\n");
// close sockets
//socket_close($spawn);
//socket_close($socket);
?>
and here is the code for the client connecting...
<?php
$fp = fsockopen("192.168.1.109", 1234, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
//$out = "testing";
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: 127.0.0.1\r\n";
$out .= "Connection: Close\r\n\r\n";
$out .= "testing\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
//exit();
}
//exit;
?>
socket_read without the O_NONBLOCK flag (see socket_set_nonblock) is a blocking operation, so it will wait there until it receives something.
As soon as something is received, the rest of the script continues, and exits, as there is no loop in place to do the next read. (i.e: in a server it's usual to do a while(true){} loop)
You need to wrap the accept in a loop or something. It closes because the script execution has ended.
You could do something like this:
while ($spawn = socket_accept($socket)) {
//do stuff
}

Is there a way to specify the source UDP port using PHP?

I need to send a message via UDP to a remote equipment. I know that if I use the same UDP port that it used while sending information to the server to write back to it the messages goes through.
I am currently using:
$fp = fsockopen( $destination, $port, $errno, $errstr);
if (!$fp) {
echo "ERROR: $errno - $errstr\n";
} else {
fwrite($fp, $m);
fclose($fp);
}
But in this way I have no control of which port is going to be used as the source port.
In Java have one can use:
client = new DatagramSocket(21000);
Is there a way to do something similar using PHP.
You could do it by creating a normal udp socket with socket_create() and using socket_bind() to bind it to a specific port. Then use e.g. socket_sendto for specifying the endpoint and port to send it to. Example code follows.
A simple server that spits out the port number and ip address of client using socket_stream_server():
<?php
set_time_limit (20);
$socket = stream_socket_server("udp://127.0.0.1:50000",
$errno, $errstr,
STREAM_SERVER_BIND);
if (!$socket) {
die("$errstr ($errno)");
}
do {
$packet = stream_socket_recvfrom($socket, 1, 0, $peer);
echo "$peer\n";
stream_socket_sendto($socket, date("D M j H:i:s Y\r\n"), 0, $peer);
} while ($packet !== false);
?>
and the client is like this:
<?php
$address = '127.0.0.1';
$port = 50001;
$dest_address = '127.0.0.1';
$dest_port = 50000;
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed:" . socket_strerror(socket_last_error($sock)) . "\n";
}
$msg = "Ping !";
socket_sendto($sock, $msg, strlen($msg), 0, $dest_address, $dest_port);
socket_close($sock);
?>
Running the server (on the command line) gives this output when running the client multiple times:
<knuthaug#spider php>php server.php
127.0.0.1:50001
127.0.0.1:50001
127.0.0.1:50001
^C
<knuthaug#spider php>

Categories