PHP/UDP: Can write via UDP, but recieving data seams not possible - php

I have this code which is running on php on my local mac:
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$result = socket_connect($socket, $address, $port);
$msg = "i";
$len = strlen($msg);
socket_send($socket, $msg, $len, 0);
socket_recv($socket, $buf, 512, 0);
echo $buf;
socket_close($socket);
The code works up to "socket_recv". Actually, when running a udp test app on the computer, I can see the answer from the udp server on the terminal screen of the app. However, the script itself is running for ever (I assume that it is waiting for input which never comes through the socket for some reason).
I also tried to run the script on a local Webserver on a synology NAS. Same result.
(Same result with the UDP App obviously closed)

Problem solved by using this code. Apparently one has to close the socket after sending and open a new one for receiving. It seams to be a unique behaviour of this device (server):
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$bytes = socket_sendto($socket, $message, strlen($message), 0, "192.168.0.37", 7090);
socket_close($socket);
$message="curr 7700";
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($socket, '0.0.0.0', 7090);
$from = '';
$port = 0;
socket_recvfrom($socket, $buf, 512, 0, $from, $port);
echo $buf . PHP_EOL;

Related

Send an http request through UDP

I need to send a simple http request to trigger an event through UDP protocol, I don't need to receive the response
Is it possible and how can I do it with php
I think it should help you:
$server_ip = '127.0.0.1';
$server_port = 43278;
$message = 'Message';
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_sendto($socket, $message, strlen($message), 0, $server_ip, $server_port);
socket_close($socket);
More informations you can get at PHP MANUAL: socket_sendto

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

get UDP Multicast stream in PHP

I am trying to receive a UDP multicast stream in PHP. The receive command never gets anything and waits forever.
I can watch the stream using VLC player so the stream is accessible on my machine. Any help on how to do this using PHP is highly appreciated.
Here is my code.
<?php
error_reporting(E_ALL | E_STRICT);
//create a new socket
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
//i am not sure about this command. I think i have to set this option to start receiving packets.
socket_set_option($socket, SOL_SOCKET, MCAST_JOIN_SOURCE_GROUP, array("group"=>"239.194.0.73","interface"=>"eth0","source"=>"239.194.0.73"));
$binded = socket_bind($socket, '127.0.0.1', 6073);
//receive data
$from = '';
$port = 0;
socket_recvfrom($socket, $buf, 12, MSG_WAITALL, $from, $port);
echo "Received $buf from remote address $from and remote port $port" . PHP_EOL;
?>
$binded = socket_bind($socket, '127.0.0.1', 6073);
Should be
$binded = socket_bind($socket, '0.0.0.0', 6073);
Or else you will only recv packets originating from the local host.

How to read php socket connection response?

Following is my socket connection request and response order.
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
$connection = socket_connect($socket, $Host, $Port);
$Md5CheckSum = md5($msg);
$WillWait = 'SOAP '. $Md5CheckSum. ' WILL_WAIT'."\n";
socket_write($socket,$WillWait);
socket_write($socket,$msg);
socket_write($socket, SoapSender::$TERM_CHAR);
sleep(1);
$buf = socket_read($socket, 2048);
//socket_write($socket,"&\r\n");
echo "$buf\n";
Please could somebody tell me how to read response that I receive after last socket_write request. I have been searching for this answer all day but have not been able to find any help through Google.
Thanks a lot for your time.
Two functions should be used:
stream_set_blocking($socket, true);
And
stream_get_contents($socket);
Setting a block on your stream requires the return of data before your application will continue execution of the script.
If you do not set a stream block, sometimes latency will cause your PHP script to think there was no response, causing you to not receive data.
Also, use stream_get_contents to pull from the socket. This will grab by default the full buffer.
The correct way is to use socket_read, not stream_get_contests as someone else suggested.
Here is an example:
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$result = socket_connect($sock, "10.197.24.40", "5000");
$request = '{ "request" : { "id" : "some_function_id", "data": "55555555-5"} }';
// We send the request
socket_write($sock,$request);
socket_read($sock,1000000);
socket_close($sock);
I have tested this code in a live environment and it works correctly.

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