title: PHP/Java socket communication not working
body: <p>I've been stuck for two days with the following problem: I wrote a java socket server which can receive and send data to a socket hosted at 'localhost:64005'. I can connect to it via php and send or receive messages. However I cannot send and then receive an answer. I've traced the problem back to the php script i've written.</p>
  
  <pre><code>&lt;?php
      class socketCommunication{
          protected $PK;
          protected $ip = '127.0.0.1';
          protected $port = 64005;
          protected $socket;
          public $result;
  
          function __construct($key){
           $this-&gt;socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
           $this-&gt;result = socket_connect($this-&gt;socket, $this-&gt;ip, $this-&gt;port) or die("Could not connect to server\n");
           $this-&gt;PK = $key;
           $this-&gt;sendSocket();
          }
  
          function getResponse(){
              //$input =  socket_read($this-&gt;socket, 1024) or die("Could not read");
              $bytes = socket_recv($this-&gt;socket, $buf, 2048, MSG_WAITALL);
              return $buf;
          }
  
          function sendSocket(){
              $len = strlen($this-&gt;PK);
              socket_send ($this-&gt;socket, $this-&gt;PK, $len, 0);
          }
      }
  ?&gt;
  
  &lt;?php
      //include("/mysql/RandomQuery.php");
      include("/java/socketCommunication.php");
  
      $object2 = new socketCommunication(100001);
      echo $object2-&gt;getResponse();
   ?&gt;
  </code></pre>
  
  <p>the java socket server:</p>
  
  <pre><code>package Server;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.net.Socket;
  import java.util.concurrent.Semaphore;
  
  import Database.Reader;
  
  public class Receive implements Runnable {
      Semaphore semaphore;
      private Socket connection;
      String result = "default";
  
      public Receive(Socket conn, Semaphore sem){
          this.connection = conn;
          this.semaphore = sem;
      }
  
      @Override
      public void run() {
              try {
                  semaphore.acquire();
                  System.out.println(connection.toString());
                  BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                  PrintWriter out = new PrintWriter(connection.getOutputStream(), true);
                  String userInput;
                  while ((userInput = in.readLine()) != null) {
                      System.out.println(userInput);
                      Reader reader = new Reader(Integer.parseInt(userInput));
                      this.result = reader.getResult();
                      System.out.println(result);
                      out.println(result);
                      break;
                  }
                  connection.close();
                  in.close();
                  System.out.println("input is closed");
                  semaphore.release();
                  } catch (IOException | InterruptedException e) {e.printStackTrace();}
      }
  
  }
  </code></pre>
  
  <p>As soon as I call both the sendSocket and getResponse method in php the page just keeps loading infinitely. However if I just call the sendSocket or getResponse(after changing the java socket so it won't wait for input) method seperatly they work fine.</p>
  
  <p>what am I doing wrong? </p>
  
  <p>Kind Regards.</p>