Receive Java Socket in PHP - 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!

Related

php UDP socket listener on windows

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

Read Socket in PHP, receive only Trash

i try to read gps data from my Teltonika router. I can receive data, but that are only trash for my eyes. So i cant parse that.
That are my received data: (the red is my IMEI Number...)
And that is my PHP:
<?php
require_once 'FMXXXX_Parser/Parser.class.php';
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_bind($sock, '10.81.0.1' , 62250) ) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n"); }
echo "Socket bind OK \n";
$remote_ip = '10.81.0.1';
$remote_port = 62250;
while(1) {
echo "Waiting for data ... \n";
$r = socket_recvfrom($sock, $buf, 4096, 0, $remote_ip, $remote_port);
echo "$remote_ip : $remote_port -- " . $buf;
die;
}
socket_close($sock);
Have somebody an idea?

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

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

udp socket programming in php

I'm trying to run a udp socket on windows cmd..here's my server script..
<?php
error_reporting(~E_WARNING);
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, '0.0.0.0' , 80) )
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n");
}
echo "Socket bind OK \n";
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 get a response on running the above script that reads as follows:
Socket created
Socket bind OK
Waiting for data ...
Below is my client script..
<?php
error_reporting(~E_WARNING);
$server = '127.0.0.1';
$port = 9999;
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";
while(1)
{
//Take some input to send
echo 'Enter a message to send : ';
$input = fgets(STDIN);
//Send the message to the server
if( ! socket_sendto($sock, $input , strlen($input) , 0 , $server , $port))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not send data: [$errorcode] $errormsg \n");
}
//Now receive reply from server and print it
if(socket_recv ( $sock , $reply , 2045 , MSG_WAITALL ) === FALSE)
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not receive data: [$errorcode] $errormsg \n");
}
echo "Reply : $reply";
}
On running the above socket, i get the following output..
Socket created
Enter a message to send:[random input]
Could not receive data:[0] The operation completed successfully
What's causing this?..after i enter some input, the last line above is displayed instead of 'Reply : [random input]'
Thanks in advance
use this flag "MSG_PEEK" instead of "MSG_WAITALL" in socket_recv function.
Thank you.

Categories