Dunque il problema principalmente è capire come far comunicare server e client più volte, cioè non si tratta della solita richiesta singola al server che poi risponde al client e finisce la, creando dei metodi, per esempio l’invio dell’offerta relativa ad un asta dopo che si è visualizzato l’elenco delle aste disponibili e si è fatto il login…

Qui di seguito vi allego il codice che fino ad ora ho scritto, consapevole che deve essere modificato:

codice:
 
*****************************CLASSE  CLIENT*******************


    
package AstaCrittografata;

/**
 *
 * @author Luca
 */
import java.io.*;
import java.net.*;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
import javax.crypto.Cipher;
import AstaCrittografata.Inserzione;

/**
 * Questo è un esempio di un'applicazione che garantisce la segretezza della
 * comunicazione, in quanto cifra i dati da inviare verso il server con la
 * chiave pubblica comunicat dal server stesso. Solo il server sarà capace di
 * decifrare questi dati, in quanto è l'unico che dispone della chiave privata
 * accoppiata alla chiave pubblica.
 *
 * @author Raffaele Giordanelli
 *
 */
class AstaClient {
    GestoreUtenti gestU=new GestoreUtenti();
    int serverPort;
    int myPort;
    InetAddress groupAddress;
    InetAddress serverAddress;
    Socket s;
  
    // Cryptography: begin
    // Socket per richiedere la chiave pubblica del asta server
    Socket gks;
    // Porta del server e socket a cui richiedere la chiave pubblica
    int getKeyServerPort;
    // Cipher per cifrare i dati da inviare al server
    Cipher cipher;
    // Cipher per decifrare i dati da inviati dal server
    Cipher decipher;
    // Cryptography: end
    protected Utente u = new Utente();
    protected boolean Uloggato;

    public AstaClient(Utente u, InetAddress gAddress, InetAddress server, int sPort, int mPort, int gkServerPort) {
        this.u=u;
        groupAddress = gAddress;
        serverAddress = server;
        serverPort = sPort;
        myPort = mPort;
        getKeyServerPort = gkServerPort;
        try {
            s = new Socket(serverAddress, serverPort);

            // Cryptography: begin
            // Inizializzazione del socket per richiedere la chiave pubblica del
            // asta server
            gks = new Socket(serverAddress, getKeyServerPort);
            // Cryptography: end

        } catch (IOException ioe) {
            System.out.println(ioe);
        }
        Uloggato = false;
    }

    /**
     * Legge la chiave pubblica inviata dal server, e tramite questa inizializza
     * i due cifrari RSA in modalità cifratura e decifratura
     */
    public void makeCipher() {
        try {
            // Cryptography: begin
            // Recupero gli Stream di comunicazione
            InputStream inGetKey = gks.getInputStream();
            OutputStream outGetKey = gks.getOutputStream();

            // Richiedo la chiave publica del Asta Server
            outGetKey.write(1);

            // Salvo la chiave inviata dal server in publicKeyBytes
            byte[] publicKeyBytes = new byte[1000];
            int numByte = inGetKey.read(publicKeyBytes);
            publicKeyBytes = Arrays.copyOf(publicKeyBytes, numByte);

            // Inizializza convertitore da X.509 a chiave pubblica
            X509EncodedKeySpec ks = new X509EncodedKeySpec(publicKeyBytes);
            // Inizializza un KeyFactory per ricreare la chiave usando RSA
            KeyFactory kf = KeyFactory.getInstance("RSA");
            // Crea una chiave pubblica usando generatePublic di KeyFactory in
            // base la chiave decodificata da ks
            PublicKey publicKey = kf.generatePublic(ks);

            // Inizializzo un cifrario che usa come algoritmo RSA, come modalità
            // ECB e come padding PKCS1
            cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

            // Lo inizializzo dicendo modalità di codifica e chiave pubblica da
            // usare
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);

            // Inizializzo un cifrario che usa come algoritmo RSA, come modalità
            // ECB e come padding PKCS1
            decipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

            // Lo inizializzo dicendo modalità di codifica e chiave pubblica da
            // usare
            decipher.init(Cipher.DECRYPT_MODE, publicKey);

            // Cryptography: end
        } catch (Exception e) {
            System.out.println(e);
        }

    }

    
    public boolean InviaLoginClient(String user, String password) throws IOException {
         
  
        makeCipher();
        String e = "";
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            OutputStream out = s.getOutputStream();

            byte[] dati = cipher.doFinal((" " + user +" "+ password).getBytes());
            out.write(dati);

            e = in.readLine();
        } catch (Exception err) {
            System.out.println(err);
        }
        
        
         if(e.equals("true")){
             Uloggato=true;
             return true;
            }
        return false;

    }
