Aplicatii cu fire de executie (3) - Server de Web simplificat

Salvati urmatoarele secvente de cod in fisiere separate, grupate in directoare separate,
compilati si executati programele, urmand procedura descrisa mai sus.
Porniti mai intai programul WebServer
Porniti dupa aceea programul TestClientWebServer, eventual 2 instante in acelasi timp (din 2 console diferite)
Observati ce printeaza fiecare program


Server de web simplificat

File: Handler.java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class Handler {

	void process(Socket s) {
		DataInputStream  in = null;
		DataOutputStream out = null;
		try {
			in = new DataInputStream(s.getInputStream());
			out = new DataOutputStream(s.getOutputStream());
			int request = in.readInt();
			System.out.println("[SERVER HANDLER]: read from input stream: " + request);
			int result = -request;
			out.writeInt(result);
			System.out.println("[SERVER HANDLER]: write to output stream: " + result);

		}
		catch (IOException ex) {}
		finally {
			try { if (in != null)  in.close();
			} catch(IOException ignore) {};
			try { if (out != null) out.close();
			} catch(IOException ignore) {};
			try { s.close();
			} catch(IOException ignore) {System.out.println("[SERVER HANDLER] Close port: Error closing port");};
		}
	}
}
File: WebServer.java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class WebServer implements Runnable {
	static final int PORT = 1042;
	final int TIMEOUT = 30000;
	Handler handler = new Handler();
	ServerSocket socket = null;

	public void run() {
		try {
			socket = new ServerSocket(PORT);
			socket.setSoTimeout(TIMEOUT);
			System.out.println("[SERVER]Socket: " + socket.getLocalPort() + " " + socket.getSoTimeout());
			System.out.println("[SERVER] Web server started and waiting for requests ...");
			while(true){
				final Socket connection = socket.accept();
				new Thread(new Runnable() {
					public void run() {
						System.out.println("[SERVER] N waiting for requests ...");
						handler.process(connection);
					}
				}).start();
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	public static void main (String args[]) throws IOException{
		WebServer webServer = new WebServer();
		webServer.run();
	}
}

File: TestClientWebServer.java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;


public class TestClientWebServer {

	static Socket socket = null;
	static DataInputStream  in = null;
	static DataOutputStream out = null;
	static int request = -1;
	static int response = -1;
	static final int PORT = 1042;
	static final int REQ = 1024;

	public static void main (String args[]) throws IOException {
		request = REQ;
		for(int i = 0; i < 3; i++) {
			socket = new Socket("localhost",PORT);
			try {
				in = new DataInputStream(socket.getInputStream());
				out = new DataOutputStream(socket.getOutputStream());
				out.writeInt(request);
				System.out.println("[CLIENT]: wrote to the output stream: " + request);
				response = in.readInt();
				System.out.println("[CLIENT]: read from the input stream: " + response);
				request = response * (int)(10 * Math.random());
			}
			catch (IOException ex) {}
			finally {
				try { if (in != null)  in.close();
				} catch(IOException ignore) {};
				try { if (out != null) out.close();
				} catch(IOException ignore) {};
				try { socket.close();
				} catch(IOException ignore) {System.out.println("[SERVER HANDLER] Close port: Error closing port");};
			}
		}


	}

}