dtcd27183 2016-10-15 12:52
浏览 39

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

I am using WAMP and I have a module which supports WebSockets. I intend to implement a standalone Java project which will start WAMP in a new Thread, initializes a Java ServerSocket at a given port and at localhost as address and to listen to connections on that port. So far I am able to start WAMP, but I have a problem with initializing the ServerSocket and receiving actual messages. These are my Java classes:

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

}

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

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

}

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

}

When I start the Java project, I issue WAMP Start and WS commands and my output is:

WAMP Start

Command: WAMP Start

WAMP is started

WS

Command: WS

The WS Thread enters a waiting state at the line of

Socket clientSocket = serverSocket.accept();

After WAMP is started, I execute the following PHP script:

$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$mess = "This is a message";
socket_sendto($sock, $mess, strlen($mess), 0, '127.0.0.1', 4444);
socket_close($sock);

however, my Java program does not continue its process, which means that it still did not establish a socket connection. How could I fix this, what am I missing?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?