connect multiple device android to the same php socket - php

My problem is when I try to connect the Android application with Java socket identified by an IP and a port 4444 with a PHP service, identified by the same port, and keep this connection open to accept multiple connections via the Android app.
So with a second Android device if we connect to PHP we will have the same communication channel as the one device and we cannot identify which device is connected via PHP.
Is there a possibility to differentiate these two connections via the PHP file or instantiate two different PHP service for the two devices.
The Android code:
public String connect() {
Socket socket = null;
try {
socket = new Socket();
// This limits the time allowed to establish a connection in the case
// that the connection is refused or server doesn't exist.
socket.connect(new InetSocketAddress(dstAddress, dstPort), context.getResources().getInteger(R.integer.time_out));
// This stops the request from dragging on after connection succeeds.
socket.setSoTimeout(context.getResources().getInteger(R.integer.time_out));
// socket = new Socket();
// socket.connect(new InetSocketAddress(dstAddress, dstPort), 2000);
if (!socket.isConnected())
throw new SocketException("Could not connect to Socket");
DataInputStream DIStream = new DataInputStream(socket.getInputStream());
DataOutputStream DOStream = new DataOutputStream(socket.getOutputStream());
DOStream.write(numCmd.getBytes(), 0, numCmd.getBytes().length);
DOStream.flush();
response = DIStream.readLine();
DOStream.close();
DIStream.close();
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "{\"Error_no\":5000}";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "{\"Error_no\":5000}";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "{\"Error_no\":5000}";
} finally {
if (socket != null) {
try {
socket.close();
if(response == null){
response = "{\"Error_no\":5000}";
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "{\"Error_no\":5000}";
}
}
}
// Log.i("resp",response);
return response;
}
The PHP code:
<?php
echo "service php started \n";
$version = '2.2.0';
$address = '10.164.2.1';
$port = 4444;
$timeout= 2;
$cmd = explode("*", str_replace(array("/"), "*",
str_replace(" ","",shell_exec("netstat -tulpn | grep :".$port))));
echo "pid ".$cmd[1];
$cmd[1] = substr_replace($cmd[1], '', 0, 6);
echo "pid ".$cmd[1];
exec("kill -9 $cmd[1]");
sleep(1);
$socketPath = 'unix:///home/root/sockets/local_iapp_socket';
// Create WebSocket.
$server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($server, SOL_SOCKET, SO_REUSEADDR, 1);
if(socket_bind($server, $address, $port)){
echo "Connected with tcp socket \n";
}else{
echo "connection with tc socket error \n";
}
socket_listen($server);
echo "server is listning \n";
$socket_fd = connect_socket_local();
echo "connected to unix socket \n";
while ($client = socket_accept($server)) {
$num_cmd = socket_read($client, 5000);
echo "Client Message : ".$num_cmd ."\n";
if($socket_fd != false){
$id=24;
$len= strlen($num_cmd);
$fwrite1 = fwrite($socket_fd, pack("L", $len),4);
if($fwrite1 === false){
$content = '{"Error_no":5000}';
echo "fwrite1 a échoué : raison : " . socket_strerror(socket_last_error()) . "\n";
exit;
}
$fwrite2 = fwrite($socket_fd,pack("L", $id),4);
if($fwrite2 === false){
$content = '{"Error_no":5000}';
echo "fwrite2 a échoué : raison : " . socket_strerror(socket_last_error()) . "\n";
exit;
}
$fwrite3 = fwrite($socket_fd,$num_cmd,strlen($num_cmd));
if($fwrite3 === false){
$content = '{"Error_no":5000}';
echo "fwrite3 a échoué : raison : " . socket_strerror(socket_last_error()) . "\n";
exit;
}
$content = fread($socket_fd, 5000) ;
if( $content == null){
echo "impossible d'atteindre la socket locale" . socket_strerror(socket_last_error()) . "\n";
$content = '{"Error_no":5000}';
}
}else{
$content = '{"Error_no":5000}';
echo "problème de connexion" . socket_strerror(socket_last_error()) . "\n";
}
$json_resp = $content;
echo "json_resp" . $json_resp."\n\r";
socket_write($client, $json_resp,strlen($json_resp));
socket_close($client);
}
socket_close($server);
function connect_socket_local()
{
global $socketPath , $timeout;
if (($socket_fd= stream_socket_client($socketPath , $errorno, $errorstr, $timeout)) === false) {
echo "stream_socket_client a échoué : raison : " . socket_strerror(socket_last_error()) . "\n";
}
return $socket_fd;
}
?>

You can run a separate server process for each client. Each server process needs to have an unique IP address or TCP port. Each client needs to be configured to connect to the correct server address and port. This is difficult to manage and doesn't scale.
At the server side, you can get the peer address, that is the IP address and port of the client. In PHP, you can use socket_getpeername() for this. However, depending on the network setup, this might not be the IP address of the client itself. It can be the IP address of an intermediate gateway or other network device. Also, the client IP address might be dynamic.
The best solution is to send some identification from the client to the server when the clients connects to the server. This can be a name or some other unique identification. You have to change the protocol between the client and server to support this. The client needs to know when and how to send the identification. The server needs to know when and how to receive the identification.

Related

Request ajax to file not control PHP in Laravel 5.1?

I have an own file that receives for post a request ajax, But the request never comes.
In a view:
<script type="text/javascript">
$(document).ready(function(){
$("#bloquear").click(function(e){
$.post("proyectodam/app/Lib/ServerSocket.php", {op: 1});
});
});
</script>
The php I have it in a directory that I have created,His namespaces:
namespace proyectodam\Lib;
file PHP:
<?php
namespace proyectodam\Lib;
try{
set_time_limit(0);
$address = '127.0.0.1';
$port = 5555;
// Create a TCP Stream socket
$sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); // 0 for SQL_TCP
// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address'); //0 for localhost
// Start listening for connections
socket_listen($sock);
//loop and listen
while(true){
/* Accept incoming requests and handle them as child processes */
$client = socket_accept($sock);
//$_SESSION[socket_getpeername($client, AF_INET, 5555)] = socket_getpeername($client, AF_INET, 5555);
// Read the input from the client – 1024000 bytes
//$input = socket_read($client, 1024000);
// from here you need to do your database stuff
// and handle the response
// Display output back to client
if(isset($_POST["op"])){
$message = 1;
socket_write($client, $message, strlen($message));
}
socket_close($client);
}
// Close the master sockets
socket_close($sock);
}catch(Exception $e){
echo 'Excepción capturada: ', $e->getMessage(), "\n";
}
Client java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class ClienteSocket{
public static void main(String args[]){
try {
Socket client = new Socket("127.0.0.1", 5555);
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
OutputStreamWriter wr = new OutputStreamWriter(client.getOutputStream());
String op;
while((op = in.readLine()) != null){
System.out.println(op);
}
}catch (IOException e){
System.out.println("error");
e.printStackTrace();
}
}
}
That fails?
If you are making a post request to proyectodam/app/Lib/ServerSocket.php You need to make sure that you have a file in /public/proyectodam/app/Lib/ServerSocket.php
What I would suggest doing however is making an api endpoint such as:
Route::post('socket', function() {
return new ClientSocket();
}
And then have your client socket library class be:
<?php
namespace proyectodam\Lib;
class ClienteSocket {
public function __construct()
{
try{
set_time_limit(0);
$address = '127.0.0.1';
$port = 5555;
// Create a TCP Stream socket
$sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); // 0 for SQL_TCP
// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address'); //0 for localhost
// Start listening for connections
socket_listen($sock);
//loop and listen
while(true){
/* Accept incoming requests and handle them as child processes */
$client = socket_accept($sock);
//$_SESSION[socket_getpeername($client, AF_INET, 5555)] = socket_getpeername($client, AF_INET, 5555);
// Read the input from the client – 1024000 bytes
//$input = socket_read($client, 1024000);
// from here you need to do your database stuff
// and handle the response
// Display output back to client
if(isset($_POST["op"])){
$message = 1;
socket_write($client, $message, strlen($message));
}
socket_close($client);
}
// Close the master sockets
socket_close($sock);
} catch(Exception $e){
echo 'Excepción capturada: ', $e->getMessage(), "\n";
}
}
}

