send and get data from udpsocket - php

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

Related

Wondering if there's a way to stop an mjpeg stream being output by PHP

I am outputting an MJPEG stream through PHP, so that the direct link to the stream does not appear publicly on a website (I just link to this file as an intermediary).
The code works, and is as follows:
header('Content-type: video/x-motion-jpeg');
$server = "server.com";
$port = "8080";
$url = "/mjpg/Cam1/video.mjpg";
set_time_limit(30); // Doesn't affect the streaming timeout
$fp = fsockopen($server, $port, $errno, $errstr, 30); // The timeout set here doesn't affect the streaming timeout
if (!$fp) {
echo "$errstr ($errno)<br>\n";
} else {
$urlstring = "GET ".$url." HTTP/1.0\r\n\r\n";
fputs ($fp, $urlstring);
while ($str = trim(fgets($fp, 4096))){
header($str);
}
fpassthru($fp);
fclose($fp);
}
What I am wondering is if there's a way I can kill this stream (server-side, not client-side) after a certain duration (ex: 5 minutes).
I'm open to using different code/functions if required, or approaching this in a different way.
Based on the suggestion from #Barmar , I wrote a loop that calls fread() and checks the time in each iteration until the timeout is reached. So far it's doing the trick.
The code is as follows:
$timeout = 5; // seconds
$fp = fsockopen($server, $port, $errno, $errstr, 5); // The timeout set here doesn't affect the streaming timeout
if (!$fp) {
echo "$errstr ($errno)<br>\n";
} else {
$urlstring = "GET ".$url." HTTP/1.0\r\n\r\n";
fputs ($fp, $urlstring);
while ($str = trim(fgets($fp, 4096))){
header($str);
}
$start = time();
$now = time();
while ( $now - $start < $timeout ) {
echo fread( $fp, 4096 );
$now = time();
}
fclose($fp);
}

reading from external https path by php

I want to read some phrases from an external website which protocol is https.
I've do this for websites with http protocol by this code:
$homepage = file_get_contents('https://www.examlple.com/');
echo $homepage;
but it does not work for https sites. then I used this one:
$fp = fsockopen("https://www.example.com/", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "Data sent by socket");
$content = "";
while (!feof($fp)) { //This looped forever
$content .= fread($fp, 1024);
}
fclose($fp);
echo $content;
}
but I always get an error:
Unable to find the socket transport "http" - did you forget to enable
it when you configured PHP? (2)
actualy the case is to fetch my site's statistics from analytics.
Fsockopen does not know about "http, https". Please remove the http part from your domain and use SSL Port for connection.
See example, this should work:
// open ssl connection - dont add "http or https!"
$host = "ssl://" . "example.com";
$port = 443;
$fp = fsockopen($host,$port);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "Data sent by socket");
$content = "";
while (!feof($fp)) { //This looped forever
$content .= fread($fp, 1024);
}
fclose($fp);
echo $content;
}

Php read socket stream update

I connect to an application for a push/stream data.
With the following code I have a right answer from the app (outcome=OK|item=THEITEM) but can't figure out how to get the var "last_value" and its updates.
The loop is commented out, I have no echo with it (loop)
$port = ('5333');
$address = ('127.0.0.1');
$fp = stream_socket_client("tcp://$address:$port", $errno, $errstr, 1);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$in = "function=subscribe|item=THEITEM|schema=last_value";
fwrite($fp, $in."\n");
//while (!feof($fp)) {
echo fgets($fp, 1024);
//}
fclose($fp);
}
This returns outcome=OK|item=THEITEM.
The updates should be like THEITEM|last_value.

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