import java.net.*; import java.io.*; import sdsu.io.ASCIIInputStream; class SumServer { static int portNumber = 4444; public static void main(String args[]) { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket( portNumber ); } catch (IOException e) { System.out.println("Could not listen on port: " + portNumber + ", " + e); System.exit(1); } Socket clientSocket = null; try { clientSocket = serverSocket.accept(); } catch (IOException e) { System.out.println("Accept failed: " + portNumber + ", " + e); System.exit(1); } try { InputStream inputSocketStream; inputSocketStream = clientSocket.getInputStream(); BufferedInputStream bufferedInputSocketStream; bufferedInputSocketStream = new BufferedInputStream( inputSocketStream ); ASCIIInputStream cin; cin = new ASCIIInputStream( bufferedInputSocketStream ); OutputStream outputSocketStream; outputSocketStream = clientSocket.getOutputStream(); BufferedOutputStream bufferedOutputSocketStream; bufferedOutputSocketStream = new BufferedOutputStream( outputSocketStream, 1024 ); PrintStream cout; cout = new PrintStream( bufferedOutputSocketStream, false); cout.print("Enter a number: "); cout.flush(); int one = cin.readInt(); cout.print("Enter another number: "); cout.flush(); int two = cin.readInt(); cout.println("The sum is " + (one + two)); cout.flush(); cin.close(); cout.close(); } catch (IOException e) { e.printStackTrace(); } } }
import java.net.*; import java.io.*; import sdsu.io.ASCIIInputStream; class SumServer { static int portNumber = 4444; public static void main(String args[]) { ServerSocket serverSocket; serverSocket = serverSocketOn( portNumber ); Socket clientSocket = null; InputStream fromClient; OutputStream toClient; while ( true ) { clientSocket = acceptClientRequestOn( serverSocket ); fromClient = inputStreamFromClient( clientSocket ); toClient = outputStreamToClient( clientSocket ); processClientRequest( fromClient, toClient ); } }
protected static ServerSocket serverSocketOn( int portNumber ) { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket( portNumber ); } catch (IOException e) { System.out.println("Could not listen on port: " + ", " + e); System.exit(1); } return serverSocket; } protected static Socket acceptClientRequestOn( ServerSocket server) { Socket clientSocket = null; try { clientSocket = server.accept(); } catch (IOException e) { System.out.println("Accept failed: " + ", " + e); System.exit(1); } return clientSocket; }
protected static InputStream inputStreamFromClient( Socket client ) { InputStream inputSocketStream; BufferedInputStream bufferedInputSocketStream = null; try { inputSocketStream = client.getInputStream(); bufferedInputSocketStream = new BufferedInputStream( inputSocketStream ); } catch ( IOException e) { // What to do here? } return bufferedInputSocketStream; }
protected static OutputStream outputStreamToClient( Socket client ) { OutputStream outputSocketStream; BufferedOutputStream bufferedOutputSocketStream = null; try { outputSocketStream = client.getOutputStream(); bufferedOutputSocketStream = new BufferedOutputStream( outputSocketStream, 1024 ); } catch ( IOException e) { // What to do here? } return bufferedOutputSocketStream; }
protected static void processClientRequest( InputStream in, OutputStream out ) { try { ASCIIInputStream cin = new ASCIIInputStream( in ); PrintStream cout = new PrintStream( out ); cout.print("Enter a number: "); cout.flush(); int one = cin.readInt(); cout.print("Enter another number: "); cout.flush(); int two = cin.readInt(); cout.println("The sum is " + (one + two)); cout.flush(); cin.close(); cout.close(); } catch (IOException e) { e.printStackTrace(); } } }
class SumHandler implements Runnable { protected ASCIIInputStream cin; protected PrintStream cout; public SumHandler( InputStream in, OutputStream out ) { cin = new ASCIIInputStream( in ); cout = new PrintStream( out, false); } public void run () { try { cout.print("Enter a number: "); cout.flush(); int one = cin.readInt(); cout.print("Enter another number: "); cout.flush(); int two = cin.readInt(); cout.println("The sum is " + (one + two)); cout.flush(); cin.close(); cout.close(); } catch (IOException e) { // What to do here? } } }
New version of processClientRequest
protected static void processClientRequest( InputStream in, OutputStream out ) { SumHandler adder = new SumHandler( in, out); Thread serverThread = new Thread( adder, "Add" ); serverThread.start(); }