V I S U A L I Z Z A D I S C U S S I O N E |
shadow89 |
Inserito il - 21/11/2009 : 17:34:02 Esercizio di Laboratorio MathServer e MathClient svolto
//MathClient Class
import java.io.*; import java.net.*;
public class MathClient { public static void main(String[] args) throws IOException {
System.out.println("CLIENT IS READYING");
Socket MathSocket = null; PrintWriter out = null; BufferedReader in = null;
try { MathSocket = new Socket("localhost", 4321); out = new PrintWriter(MathSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(MathSocket.getInputStream())); } catch (UnknownHostException e) { System.exit(1); } catch (IOException e) { System.exit(1); }
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); String fromServer; String fromUser; boolean control = false;
while ((fromServer = in.readLine()) != null && control == false ) { if (fromServer.equals("CONNECTED")){ System.out.println(fromServer); fromUser = stdIn.readLine(); if (fromUser.equals("SUM")) { out.println(fromUser); while ((fromServer = in.readLine()) != null) { if (fromServer.equals("OP1")){ System.out.println("Operatore Primo"); fromUser = stdIn.readLine(); out.println(fromUser); } while ((fromServer = in.readLine()) != null) { if (fromServer.equals("OP2")){ System.out.println("Operatore Secondo"); fromUser = stdIn.readLine(); out.println(fromUser); } while ((fromServer = in.readLine()) != null) { System.out.println(fromServer); break; } break; } break; } } else if (fromUser.equals("SUB")) { out.println(fromUser); while ((fromServer = in.readLine()) != null) { if (fromServer.equals("OP1")){ System.out.println("Operatore Primo"); fromUser = stdIn.readLine(); out.println(fromUser); } while ((fromServer = in.readLine()) != null) { if (fromServer.equals("OP2")){ System.out.println("Operatore Secondo"); fromUser = stdIn.readLine(); out.println(fromUser); } while ((fromServer = in.readLine()) != null) { System.out.println(fromServer); break; } break; } break; } } else if (fromUser.equals("QUIT")) { out.println(fromUser); while ((fromServer = in.readLine()) != null) { if (fromServer.equals("CONNESSIONE TERMINATA")){ System.out.println(fromServer); control = true; break; } } } } } out.close(); in.close(); stdIn.close(); MathSocket.close(); } }
questa invece è la classe mathserver modificata per gestire più connessioni
//MathServer import java.net.*; import java.io.*;
public class MathServer{
public void start() throws Exception{
System.out.println("SERVER READY"); ServerSocket serverSocket = null;
try { serverSocket = new ServerSocket(4321); } catch (IOException e) { System.exit(1); }
while (true){ System.out.println("In attesa di chiamate dai Client... "); Socket socket = serverSocket.accept(); System.out.println("Ho ricevuto una chiamata di apertura da:\n" + socket); Protocol serverThread = new Protocol(socket); serverThread.start(); } } public static void main(String[] args) throws Exception{ MathServer server = new MathServer(); server.start(); } }
e successivamente la classe protocol ove vi è implementato il protocollo
//Prtotocol class import java.net.*; import java.io.*;
public class Protocol extends Thread { private Socket clientSocket; public Protocol(Socket socket){ this.clientSocket = socket; }
public void run (){ try { PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String inputLine; String outputLine; outputLine = "CONNECTED"; out.println(outputLine);
boolean control = false;
while ((inputLine = in.readLine()) != null && control == false) { if (inputLine.equals("SUM")){ int OP1; int OP2; outputLine = "OP1"; out.println(outputLine); while ((inputLine = in.readLine()) != null) { OP1 = Integer.parseInt(inputLine); outputLine = "OP2"; out.println(outputLine); while ((inputLine = in.readLine()) != null) { OP2 = Integer.parseInt(inputLine); int rst = OP1 + OP2; outputLine = "il risulato è " + rst; out.println(outputLine); break; } break; } } else if (inputLine.equals("SUB")){ int OP1; int OP2; out.println("OP1"); while ((inputLine = in.readLine()) != null) { OP1 = Integer.parseInt(inputLine); out.println("OP2"); while ((inputLine = in.readLine()) != null) { OP2 = Integer.parseInt(inputLine); int rst = OP1 - OP2; out.println("il risulato è " + rst); break; } break; } } else if (inputLine.equals("QUIT")){ out.println("CONNESSIONE TERMINATA"); control = true; break; } outputLine = "CONNECTED"; out.println(outputLine); } out.close(); in.close(); clientSocket.close();
} catch (IOException e) { e.printStackTrace(); } } } |
1 U L T I M E R I S P O S T E (in alto le più recenti) |
supernova87 |
Inserito il - 24/11/2009 : 20:25:15 mamma mia...davvero imbarazzante... |
|
|