php UDP socket listener on windows - php

I want to make udp listener on windows but I failed to do so, I tried couple of codes which available online but it didn't work for me, it seems that my windows php configuration is not done properly so code is not able to connect to socket, the code keeps waiting for messages but it receive nothing, I tested my network using two PCs with ready UDP sender/receiver app, and it works fine,
please check my code below and the output, please let me know if I should do something in php.ini
<?php
//Reduce errors
error_reporting(~E_WARNING);
//Create a UDP socket
if(!($sock = socket_create(AF_INET, SOCK_DGRAM, 0)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "Socket created \n";
// Bind the source address
if( !socket_bind($sock, "127.0.0.1" , 1235) )
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n");
}
echo "Socket bind OK \n";
//Do some communication, this loop can handle multiple clients
while(1)
{
echo "Waiting for data ... \n";
//Receive some data
$r = socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);
echo "$remote_ip : $remote_port -- " . $buf;
//Send back the data to the client
socket_sendto($sock, "OK " . $buf , 100 , 0 , $remote_ip , $remote_port);
}
socket_close($sock);
cmd php code with output

Related

A UDP server on PHP, troubles with ports

Im trying to create a simple UDP server on php which initially will be receiving some dummy data. I've tried following some tutorials and now i can create and bind a socket. However when im using socket_recvfrom for some reason the page just keeps loading and when i refresh it again, the port is taken and i cant bind the socket in that port anymore. below is my code and any answer is welcomed.
`
if(!($sock=socket_create(AF_INET, SOCK_DGRAM, 0)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldnt create socket: [$errorcode] $errormsg \n");
}
echo "socket create OK";
if (!socket_bind($sock,"localhost",8880))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldnt bind socket: [$errorcode] $errormsg \n");
}
echo "socket bind OK";
// while(1)
// {
$remote_ip ='';
$remote_port=0;
echo "waiting for data ... \n";
$r=socket_recvfrom($sock, $buf, 512 , 0 , $remote_ip , $remote_port);
echo "$remote_ip : $remote_port --". $buf;
// }
// socket_close($sock);
I ended up using this function:
if (false ===($tcpsock = stream_socket_server("tcp://127.0.0.1:8876",$errorno,$errstr)))
echo"tcp socket failed : $errstr($errorno)\n";
if(false ===($connection = stream_socket_accept($tcpsock)))
echo "tcp accept failed!\n";
else echo "tcp accepted";
And if you want a UDP socket, you just have to :stream_socket_server("udp://127.0.0.1:8876",$errorno,$errstr)))

socket server not working with command lines

<?php
if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "Socket created \n";
// Bind the source address
if( !socket_bind($sock, "127.0.0.1" , 5000) )
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n");
}
echo "Socket bind OK \n";
if(!socket_listen ($sock , 10))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not listen on socket : [$errorcode] $errormsg \n");
}
echo "Socket listen OK \n";
echo "Waiting for incoming connections... \n";
//Accept incoming connection - This is a blocking call
$client = socket_accept($sock);
//display information about the client who is connected
if(socket_getpeername($client , $address , $port))
{
echo "Client $address : $port is now connected to us.";
}
socket_close($client);
socket_close($sock);
?>
This code not working with command line.
No output is display in commend line after enter
But when i try in browser it works in infinite loop.
May be this is fine or another way to run a service in php
These codes build a tcp server,You can use telnet or netcat to access the server.
You also can use browser to access the server,like "http://127.0.0.1:5000/".Now you will see the command line output.
socket_accept blocks the proccess,until a request reach.You can visit this page for more information.

PHP UDP Server with Arduino as Client

I have programmed arduino to send massage to udp port 8888 when button pressed, and is working fine when I used third party software such as Socket test, but I am trying to use below php code to receive the massage.
The code(Not my Code) works, but the result show up when we get fatal errors and not when I send the massage(Press Button)
Below is sample of result(all result show together and not when I press Button):
Socket created Socket bind OK Waiting for data ... 10.10.1.48 : 8888 --YellowWaiting for data ... 10.10.1.48 : 8888 -- GreenWaiting for data ... 10.10.1.48 : 8888 -- YellowWaiting for data ... 10.10.1.48 : 8888 -- GreenWaiting for data ...
Fatal error: Maximum execution time of 120 seconds exceeded in C:\xampp\htdocs\Push\udp.php on line 38
Below is the code:
if(!($sock = socket_create(AF_INET, SOCK_DGRAM, 0)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "Socket created \n";
if ( ! socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1))
{
echo socket_strerror(socket_last_error($sock));
exit;
}
// Bind the source address
if( !socket_bind($sock, "0.0.0.0" , 8888) )
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n");
}
echo "Socket bind OK \n";
//Do some communication, this loop can handle multiple clients
while(1)
{
echo "Waiting for data ... \n";
//Receive some data
$r = socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);
echo "$remote_ip : $remote_port -- " . $buf;
usleep(100000); // debuging purpose
ob_flush();
flush();
//Send back the data to the client
socket_sendto($sock, "OK " . $buf , 100 , 0 , $remote_ip , $remote_port);
}
socket_close($sock);

Reading UDP Packets from PHP with my browser

I tried to make a php script to reading udp packets from browser but I just make one that only I can use it with cmd with ( php filename.php ), my question is how I can read the same way from my browser to get packets!
my php script I use:
<?php
//Reduce errors
error_reporting(~E_WARNING);
//Create a UDP socket
if(!($sock = socket_create(AF_INET, SOCK_DGRAM, 0)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "Socket created \n";
// Bind the source address
if( !socket_bind($sock, "127.0.0.1" , 2223) )
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n");
}
echo "Socket bind OK \n";
//Do some communication, this loop can handle multiple clients
while(1)
{
echo "Waiting for data ... \n";
//Receive some data
$r = socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);
echo $buf;
//echo "$remote_ip : $remote_port -- " . $buf;
//Send back the data to the client
//socket_sendto($sock, "OK " . $buf , 100 , 0 , $remote_ip , $remote_port);
}
socket_close($sock);
?>

Receive Java Socket in PHP

I've got the following problem:
I just created a new Socket connection and told my mediaRecorder to send its recorded data via socket:
Socket soc=null;
try {
soc = new Socket("192.168.178.103",8888);
Log.i(TAG, "Connected to socket");
} catch (Exception e) {
}
ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(soc);
myCamera = getCameraInstance();
mediaRecorder = new MediaRecorder();
myCamera.unlock();
mediaRecorder.setCamera(myCamera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
mediaRecorder.setOutputFile(pfd.getFileDescriptor());
.
.
.
I have the following php test code from http://www.binarytides.com/udp-socket-programming-in-php/:
<?php
//Reduce errors
error_reporting(~E_WARNING);
//Create a UDP socket
if(!($sock = socket_create(AF_INET, SOCK_DGRAM, 0)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "Socket created \n";
// Bind the source address
if( !socket_bind($sock, "192.168.178.103" , 8888) )
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n");
}
echo "Socket bind OK \n";
//Do some communication, this loop can handle multiple clients
while(1)
{
echo "Waiting for data ... \n";
//Receive some data
$r = socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);
echo "$remote_ip : $remote_port -- " . $buf;
//Send back the data to the client
socket_sendto($sock, "OK " . $buf , 100 , 0 , $remote_ip , $remote_port);
}
socket_close($sock);
I just tested around when i just write something like "procedured" in a file when my index.php on the server side is called. It works, but with this code my socket does not receive any data.
Do you have a clue for me?
Thank you!

Categories