简体   繁体   中英

Send data from PHP to Java tcp listener

So, I have a very basic test set up to see if i can send data from a php web page to a java app running on the same server.

The java app is dead simple, it just listens on a TCP socket for data

import java.io.*;
import java.net.*;

class TCPServer
{
   public static void main(String argv[]) throws Exception
      {
         String clientSentence;
         String capitalizedSentence;
         ServerSocket welcomeSocket = new ServerSocket(6789);

         while(true)
         {
            Socket connectionSocket = welcomeSocket.accept();
            BufferedReader inFromClient =
               new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            clientSentence = inFromClient.readLine();
            System.out.println("Received: " + clientSentence);
            capitalizedSentence = clientSentence.toUpperCase() + '\n';
            outToClient.writeBytes(capitalizedSentence);
            connectionSocket.close(); //this line was part of the solution
         }
      }
}

I have tried two ways to send and read the response with php, but none seem to work. I get a connection OK and data sent OK, but the server doesn't print anything nor respond with anything so I dont know why it's saying it's OK :)

Method 1

$host = "tcp://localhost"; 
$port = 6789;
$data = 'test' . PHP_EOL;  //Adding PHP_EOL was the other part of the solution
$errstr = '';
$errno = '';

if ( ($fp = fsockopen($host, $port, $errno, $errstr, 3) ) === FALSE)
    echo "$errstr ($errno)";
else {
    print 'SUCCESS!<br />';
    fwrite($fp, $data);
    while (! feof($fp)) {
      echo fgets($fp, 4096);
    }
    fclose($fp);
}

Method 2

$host = "localhost"; 
$port = 6789;
$data = 'test';

if ( ($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === FALSE )
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error());
else 
{
    echo "Attempting to connect to '$host' on port '$port'...<br>";
    if ( ($result = socket_connect($socket, $host, $port)) === FALSE )
        echo "socket_connect() failed. Reason: ($result) " . socket_strerror(socket_last_error($socket));
    else {
        echo "Sending data...<br>";
        socket_write($socket, $data, strlen($data));
        echo "OK<br>";

        echo "Reading response:<br>";
        while ($out = socket_read($socket, 2048)) {
            echo $out;
        }
    }
    socket_close($socket);      
}

EDIT

Apparently fsockopen() has a problem connecting to localhost from a comment I found at PHP fsockopen doesnt return anything so changed to 127.0.0.1 but still not working.

  • Try changing $data = 'test' to $data = "test\\n" and see if that helps.
  • Close the socket on the server when your done otherwise the client will block in the while loop waiting for more data:

      outToClient.writeBytes(capitalizedSentence); connectionSocket.close() 

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