How sending data to a server TCP in PHP from jQuery and this one this to the client Java?

I manage to send data for ajax (jquery) to the server TCP who is running for line of commands, but apparently the serveris unable to handle them.
From this JS/HTML I try to send data to the server.
<script type="text/javascript">
$(document).ready(function(){
$("#bloquear").click(function(){
$.post("http://localhost/proyectodam/app/Lib/ServerSocket.php", {op: 1});
});
});
</script>
Server in PHP.
<?php
namespace proyectodam\Lib;
try{
set_time_limit(0);
$address = '127.0.0.1';
$port = 5555;
// Create a TCP Stream socket
$sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); // 0 for SQL_TCP
// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address'); //0 for localhost
// Start listening for connections
socket_listen($sock);
//loop and listen
while(true){
/* Accept incoming requests and handle them as child processes */
$client = socket_accept($sock);
//$_SESSION[socket_getpeername($client, AF_INET, 5555)] = socket_getpeername($client, AF_INET, 5555);
// Read the input from the client – 1024000 bytes
//$input = socket_read($client, 1024000);
// from here you need to do your database stuff
// and handle the response
// Display output back to client
if(isset($_POST["op"]) && $_POST["op"] == 1){
$message = $_POST["op"];
socket_write($client, $message, strlen($message));
//echo json_encode("si");
}
socket_close($client);
}
// Close the master sockets
socket_close($sock);
}catch(Exception $e){
echo 'Excepción capturada: ', $e->getMessage(), "\n";
}
Client Java.
public class ClienteSocket{
public static void main(String args[]){
try {
Socket client = new Socket("127.0.0.1", 5555);
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
OutputStreamWriter wr = new OutputStreamWriter(client.getOutputStream());
String op;
while((op = in.readLine()) != null){
System.out.println(op);
}
}catch (IOException e){
System.out.println("error");
e.printStackTrace();
}
}
}
How the server intercept the data that come for ajax and once obtained to send to the client Java?

