dongyu1918 2015-11-12 19:09
浏览 26

在java app和Php之间打开套接字

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
");
            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
");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket
");
$result = socket_listen($socket, SOMAXCONN) or die("Could not set up socket listener
");
$spawn = socket_accept($socket) or die("Could not accept incoming connection
");
$input = socket_read($spawn, 10000, PHP_NORMAL_READ) or die("Could not read input
");
echo $input;

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

socket_close($spawn);
socket_close($socket);
}
catch(Exception $e) {
 echo 'Message: ' .$e->getMessage();
 }
  • 写回答

1条回答 默认 最新

  • du9537 2015-11-13 10:05
    关注

    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 
    ");
                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);
        }
    
    
    
    }
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路