PHP Tutorial

Important links:
http://www.cs.washington.edu/lab/cgi.html (information to use PHP at UW)
http://www.phpcomplete.com/content.php?id=66 (PHP form variables)
http://www.php.net/manual/en/ (PHP manual)
http://www.php.net/manual/en/function.fsockopen.php (PHP manual - fsockopen())

The first thing to do is read up on how to get PHP to work and make sure that you have the necessary accounts. This is the first link above.

Then I'd suggest trying to get this example to work. To do this, copy the java server example, compile and run it, and test it at the command line as given in the comments. It's probably a good idea to try another port. Note that the server terminates after a single connection (not much of a server!) This means you need to restart it each time, or extend it in some way to accept multiple requests and handle them with multiple threads. For more information on how to do this (and almost anything Java related), I highly recomment "Core Java - Volume II - Advanced Features" by Horstmann and Cornell.

Once you get the server to work, then create the html form page and the php_server_test.php page (this should be done on abstract.cs.washington.edu)

Then you're done and ready to test. Start the server, go to the forms page, and see if you can submit a string and have the server modify it and return it on the new page. Note that each time you'll have to restart the server.

Example of a java server:

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

/* This server takes a line (terminatd by '\n') and returns a new line
   appending '** ' and terminating the line with ' **'

javac SimpleServer.java
java SimpleServer
telnet <server/IP> 8080
<type a line>
<output result>
program terminates...
*/

public class SimpleServer {
  public static void main(String[] args) {
    try {
      ServerSocket s = new ServerSocket(8080);
      Socket incoming = s.accept();
      BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
      PrintWriter out = new PrintWriter(incoming.getOutputStream(), true);

      String line = in.readLine();

      System.out.println(line);

      String newLine = "** " + line + " **";
      out.println(newLine);

      incoming.close();
    } catch(Exception e) {
      System.out.println(e);
    }
  }
}

Example of a form page:

<form action="php_server_test.php" method="POST">
 Query: <input type="text" name="val" value="default" />
 <input type="submit">
</form>

Example of a php_server_test.php page: (connecting to server above)

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php
$address = '128.95.1.131';
$port = 8080;
$fp = fsockopen($address, $port, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br>\n";
} else {
  fputs ($fp, $_POST['val']);
  fputs ($fp, "\n");
  while (!feof($fp)) {
    echo fgets ($fp,128);
  }
  fclose ($fp);
}
 ?>
 </body>
</html>
</pre>