简体   繁体   English

js事件时将php套接字连接到java

[英]Socket php to java when js event

So currently, I manage to communicate between php and java thanks to the socket.所以目前,由于套接字,我设法在 php 和 java 之间进行通信。 The problem is that once the java client is connected, I can't do anything anymore.问题是,一旦连接了java客户端,我就不能再做任何事情了。

I would like that when a js event is triggered, information to the java client is transmitted.我希望在触发 js 事件时,将信息传输到 java 客户端。

How can I do that?我怎样才能做到这一点?

Thanks谢谢

My code php:我的代码php:

    $host = "0.0.0.0";
    $port = 6969;

    set_time_limit(0);

    $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Create Error");

    $result = socket_bind($socket, $host, $port) or die("Bind Error");

    $result = socket_listen($socket, 3) or die("Listener Error");

    $spawn = socket_accept($socket) or die("Accept Error");

    $input = socket_read($spawn, 1024) or die("Read Error");

    echo "Received: ".$input;

    $output = "Received";
    socket_write($spawn, $output, strlen($output)) or die("Write Error");

My java code:我的Java代码:

        Socket socket = new Socket("localhost", 6969);

        PrintWriter writer = new PrintWriter(socket.getOutputStream());
        writer.println("Test Message");
        writer.flush();

        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        System.out.println(reader.readLine());

I will suggest you to use REST unless you have a very specific use case .除非您有非常具体的用例,否则我建议您使用 REST。

Else you need to close socket (on client * php ) and stream once you get the data.否则,您需要关闭套接字(在客户端 * php 上)并在获取数据后进行流式处理。

https://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html read the explanation here, this will help you https://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html阅读这里的解释,这会对你有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM