0

I'm using socket to send data from PHP page to Java desktop application to process it and return processed data. The problem is i can't use the data that received from Php page in POINT X (see java code).. i meant between reading and writing ! even if i want to just print it:

String line = "";
            while ((line = br.readLine()) != null){
                   System.out.println(line);
            }

This is the code..

In JAVA:

boolean stayRunning=true;
    while(stayRunning){
        try{
            Socket s = new Socket("localhost",1235);
            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));


            POINT X


            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            bw.write("Hello Java\n");
            bw.flush();

            String line = "";
            while ((line = br.readLine()) != null){
                   System.out.println(line);
            }



            bw.close();
            br.close();
            s.close();   
        }
        catch(Exception e)
        {
            //System.out.println("Java Error: " + e.getMessage());
        }
    }

In Php:

try {
$host = "localhost";
$port = 1235;

$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$result = socket_listen($socket, SOMAXCONN) or die("Could not set up socket listener\n");
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
$input = socket_read($spawn, 10000, PHP_NORMAL_READ) or die("Could not read input\n");
echo $input;

$output = $_POST["txtbox"]."+|+".$_POST["se"];
socket_write($spawn,$output."\n", strlen($output) + 1) or die("Could not write output\n");

socket_close($spawn);
socket_close($socket);
}
catch(Exception $e) {
 echo 'Message: ' .$e->getMessage();
 }
Minions
  • 5,104
  • 5
  • 50
  • 91

1 Answers1

0

When the socket is open, the buffered reading is busy and couldn't used ! This problem called Blocking..

you can solve it by using Multi-threading in your application.

The Java Code:

public class PhpJavaConnection extends Thread {
    static BufferedReader br = null;
    static BufferedWriter bw = null;
    static Socket s = null;

public void run(){
                    String line = "";
                    try {
                        while ((line = br.readLine()) != null){
                            System.out.println(line); 
                        }       
                    } catch (IOException ex) {
                        //Logger.getLogger(PhpJavaConnection.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

public static void main(String[] args) {

    boolean stayRunning=true;


    System.out.println("Reading..");
    while(stayRunning){
        try{
            s = new Socket("localhost",1235);
            br = new BufferedReader(new InputStreamReader(s.getInputStream()));


            PhpJavaConnection my = new PhpJavaConnection();
            my.start();



            bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            bw.write("Hello Java \n");
            bw.flush();


        }
        catch(Exception e)
        {
            //System.out.println("Java Error: " + e.getMessage());
        }
    }

    try {
        bw.close();
        br.close();
            s.close();
    } catch (IOException ex) {
        Logger.getLogger(PhpJavaConnection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }



}

}
Minions
  • 5,104
  • 5
  • 50
  • 91