簡體   English   中英

在Java應用程序和PHP之間打開套接字

[英]open socket between java app and Php

我正在使用套接字將數據從PHP頁面發送到Java桌面應用程序以對其進行處理並返回處理后的數據。 問題是我無法使用從POINT X中的Php頁面接收的數據(請參閱Java代碼)。我的意思是在讀寫之間! 即使我只想打印它:

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

這是代碼。

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

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

當套接字打開時,緩沖的讀數正忙,無法使用! 這個問題叫做阻塞。

您可以通過在應用程序中使用多線程來解決它。

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