繁体   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