简体   繁体   中英

Java and PHP socket communication problems

I am trying to have a website that I am working on communicate with a Java program. I'm very new to sockets of any form so I'm having problems having my Java program send a response back to my website. Here's my current Java code:

while(true) {
    ServerSocket socket = null;
    Socket connection = null;
    InputStreamReader inputStream = null;
    BufferedReader input = null;
    DataOutputStream response = null;
    System.out.println("Server running");
    try {
        socket = new ServerSocket(4400);
        while(true) {
            connection = socket.accept();
            inputStream = new InputStreamReader(connection.getInputStream());
            input = new BufferedReader(inputStream);
            String command = input.readLine();
            System.out.println("command: " + command);
            if(command.equals("restart")) {
                break;
            } else if(command.equals("requeststatus")) {
                String reply = "testing";
                System.out.println(reply);
                response = new DataOutputStream(connection.getOutputStream());
                response.writeUTF(reply);
                response.flush();
            }
        }
    } catch(IOException e)  {
        e.printStackTrace();
    } finally {
        if(socket != null) {
            try {
                socket.close();
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
        if(connection != null) {
            try {
                connection.close();
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
        if(inputStream != null) {
            try {
                inputStream.close();
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
        if(input != null) {
            try {
                input.close();
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
        if(response != null) {
            try {
                response.close();
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
    }
    System.out.println("Server Closing");
}

And my current PHP code:

<?php
function send($message)  {
    $address = 'localhost';
    $port = 4400;
    $socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
    try {
        socket_connect($socket, $address, $port);
        $status = socket_sendto($socket, $message, strlen($message), MSG_EOF, $address, $port);
        if($status != false)  {
            // If it worked then wait for a response?
            // This is where the problem is at
            if($next = socket_read($socket, $port)) {
                echo $next;
            }
            return true;
        }
        return false;
    } catch(Exception $e)  {
        return false;
    }
}
if(send("requeststatus")) {
    echo "Worked";
}
?>

When I start up my program and I load my webpage the page just continually loads without printing anything. I'm guessing PHP runs everything and then displays the results in my browser after the script finishes and my script is getting "blocked up" when waiting for a reply? If so, how would I get my PHP script to send "requeststatus" to my Java program and have my Java program respond? The end goal is to display the response from the Java program on my website.

Also I'm pretty sure I'm writing this system inefficiently and wrong. What is the correct way to write this type of system? Any tips? Thank you for reading.

String command = input.readLine();

This waits for a newline or end of stream. You never send a newline, and if you close the stream you can not write to output either.

So basically add a newline to your message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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