title: Using sockets with PHP and Java
body: <p>I attempted to create a PHP script which determines if the server (the computer which hosts the java program listed below) is running or not, If it is, the php funciton should return true, if not it returns false. </p>
  
  <p>Here is the server:</p>
  
  <pre><code>package darestium.minecraft.server;
  
  import java.io.BufferedReader;
  import java.net.*;
  import java.io.*;
  
  public class Server {
      private static ServerSocket socket;
  
      private static Socket connection;
      private static String command       = new String();
      private static String responseStr   = new String();;
  
      private static int port = 4343;
  
      public static void main(String args[])  {
          System.out.println("Signal Server is running.");
  
          try  {
              socket = new ServerSocket(port);
  
              while (true)  {
                  connection = socket.accept();
  
                  InputStreamReader inputStream = new InputStreamReader(connection.getInputStream());
                  DataOutputStream response = new DataOutputStream(connection.getOutputStream());
                  BufferedReader input = new BufferedReader(inputStream);
  
                  command = input.readLine();
  
                  response.writeBytes(responseStr);
                  response.flush();
                  //response.close();
  
                  System.out.println("Running");
              }
          } catch (IOException e)  {
              System.out.println("Fail!: " + e.toString());
          }
  
          System.out.println("Closing...");
      }
  }
  </code></pre>
  
  <p>And here is the client:</p>
  
  <pre><code>&lt;?
      function isRunning()  {
          $address = 'darestium.dyndns-free.com';
          $port = 4343;
          $socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
          $message = 'loolololol';
  
          try {
              socket_connect($socket, $address, $port);
  
              $status = socket_sendto($socket, $message, strlen($message), MSG_EOF, $address, $port);
  
              if ($status != false)  {
                  return true;
              }
  
              return false;
          } catch (Exception $e)  {
              return false;
          }
      }
  ?&gt;
  </code></pre>
  
  <p>The following are the error messages that show up on the php page that echos out the result of the function like so:</p>
  
  <pre><code>include('server.php');
  echo isRunning();
  </code></pre>
  
  <p>Then the error messages:</p>
  
  <pre><code>Warning: socket_connect() [function.socket-connect]: unable to connect [0]: No connection could be made because the target machine actively refused it. in C:\Users\darestium\Documents\Portables\xampp\htdocs\darestium\minecraftserver.php on line 9
  
  Notice: Use of undefined constant MSG_EOF - assumed 'MSG_EOF' in C:\Users\darestium\Documents\Portables\xampp\htdocs\darestium\minecraftserver.php on line 11
  
  Warning: socket_sendto() expects parameter 4 to be long, string given in C:\Users\darestium\Documents\Portables\xampp\htdocs\darestium\minecraftserver.php on line 11
  </code></pre>
  
  <p>I was wondering how I could fix this issue. Also, I would like to be able to send messages to the server, any ideas how I could do this? I am basing this off <a href="https://stackoverflow.com/questions/9714694/simple-java-tcp-server-and-php-client-problems">Simple Java TCP Server and PHP Client Problems</a></p>
  
  <p>Note: that I am very new to Sockets, and Server/Client communication.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>@VideanuAdrian OK, just port forwarded the port 4343 and it no longer shows up with the first error, but the function always seems to return false, and the last two errors still show.</p>