• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
 
Esempio di una semplice applicazione
La seguente applicazione realizzata con le tre tecniche di comunicazione (Socket, RPC, RMI) consiste in un server che può fornire due tipi di servizi a un client che si connette ad esso: la data e l'ora di sistema. Il seguente esempio hal'obiettivo di mostrare quanto detto nel paragrafo 1.3, senza badare ad ulteriori dettagli implementativi.
Socket
 Implementazione del client (Client.java):
import java.io.*;import java.net.*;public class Client{public static void main( String[] args ) throws IOException{Socket socket = null;PrintStream out = null;DataInputStream in = null;String server = "127.0.0.1";try {socket = new Socket( server, 4321 );out = new PrintStream( socket.getOutputStream() );in = new DataInputStream( socket.getInputStream() );}catch( UnknownHostException e ){System.err.println("Impossibile collegarsi al server " + server );System.exit(1);}catch( IOException e ){System.err.println( "Impossibile comunicare con il server " + server );System.exit(1);}DataInputStream stdIn = new DataInputStream( System.in );String fromServer;String fromUser;while( ( fromServer = in.readLine() ) != null ){System.out.println( "Server: " + fromServer );if( fromServer.equals( "fine collegamento" ) )break;System.out.print( "> " );fromUser = stdIn.readLine();if( fromUser != null ){System.out.println("Client: " + fromUser);out.println(fromUser);}}out.close();in.close();stdIn.close();socket.close();}}
 
 Implementazione del server (Server.java):
import java.net.*;import java.io.*;public class Server{public static void main(String[] args) throws IOException{int porta = 4321;ServerSocket serverSocket = null;try {serverSocket = new ServerSocket( porta );}catch (IOException e){System.err.println( "Impossibile ascoltare sulla porta " + porta );System.exit(1);}Socket clientSocket = null;try {clientSocket = serverSocket.accept();}catch (IOException e){System.err.println( "Impossibile accettare il collegamento" );System.exit(1);}PrintStream out = new PrintStream( new BufferedOutputStream(clientSocket.getOutputStream(), 1024), false);DataInputStream in = new DataInputStream( new BufferedInputStream(clientSocket.getInputStream() ) );String inputLine, outputLine;Protocol p = new Protocol();outputLine = p.processaInput(null);out.println(outputLine);out.flush();while( (inputLine = in.readLine() ) != null){outputLine = p.processaInput(inputLine);out.println(outputLine);out.flush();if (outputLine.equals("fine collegamento") ) break;}out.close();in.close();clientSocket.close();serverSocket.close();}}
 
 Implementazione del protocollo di comunicazione (Protocol.java):
import java.net.*;import java.io.*;import java.util.Date;import java.text.*;public class Protocol{private boolean collegato = false;public String processaInput( String input ){String output = null;if( !collegato ){output = "pronto a ricevere richieste";collegato = true;}else{if( input.equalsIgnoreCase("DATA"))output = "" + DateFormat.getDateInstance().format( new Date() );else if( input.equalsIgnoreCase("ORA"))output = "" + DateFormat.getTimeInstance().format( new Date() );elseoutput = "fine collegamento";}return output;}}
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...