//******FINIRE CON REGISTRAZIONE UTENTE E CREAZIONE SE NON è LOGGATO
    public boolean CreaOfferta(int ID, double importo) throws FileNotFoundException, IOException {
      boolean esito=false;
        Uloggato=gestU.verificaPresenza(u.getUserId(), u.getPassword());
        if (Uloggato == false) {
            System.out.println("#ERRORE! - Eseguire il login per fare offerte!");}
            else{
        // controllare il ricevi prezzo corrente
        // Cryptography: begin2
        // Creo il Cipher per criptare i dati inviati
        makeCipher();
        // Cryptography: end

        String e = "";
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            OutputStream out = s.getOutputStream();

            byte[] asta = cipher.doFinal((" " + importo).getBytes());
            out.write(asta);

            e = in.readLine();
        } catch (Exception err) {
            System.out.println(err);
        }
        esito=true;
        return e.equals("Offerta accettata.");
        
        
    }
    return esito ;
    }

    /**
     * Riceve l'elenco dei vincitori inviato dal server in multicast a tutti i
     * client
     */
    public void riceviElenco() {
        try {
            MulticastSocket socket = new MulticastSocket(myPort);
            socket.joinGroup(groupAddress);

            byte[] buf = new byte[128];
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            socket.receive(packet);

            // Cryptography: begin
            String elenco = new String(decipher.doFinal(packet.getData()));
            // Cryptography: end

            System.out.println("Elenco vincitori: ");
            System.out.println(elenco);

        } catch (IOException ioe) {
            System.out.println(ioe);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

     public void riceviPrezzoCorrente() {
        try {
            MulticastSocket socket1 = new MulticastSocket(myPort);
            socket1.joinGroup(groupAddress);

            byte[] buf = new byte[128];
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            socket1.receive(packet);

            // Cryptography: begin
            String prezzo = new String(decipher.doFinal(packet.getData()));
            // Cryptography: end

            System.out.println("PrezzoCorrente ");
            System.out.println(prezzo);

        } catch (IOException ioe) {
            System.out.println(ioe);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
    
    
    
    public static void main(String[] args) throws FileNotFoundException, IOException {
       Utente uTemp=new Utente();
       Utente u=uTemp.RegistrazioneU();
        int serverPort = 8001;
        int myPort = 8002;

        // Cryptography: begin
        int getKeyServerPort = 8443;
        // Cryptography: end
        Orario inizio = new Orario(29, 5, 2012, 18, 1);
        Orario fine = new Orario(29, 5, 2012, 18, 5);
        int id = 1;
        String s = "seggia";
        double base = 10.0;
        double baser = 1.0;
        Inserzione ins1 = new Inserzione(id, s, base, baser, base, inizio, fine);

        try {
            InetAddress group = InetAddress.getByName("230.0.0.1");
            InetAddress server = InetAddress.getByName("127.0.0.1");

            AstaClient client = new AstaClient(u,group, server, serverPort, myPort, getKeyServerPort);


//super(1,"descrizione",10.0,1,inizio,fine);
            double importoff = ((double) (Math.random() * 10)) + 1;
            if (client.CreaOfferta(1, importoff)) {
                client.riceviElenco();
            }


        } catch (UnknownHostException uhe) {
            System.out.println(uhe);
        }

    }// main
}// AstaClient
Ho creato inoltre altre classi (classe GestoreInserzione e classe GestoreUtente) per la gestione di inserzioni e degli utenti su file, oltre alla creazione della classe utente ,

di seguito vi posto la classe Server....