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.
Related
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;
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
I am newbie in using PHP Websocket.
Here is my problem:
I have already successful running PHP websocket and creating a simple chat application(web application of course). In receiving client data. All of the clients will receive the data. How could I send data to a specified client or maybe to a several clients(not all clients).
I learn it at here
<?php
// Create a new socket
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// An example list of IP addresses owned by the computer
$sourceips['kevin'] = '127.0.0.1';
$sourceips['madcoder'] = '127.0.0.2';
// Bind the source address
socket_bind($sock, $sourceips['madcoder']);
// Connect to destination address
socket_connect($sock, '127.0.0.1', 80);
// Write
$request = 'GET / HTTP/1.1' . "\r\n" .
'Host: example.com' . "\r\n\r\n";
socket_write($sock, $request);
// Close
socket_close($sock);
?>
SOURCE: http://php.net/manual/en/function.socket-bind.php
I want to talk with php server with socket by telnet.
I wrote 'echo' server (i send string to server, server send it to me)
i use chr(0) on end of output string to send information that string is sent
socket_write($client, $output.chr(0));
but telnet haven't see it and i cant send new string
TELNET
telnet 127.0.0.1 9000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
hello
hello_
PHP
<?php
set_time_limit (0);
$address = '127.0.0.1';
$port = 9000;
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($sock, $address, $port) or die('Could not bind');
socket_listen($sock);
while(true) {
$client = socket_accept($sock);
$input = trim(socket_read($client, 1024));
if ($input == 'off') break;
$output = $input.chr(0);
socket_write($client, $output);
}
socket_close($client);
socket_close($sock);
?>
what i'm doing wrong?
You should end the lines with carriage return or "\r\n", instead of just the \0 character. The telnet client watches out for them too I think.
The real problem however is your use of $client = socket_accept(..) within the loop. You must only establish the connection once, before the while. Otherwise you will reset the connected stream.
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>