如何在Java和PHP之间建立通信?

时间:2016-10-15 12:52:02

标签: java php multithreading sockets wamp

我正在使用WAMP,我有一个支持WebSockets的模块。我打算实现一个独立的Java项目,该项目将在新的Thread中启动WAMP,在给定的ServerSocket和localhost处初始化Java port作为地址,并监听该{{}}上的连接{1}}。到目前为止,我能够启动WAMP,但是我在初始化port和接收实际消息方面遇到了问题。这些是我的Java类:

WebSocket.java

ServerSocket

Command.java

package websocket;
import java.util.Scanner;

public class WebSocket {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String command = "";
        Scanner scanner = new Scanner(System.in);
        do {
            String[] commandArray = command.split(" ");
            Command.executeCommand(commandArray);
            command = scanner.nextLine();
            System.out.println("Command: " + command);
        } while (!command.equals("exit"));
        Command.exit();
        System.out.println("Finished");
        System.exit(0);
    }

}

WAMP.java

package websocket;

public class Command {
    private static WAMP wamp;
    private static WS ws;

    public static void WAMPMethod(String[] commandArray) {
        if (wamp == null) {
            wamp = WAMP.create(commandArray);
        }
    }

    public static void WSMethod(String[] commandArray) {
        if (ws == null) {
            ws = WS.create();
        }
    }

    private static void stopThread(Thread t) {
        if (t != null) {
            try {
                t.join();
            } catch (Exception e) {
                System.err.println(e.toString());
            }
        }
    }

    public static void exit() {
        stopThread(wamp);
        stopThread(ws);
    }

    public static void executeCommand(String[] commandArray) {
        if (commandArray[0].equals("WAMP")) {
            WAMPMethod(commandArray);
        } else if (commandArray[0].equals("WS")) {
            WSMethod(commandArray);
        }
    }
}

WS.java

package websocket;

public class WAMP extends Thread {

    public String[] command;

    public WAMP(String[] command) {
        this.command = command;
    }

    public static WAMP create(String[] command) {
        WAMP wamp = new WAMP(command);
        wamp.start();
        return wamp;
    }

    @Override
    public void run() {
        if (command[1].equals("Start")) {
            StartWAMP();
        }
    }

    private void StartWAMP() {
        try {
            Runtime runTime = Runtime.getRuntime();
            runTime.exec("C:\\wamp\\wampmanager.exe");
            System.out.println("WAMP is started");
        } catch (Exception e) {
            System.err.println(e.toString());
        }        
    }

}

当我启动Java项目时,我会发出package websocket; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class WS extends Thread { public static int port = 4444; public static WS create() { WS ws = new WS(); ws.start(); return ws; } public WS() { } @Override public void run() { try { InetAddress addr = InetAddress.getByName("127.0.0.1"); ServerSocket serverSocket = new ServerSocket(port, 50, addr); Socket clientSocket = serverSocket.accept(); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String command = ""; System.out.println("WS Started"); while (!command.equals("exit")) { command = in.readLine(); System.out.println(command); } } catch (Exception e) { System.err.println(e.toString()); } } } WAMP Start命令,我的输出是:

  

WAMP开始

     

命令:WAMP开始

     

WAMP已启动

     

WS

     

命令:WS

WS Thread在

行进入等待状态
WS

启动WAMP后,执行以下PHP脚本:

Socket clientSocket = serverSocket.accept();

但是,我的Java程序没有继续它的进程,这意味着它仍然没有建立套接字连接。我怎么能解决这个问题,我错过了什么?

0 个答案:

没有答案