java php communication

548 views Asked by At

I have written a client side java application which communicates through http to a php server. I need to implement a listener on the java (client) side to respond to requests made by the php server. Currently, the java apps are hitting a text file on the server that is updated every minute.

This has worked ok, but now the number of client java apps is rising and this primitive system is starting to break down.

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. 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.

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? Here is the java code... This is all over the web...

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

$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.

Also, is this the best method for my needs?? Thanks.

1

There are 1 answers

0
Riza On

The code you include works if the php server machine could connect to the java client machine. 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. Once you have it, assign that IP to $HOST, then the code will runs fine.

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. The java client, acting like a web browser, send a HTTP request and receive HTTP reply that contains data needed by your java client. And when the client numbers rise to a level that you PHP server cannot handle, you could scale it up. Although I haven't had the experience myself, scaling up a PHP server is not uncommon these days.