udp socket programming in php - 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.

Related

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?

PHP socket shoud return json but returns warning

Never used socket before. I have to make socket to get an answer from server in json format. This is code:
$host = '11.11.11.1';//for example
$port = 1111;
$message = "xgm";
set_time_limit(2);
if(!($sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "Socket created \n";
if(!socket_connect($sock , $host , $port))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not connect: [$errorcode] $errormsg \n");
}
echo "Connection established \n";
if( ! socket_send ( $sock , $message , strlen($message) , 0))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not send data: [$errorcode] $errormsg \n");
}
echo "Message send successfully \n";
if(socket_recv ( $sock , $buf , 2045 , MSG_WAITALL ) === FALSE)
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not receive data: [$errorcode] $errormsg \n");
}
var_dump($buf);
socket_close($sock);
And this is response I get:
Socket created
Connection established
Message send successfully
Warning: socket_recv(): unable to read from socket [10045]: The attempted operation is not supported for the type of object
referenced. in C:\wamp64\www\json\index.php on line 73
Could not receive data: [10045] The attempted operation is not supported for the type of object referenced.
So it seems socket_recv returns false. why? Is that because of json object in response from server? And how to fix that? Any directions?
UDP unlike TCP is a connectionless protocol, which means that two ends can exchange messages without having to establich a communication channel.
So, it's best to use socket_recvfrom and socket_sendto because they work well with not connection-oriented sockets.
$host = '11.11.11.1';//for example
$port = 1111;
$message = "xgm";
set_time_limit(2);
if(!($sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP))){
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "Socket created \n";
if(!socket_sendto($sock, $message, strlen($message), 0, $host, $port)){
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not send data: [$errorcode] $errormsg \n");
}
echo "Message send successfully \n";
if(socket_recvfrom($sock , $buf , 2045 , 0, $host, $port) === FALSE){
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not receive data: [$errorcode] $errormsg \n");
}
var_dump($buf);
socket_close($sock);

Socket created Enter a message to send : Notice: Use of undefined constant STDIN

Socket created Enter a message to send :
Notice: Use of undefined constant STDIN - assumed 'STDIN' in C:\xampp\htdocs\upload\client.php on line 28
Could not send data: [0] The operation completed successfully.
Server.php (run on CMD)
<?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";
if( !socket_bind($sock, "0.0.0.0" , 9999) )
{
$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";
$r = socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);
echo "$remote_ip : $remote_port -- " . $buf;
socket_sendto($sock, "OK " . $buf , 100 , 0 , $remote_ip , $remote_port);
}
socket_close($sock);
?>
Client.php (Run on Browser)
<?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)
{
echo 'Enter a message to send : ';
$input = fgets(STDIN);
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");
}
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";
}
?>
when running both of server.php and client.php on cmd work fine, but when the client run on browser its show me like that, how to fix it?

Exchange data between 2 socket clients using php

I'm developing a new socket based application for interacting with my clients and I want to achieve that using socket programming.
Let's say my server socket is running on 127.0.0.1 on 8888 port. when a client connected to my server & if he trying to send a message to admin where admin is already connected from a different socket client.
But I'm facing a problem exchanging data between admin & client sockets. However the server socket is accepting the data from the admin & client as well. When I'm trying to send client data to admin its not passing over.
server.php
<?
set_time_limit(0);
// Set the ip and port we will listen on
$address = '127.0.0.1';
$port = 8888;
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0) or die('Could not bind to address'); // 0 for SQL_TCP
// Bind the socket to an address/port
socket_bind($sock, 0, $port) or die('Could not bind to address'); //0 for localhost
// Start listening for connections
socket_listen($sock);
$admin = null;
$clients = array();
while(true) {
/* Accept incoming requests and handle them as child processes */
$client = socket_accept($sock);
if(socket_getpeername($client, $address, $port)) {
echo "Client $address : $port is now connected to us. \n";
}
// Read the input from the client – 2MB bytes
$input = socket_read($client, 2097152);
$data = json_decode($input);
print_r($data);
if($data->connection === "admin"){
echo "Admin connection\n";
$admin = $client;
}else if($data->connection === "client"){
echo "Got data from client\n";
$message = array('client_id' => $data->id, 'message' => $data->message);
$clients[$data->id] = $client;
if($admin !== null){
echo "Sending data to admin\n";
socket_write($admin, json_encode($message), strlen(json_encode($message)));
}else{
echo "Admin not available, sending response to client\n";
$message = 'Admin not available!';
socket_write($client, $message, strlen($message));
}
}
print_r($clients);
// socket_write($client, $response);
// socket_close($client);
}
// Close the master sockets
socket_close($sock);
?>
admin.php
<?
set_time_limit(0);
$server = '128.199.XXX.XXX';
$port = '8888';
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";
//Connect socket to remote server
if(!socket_connect($sock, $server, $port)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not connect: [$errorcode] $errormsg \n");
}
echo "Connection established \n";
$message = array('connection' => 'admin');
//Send the message to the server
if(!socket_send($sock, json_encode($message), strlen(json_encode($message)), 0)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not send data: [$errorcode] $errormsg \n");
}
echo "Message send successfully \n";
if(socket_recv($sock, $buf, 10, MSG_WAITALL) === FALSE) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not receive data: [$errorcode] $errormsg \n");
}
print_r(json_decode($buf));
?>
client.php
<?
set_time_limit(0);
$server = '128.199.XXX.XXX';
$port = '8888';
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";
//Connect socket to remote server
if(!socket_connect($sock, $server, $port)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not connect: [$errorcode] $errormsg \n");
}
echo "Connection established \n";
$message = array('connection' => 'client', 'id' => time(), 'message' => 'Hi');
//Send the message to the server
if(!socket_send($sock, json_encode($message), strlen(json_encode($message)), 0)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not send data: [$errorcode] $errormsg \n");
}
echo "Message send successfully \n";
if(socket_recv($sock, $buf, 10, MSG_WAITALL) === FALSE) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not receive data: [$errorcode] $errormsg \n");
}
print_r(json_decode($buf));
?>
Can anybody done this before? lease help me to achieve it.
Thanks in advance :-)

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

Categories