简体   繁体   English

在Java应用程序和PHP之间打开套接字

[英]open socket between java app and Php

I'm using socket to send data from PHP page to Java desktop application to process it and return processed data. 我正在使用套接字将数据从PHP页面发送到Java桌面应用程序以对其进行处理并返回处理后的数据。 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 ! 问题是我无法使用从POINT X中的Php页面接收的数据(请参阅Java代码)。我的意思是在读写之间! 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: 在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: 在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();
 }

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: Java代码:

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



}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM