cs205: engineering software?
(none)
20 September 2010

CS205 Notes 37 (17 November 2006)

Project Progress Reports

Remember that your project progress reports are due the Monday after Thanksgiving. A good progress report should show that some core functionality of your program is complete (working and tested), and give a clear idea of what you have left to do and any unresolved issues. See Class 36 notes for details on the sections your progress report should contain.

Network Programming

Why does network programming provide good opportunities for data abstraction?







What are the advantages and disadvantages of a "mesh" network topology over a "star" network topology? If you are building a multi-player Snakes game which should you use?







java.net.ServerSocket

public ServerSocket(int port) throws IOException
   EFFECTS: Initializes this to a new server socket on port.  
      If the socket cannot be created, throws IOException.

public Socket accept() throws IOException 
Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made. A new Socket s is created and, if there is a security manager, the security manager's checkAccept method is called with s.getInetAddress().getHostAddress() and s.getPort() as its arguments to ensure the operation is allowed. This could result in a SecurityException.
java.net.Socket
public Socket(String host, int port) throws IOException, UnknownHostException     
   EFFECTS: Creates a stream socket and connects it to the specified 
      port number on the named host. If the specified host
      is null it is the loopback address.

public OutputStream getOutputStream() throws IOException 
public InputStream getInputStream() throws IOException 

Server Code

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

public class Server {
   static public void main(String args[]) {
      ServerSocket listener;

      try {
         listener = new ServerSocket(8000);
      } catch (IOException ioe) {
         System.err.println("Cannot open server socket: " + ioe);
         return;
      }

      while (true) {
         try {
            System.out.println("Server: waiting for connection...");
            Socket connect = listener.accept();
            PrintWriter out = new PrintWriter(connect.getOutputStream(),
                                             true);
            BufferedReader in = new BufferedReader(new InputStreamReader(
                                         connect.getInputStream()));
            String inString;
            while ((inString = in.readLine()) != null) {
               // HTTP request: GET  
               if (inString.startsWith("GET ")) {
                  out.println("HTTP/1.1 200 OK");
                  out.println("Content-Type: text/html; charset=utf-8");
                  ...
              }
           }
           connect.close();
        } catch (IOException ioe) {
            System.err.println("Cannot accept: " + ioe);
        }
     }
   }
}
How secure would a server built like this be against malicious clients?

Client Code

import java.net.Socket;
import java.io.*;
import java.net.UnknownHostException;

public class Client {
   public static void main(String[] args) {
      Socket connect;

      try {
         connect = new Socket("www.microsoft.com", 80); // 80 = http
         System.out.println("Connected: " + connect);
      } catch (UnknownHostException uhe) {
         System.err.println("Cannot find host: " + uhe);
         return;
      } catch (IOException ioe) {
         System.err.println("Cannot open connection: " + ioe);
         return;
      }

      try {
         PrintWriter out = new PrintWriter (connect.getOutputStream(), true);       
         BufferedReader in = new BufferedReader(new InputStreamReader(
               connect.getInputStream()));
         out.println("GET / HTTP/1.0\r\n");
         String inString;
         while ((inString = in.readLine()) != null) {
             System.out.println (inString);
         }         
      } catch (IOException ioe) {
         System.err.println("IO Exception: " + ioe);
      }
      
      System.out.println ("Closing connection...");
      try {
         connect.close();
      } catch (IOException ioe) {
         System.err.println("Error closing: " + ioe);
      }
   }
}

Links

Web Client code: Client.java
Sun's Networking Tutorial