Can't monitor STDOUT of a C program in parallel with web sockets, by using stream select in PHP

I m writing a websocket based client server in PHP.Server is listeninig for clients which get connected from a browser, when client write something its echoed
back to the client. Client input and new connection from the client are monitored by using stream select.
In my previous work I used stream select to get output of the C program and display it on the browser now I want that when a client
send some message I execute a C program using proc open. C program outputs "hello" message in a loop after certain time, I want that when ever there
is output on the C program I send it to the client browser.
Client browser successful gets connected, when client sends data the server echo it back, the C program gets executed on server but I cant monitor its STDOUT by using same stream select that is monitoring my sockets.
I am not able to monitor my child process STDOUT using stream select. By using select How can I monitor the new process STDOUT that I started using proc open
,It looks that some how I lost the STDOUT descriptor that is usually accessible after starting the C program by using proc open.
hello.c
#include<stdio.h>
#include<stdint.h>
void main(void)
{
uint8_t i = 0;
uint8_t count = 10;
for(i = 0; i < count; i++)
{
printf("hello world\n");
sleep(1);
}
}
server.php
execute_prog('unbuffer /var/www/html/test/./hello3');//unbuffer stdout
function execute_prog($exe)
{
echo "[+execute_prog]";
$host = 'localhost'; //host
$port = '9000'; //port
$null = NULL; //null var
$read_socks;
$new_client;
$server = stream_socket_server("tcp://0.0.0.0:9000", $errno, $errorMessage);
if ($server === false)
{
throw new UnexpectedValueException("Could not bind to socket: $errorMessage");
}
set_time_limit(1800);
$exe_command = escapeshellcmd($exe);
$descriptorspec = array(
0 => array("pipe", "r"), // stdin -> for execution
1 => array("pipe", "w"), // stdout -> for execution
2 => array("pipe", "w") // stderr
);
{
$client_socks = array();
$read_socks = array($server);
$changed = array();
$stdout = NULL;
while(1)
{
//prepare readable sockets
$write = NULL;
$err = NULL;
$except = NULL;
$changed = $read_socks;//by refrence
if (false === ($num_changed_streams = stream_select($changed, $write, $except, 0)))
{
/* Error handling */
echo "Errors\n";
}
else if ($num_changed_streams > 0)
{
/* At least on one of the streams something interesting happened */
echo "Data on ".$num_changed_streams." descriptor\n";
if(in_array($stdout[0], $changed))
{
echo "Data on child process STDOUT\n";
$s = fgets($stdout[0]);
if( $s === false )
{
// Hello program has finished.
echo 'Finished', PHP_EOL;
$s = NULL;
ob_flush();
flush();
// Close all descriptors and return...
// break;
}
else
{
echo $s."</br>";
//fwrite($new_client,$s,strlen($s));//commented temporarily
$s = NULL;
ob_flush();
flush();
}
}
else if(in_array($server, $changed))
{
//new client
echo "New Connection\n";
$new_client = stream_socket_accept($server);
if ($new_client)
{
//print remote client information, ip and port number
echo 'Connection accepted from ' . stream_socket_get_name($new_client, true) . "n";
$read_socks[] = $new_client;
echo "Now there are total ". count($read_socks) . " clients.n";
}
$header = fread($new_client, 1024);//read data sent by the socket
perform_handshaking($header, $new_client, $host, $port); //perform websocket handshake
$ip = stream_socket_get_name($new_client, true);
$response = mask(json_encode(array('type'=>'system', 'message'=>$ip.' connected'))); //prepare json data
fwrite($new_client,$response,strlen($response));
//delete the server socket from the read sockets
unset($changed[ array_search($server, $changed) ]);
}
else if($write)
{
echo "Data on child process STDIN\n";
}
else if($err)
{
echo "Data on child process STDERR\n";
}
else
{
echo "Message from the client \n";
//message from existing client
foreach($changed as $sock)
{
$data = fread($sock, 128);
//echo "Data read:".$data." From sock:".$sock."\n";
if(!$data)
{
unset($client_socks[ array_search($sock, $client_socks) ]);
#fclose($sock);
echo "A client disconnected. Now there are total ". count($client_socks) . " clients.n";
continue;
}
else
{
$received_text = unmask($data); //unmask data
$tst_msg = json_decode($received_text); //json decode
$user_name = $tst_msg->name; //sender name
$user_message = $tst_msg->message; //message text
$user_color = $tst_msg->color; //color
// echo "name:".$user_name." user mesg:".$user_message."\n";
//prepare data to be sent to client
$response_text = mask(json_encode(array('type'=>'usermsg', 'name'=>$user_name, 'message'=>$user_message, 'color'=>$user_color)));
fwrite($sock, $response_text, strlen($response_text));
//..................................................................................................................
echo "Executing a process\n";
$process = proc_open($exe_command, $descriptorspec, $pipes);//creating child process
if (is_resource($process))
{
echo "Process Created";
}
$read_sock[] = $pipes[1];//add a descriptor
$stdout = array($pipes[1]);//save stdout in a variable defined above
print_r ($stdout);
}
}
}
$num_changed_streams = 0;
}
}
// close the listening socket
fclose($server);
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
echo "exitcode: ".proc_close($process)."\n";
}
echo "[-execute_prog]";
// return $ret;
}
Output Of server.php
[+execute_prog]Data on 1 descriptor
New Connection
Connection accepted from 127.0.0.1:49875nNow there are total 2 clients.nData on 1 descriptor
Message from the client
Executing a process
Process Created Array
(
[0] => Resource id #12
)
As can be seen from output that it never show message "Data on child process STDOUT" even the C program was created
Other utility functions unmask, mask etc can be provided if required.
Thankyou
i was saving the stdout in read_sock instead of read_socks array, now its resolved

