CIAO prima di tutto auguri di buon natale a tutto il forum.
ho da fare un esercizio di comunicazione client server tcp.
Un asta elettronica
il client:
-ha un budget disponibile
-il client puo volere sapere dal server l offerta migliore attiva(premo 1)
-puo rilanciare l offerta (premo 2)
-puo uscire(premo 0)
al server gli arriva un intero
-se vale 1 vuol dire che devo mandare al client la migliore offerta attiva
-se vale 2 vuol dire che il client sta cercando di rilancire l offerta,quindi leggo un altro intero lo confronto con la migliore offerta e in caso positivo aggiorno l offerta migliore con quella mandata dal client e gli invio al client n boolean=true per avvertirlo dell esito positivo dell offerta, in caso negativo gli mando false
per l input da linea di comando mi servo di una classe Input consegnata dal prof;
gli interi che via via si mandando client e server devono essere mandati come oggetti(per esercitarmi).
per stampare ogni volta il menu mi appoggio a una classe utils:
ecco il mio codicecodice:public class utils { public static void stampa(){ System.out.println("premere i seguenti tasti"); System.out.println(" 1 : conoscere offerta massima corrente"); System.out.println(" 2 : Rilanciare l offerta"); System.out.println("0 : uscire"); } }
:
client
codice:import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; public class cli { public static void main(String []args){ int budget=5000;//IMPOSTAZIONE BUDGET int porta=3575;//PORTA SERVER try { InetAddress ia=InetAddress.getLocalHost(); Socket socket=new Socket(ia,porta); System.out.println("Benvenuti"); utils.stampa(); int scelta; scelta=Input.readInt(); ObjectInputStream in=new ObjectInputStream(socket.getInputStream()); ObjectOutputStream op=new ObjectOutputStream(socket.getOutputStream()); while(scelta!=0){ switch(scelta){ case 1: System.out.println(""); System.out.println(""); op.writeObject((Integer)scelta); int max=(Integer)in.readObject(); System.out.println("Offerta massima corrente é"+max); System.out.println(""); System.out.println(""); utils.stampa(); scelta=Input.readInt(); case 2: op.writeObject((Integer)scelta); System.out.println(""); System.out.println(""); System.out.println("Di quanto vuoi rilanciare ?"); int rilancio=Input.readInt(); op.writeObject((Integer)rilancio); boolean operazione=(Boolean)in.readObject(); if(operazione) System.out.println("operazione andata in porto"); else System.out.println("operazione non andata in porto rilancio < offerta attuale"); System.out.println(""); System.out.println(""); utils.stampa(); scelta=Input.readInt(); } } } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
CODICE SERVER:
codice:import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.ServerSocket; import java.net.Socket; public class ServerAsta { public static void main(String []args){ int current=0;//MIGLIORE OFFERTA int comando; ServerSocket server; try { server = new ServerSocket(3575); Socket clientsocket=server.accept(); ObjectInputStream in = new ObjectInputStream(clientsocket.getInputStream()); ObjectOutputStream outuput = new ObjectOutputStream( clientsocket.getOutputStream()); while(true){ comando=(Integer)in.readObject(); switch (comando){ case 1: outuput.writeObject((Integer)current); case 2: int rilancio=(Integer)in.readObject(); if(rilancio >current) { outuput.writeObject((Boolean)true); current=rilancio; } else outuput.writeObject((Boolean)false); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
SE MANDO IN ESECUZIONE (PRIMA SERVER POI CLIENT!):
sia se premo 1 che se premo 2 nella shell del client, il client mi rimane in attesa e non arriva nessuna risposta del server
PLEASE HELP ME

Rispondi quotando

