Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple Java TCP Server and PHP Client Problems
    primarykey
    data
    text
    <p>I am trying to set up a simple TCP connection on a single port between a Java application that will act as the TCP server and a PHP scrip that will act as the Client.</p> <p>I will post the code for each below, but the problem is: I can connect and send data to the Java server just fine. I can get that data and print it out. My problem arises in trying to send a response back to the php server.</p> <p>When I comment out that last line of php "echo socket_read($socket, 14, PHP_NORMAL_READ);" The data gets to the Java server just fine. When I add that line back in, the data does not even get to the Java server.</p> <p>Because of this, I am assuming my problem has something to do with how I am either sending the data from Java, or trying to get the data in PHP from the server.</p> <p>This has me really stumped, any help would be greatly appreciated!</p> <p>Java Server:</p> <pre><code>protected ServerSocket socket; protected final int port = 9005; protected Socket connection; protected String command = new String(); protected String responseString = new String(); socket = new ServerSocket(port); while(true) { // open socket connection = socket.accept(); // get input reader InputStreamReader inputStream = new InputStreamReader(connection.getInputStream()); BufferedReader input = new BufferedReader(inputStream); // get output handler DataOutputStream response = new DataOutputStream(connection.getOutputStream()); // get input command = input.readLine(); // process input Logger.log("Command: " + command); responseString = command + " MC2 It Works!"; // send response response.writeBytes(responseString); response.flush(); response.close(); } </code></pre> <p>PHP Client:</p> <pre><code>$address = 'example.com'; // obviously not the address I am using $port = 9005; $message = "Test"; $socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp')); socket_connect($socket, $address, $port) if(socket_send($socket, $message, strlen($message), MSG_EOF) != FALSE) { echo socket_read($socket, 14, PHP_NORMAL_READ); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload