简体   繁体   English

java php通讯

[英]java php communication

I have written a client side java application which communicates through http to a php server. 我已经编写了一个客户端Java应用程序,该应用程序通过http与php服务器进行通信。 I need to implement a listener on the java (client) side to respond to requests made by the php server. 我需要在Java(客户端)端实现一个侦听器,以响应php服务器发出的请求。 Currently, the java apps are hitting a text file on the server that is updated every minute. 当前,Java应用程序在服务器上命中一个每分钟更新一次的文本文件。

This has worked ok, but now the number of client java apps is rising and this primitive system is starting to break down. 一切正常,但是现在客户端Java应用程序的数量正在增加,并且这个原始系统开始崩溃。

What is the best way to change this? 改变这种状况的最好方法是什么? I tried a java ServerSocket listener on the java client app, but can't get that to work. 我在Java客户端应用程序上尝试了Java ServerSocket侦听器,但无法正常工作。 I am having trouble completing the communication. 我在完成交流时遇到麻烦。 All examples on the web use localhost as ip address example, and my php server is remote hosted. 网络上的所有示例都使用localhost作为IP地址示例,而我的php服务器是远程托管的。

Do I need to get the ip address of the client machine and send that to the php server so php will know where to send the message? 我是否需要获取客户端计算机的IP地址并将其发送到php服务器,以便php知道将消息发送到哪里? Here is the java code... This is all over the web... 这是Java代码...遍布网络...

public class MyJavaServer
{

    public static void main(String[] args)
    {


        int port = 4444;
        ServerSocket listenSock = null; //the listening server socket
        Socket sock = null;          //the socket that will actually be used for communication

        try
        {

            System.out.println("listen");
            listenSock = new ServerSocket(port);

            while (true)
            {

                sock = listenSock.accept(); 

                BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
                String line = "";
                while ((line = br.readLine()) != null)
                {
                    bw.write("PHP said: " + line + "\n");
                    bw.flush();
                }

                //Closing streams and the current socket (not the listening socket!)
                bw.close();
                br.close();
                sock.close();
            }
        }
        catch (IOException ex)
        {
            System.out.println(ex);
        }
    }
}

... and here is the php ...这是PHP

$PORT = 4444; //the port on which we are connecting to the "remote" machine
$HOST = "ip address(not sure here)"; //the ip of the remote machine(of the client java app's computer???

$sock = socket_create(AF_INET, SOCK_STREAM, 0) 
        or die("error: could not create socket\n");

$succ = socket_connect($sock, $HOST, $PORT) 
        or die("error: could not connect to host\n");

$text = "Hello, Java!\n"; //the text we want to send to the server

socket_write($sock, $text . "\n", strlen($text) + 1) 
        or die("error: failed to write to socket\n");

$reply = socket_read($sock, 10000, PHP_NORMAL_READ)
        or die("error: failed to read from socket\n");

echo $reply;

This simply does not work. 这根本行不通。 The java app listens, but the php script never connects. Java应用程序会监听,但php脚本永远不会连接。

Also, is this the best method for my needs?? 另外,这是满足我需求的最佳方法吗? Thanks. 谢谢。

The code you include works if the php server machine could connect to the java client machine. 如果php服务器计算机可以连接到Java客户端计算机,则包含的代码将起作用。 In your case that this is all over the web, it means that the java client machine should have an IP that are accessible to public. 就您而言,这是整个Web上的情况,这意味着Java客户端计算机应具有可供公众访问的IP。 Once you have it, assign that IP to $HOST, then the code will runs fine. 拥有该地址后,将该IP分配给$ HOST,则代码将正常运行。

Assuming that no client can have a public IP, I think the best method is to make your java client talk to your PHP Server in request-reply manner using HTTP request. 假设没有客户端可以拥有公共IP,我认为最好的方法是使Java客户端使用HTTP请求以请求-答复的方式与PHP Server进行通信。 The java client, acting like a web browser, send a HTTP request and receive HTTP reply that contains data needed by your java client. Java客户端就像Web浏览器一样,发送HTTP请求并接收包含Java客户端所需数据的HTTP答复。 And when the client numbers rise to a level that you PHP server cannot handle, you could scale it up. 并且当客户端数量上升到PHP服务器无法处理的水平时,您可以扩大规模。 Although I haven't had the experience myself, scaling up a PHP server is not uncommon these days. 尽管我自己还没有经验,但是如今扩展PHP服务器并不罕见。

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

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