Android socket not receiving data from php server

I have a problem with android client receiving data from php server. Android can successfully writes data to php server and server accepts that data and then send back response to that client but android is not accepting. it does not move forward from Socket s = ss.accept()
Here is my android code to recieve data
public void run() {
Boolean end = false;
ServerSocket ss = serverSocket;
/*try {
ss = new ServerSocket(54546);
} catch (IOException e1) {
//TODO Auto-generated catch block
e1.printStackTrace();
}*/
while(!end){
//Server is waiting for client here, if needed
try {
Log.i("before accept", "yes");
Socket s = ss.accept();
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
//PrintWriter output = new PrintWriter(s.getOutputStream(),true); //Autoflush
String st = input.readLine();
Log.d("Tcp Example", "From client: "+st);
//output.println("Good bye and thanks for all the fish :)");
}catch (IOException e) {
e.printStackTrace();
}
}
}
Here is my php code
$host = "127.0.0.1";
$port = 54546;
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
//$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create socket\n");
/*if (!socket_connect($socket, $host, $port)) {
die('failed'.socket_strerror(socket_last_error($socket)));
}*/
if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo socket_strerror(socket_last_error($socket));
exit;
}
$result = socket_bind($socket, $host, $port) or die("Could not create socket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
echo "\nbefore socket accept while loop\n";
$aaa = fopen("tesst.txt", "w");
while(true)
{
echo "\nbefore socket accept\n";
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
echo "\nThe server is ready\n";
$input = socket_read($spawn, 1024) or die("Could not read input\n");
echo "Input recieved from $spawn : ".$input;
fwrite($aaa, $input);
$output = $input."\n";
$sent = socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
echo "Output sent ".$sent;
socket_close($spawn);
}
fclose($aaa);
socket_close($socket);
echo "\nTerminating\n";
ss.accept() is not accepting connection from server.
We don't have to do ss.accept() for the client as well. The client should establish the connection using the connect() and the server should do accept(). Once the connection is established, the server should use the file descriptor returned from the accept() to send and receive data to/from the client. On the other side, the client does not need to do any accept, it should simply call recv() to retrieve received data.
Thus, if the Android code is the client, then it should do a connect() call to the PHP server -- the connect() call would take the IP address and the port number (54546). With a successful connect() call, the accept() on the PHP would return.

Apache thrift: client timeout issues

I have some Apache Thrift (v.0.6.1) test application with perl-server and php-client.
The behaviour I cannot explain: If we call server-method with invalid argument we see the error in server-output, but php-client stays waiting the response infinitely.
Here are the sources of server:
sub new {
my $classname = shift;
my $self = {};
return bless($self,$classname);
}
sub DateToTimestamp
{
my ($self, $date) = #_;
my $result = CommonAPI::DateToTimestamp($date);
return $result;
}
eval {
my $handler = new RPCHandler;
my $processor = new RPCPerformanceTest::RPCPerformanceTestProcessor($handler);
my $serversocket = new Thrift::ServerSocket(9091);
my $forkingserver = new Thrift::ForkingServer($processor, $serversocket);
print "Starting the server...\n";
$forkingserver->serve();
print "done.\n";
}; if ($#) {
if ($# =~ m/TException/ and exists $#->{message}) {
my $message = $#->{message};
my $code = $#->{code};
my $out = $code . ':' . $message;
die $out;
} else {
die $#;
}
}
and client:
try {
$socket = new TSocket($server_host, $server_port);
$transport = new TBufferedTransport($socket, 1024, 1024);
$protocol = new TBinaryProtocol($transport);
$client = new RPCPerformanceTestClient($protocol);
$transport->open();
$start = microtime(true);
$result = $client->DateToTimestamp('071/26/2011 01:23:45');
var_dump($result);
} catch (Exception $e) {
echo 'Exception: <b>' . $e->getMessage() . '</b>';
}
Why is this happening? Is it my fault? Is it expected behavour?
The Thrift PHP library is a bit broken. You need to manually set the timeouts
E.g.
$socket = new TSocket('host', 9095);
$socket->setSendTimeout(60000);
$socket->setRecvTimeout(60000)
This happens often with protocols that do not supply message length: a client sends more data then the server expects and waits for the server to receive the data. The server receives some of the data, tries to parse it and fails. Now the server-side of the protocol is in errorneous state. If it continues to read the data, it may block. Most probably, the server-side has sent you some error response and is waiting at the same time for the client to receive the response, but that will never happen too.
This is my guess. The best strategy IMHO is to set a time-out for both client and server sockets.

Categories