Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2012
    Messaggi
    31

    [Java] Invio oggetti UDP

    Buongiorno! Da qualche giorno ho un problema con UDP.
    Ho un client che manda un oggetto e un file. Il server deve ricevere l'oggetto e successivamente il file.
    Il problema è che quando dal client mando un secondo oggetto e quindi un secondo file il server lancia una StreamCorruptedException.

    Client:
    codice:
    public class SenderUDP implements Runnable {
        private java.net.DatagramSocket clientsocket;
        private ObjectOutputStream out;
        private int port;
        private InetAddress ip;
        private Packet objToSend;
        private ByteArrayOutputStream baos;
       
        
        public SenderUDP(String ip, int port, Packet p) throws UnknownHostException{
            this.ip =InetAddress.getByName(ip);
            this.port = port;
            this.objToSend = p;
            this.out = null;
            this.clientsocket = null;
        }
        
        @Override
        public void run(){
            try{
                clientsocket = new DatagramSocket ();
                System.out.println("Sono dentro il senderUDP");
                byte[] sendData;                    
                baos = new ByteArrayOutputStream(1024);
                out = new ObjectOutputStream(baos);
                out.writeObject(objToSend);
                sendData = baos.toByteArray();           
                //Invio un pacchetto che contiene sendData byte, di lunghezza sendData.lenght, con ip e porta
                DatagramPacket sendpacket = new DatagramPacket(sendData,sendData.length,ip,port);            
                clientsocket.send(sendpacket);                 
                System.out.println("Ho mandato il pacchetto con UDP");
                out.flush();
                if(this.objToSend.getOP() == 1){
                    byte[] buf = new byte[1024];                
                    int read;
                    ByteArrayOutputStream bas = new ByteArrayOutputStream((int)this.objToSend.getFile().length());                
                    FileInputStream fis = new FileInputStream(objToSend.getFile());
                    while((read = fis.read(buf)) != -1){
                        bas.write(buf, 0, read);
                    }
                    DatagramPacket sendfile = new DatagramPacket(bas.toByteArray(), bas.toByteArray().length, ip, port);
                    clientsocket.send(sendfile);
                }           
                out.close();             
            }
            catch(UnknownHostException uhe) {
                System.err.println(uhe.getMessage());
            }
            catch(IOException ioe) {
                System.err.println(ioe.getMessage());
            }
        }
    }
    Server:
    codice:
    class ServerUDP implements Runnable {
        private DatagramSocket socket;
        private int port;
        private Controller controller;
        private byte[] buffer;
        private DatagramPacket packet;
        private Packet p;
        private ObjectInputStream ois;
        
        public ServerUDP(int port, Controller controller){
            this.socket = null;
            this.port = port;
            this.controller = controller;
        }
        
        @Override
        public void run() {
            try {
                    socket = new DatagramSocket(port);
                } catch (SocketException ex) {
                    Logger.getLogger(ServerUDP.class.getName()).log(Level.SEVERE, null, ex);
                }
            while(true){               
                buffer = new byte[1000000];
                packet = new DatagramPacket(buffer,buffer.length);
                System.out.println("Ascolto UDP!");
                try {                
                    socket.receive(packet);
                    System.out.println(packet);
                    System.out.println("1");
                } catch (IOException ex) {
                    Logger.getLogger(ServerUDP.class.getName()).log(Level.SEVERE, null, ex);
                }
                System.out.println("Packet UDP Received!");
                try {
                    ois = new ObjectInputStream(new ByteArrayInputStream(buffer));
                } catch (IOException ex) {
                    Logger.getLogger(ServerUDP.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    p = (Packet) ois.readObject();
                    System.out.println("Pacchetto/Evento arrivato con UDP!");
                    System.out.println(p);
                } catch (IOException | ClassNotFoundException ex) {
                    Logger.getLogger(ServerUDP.class.getName()).log(Level.SEVERE, null, ex);
                }
                if(p.getOP() == 1){
                    Thread t = new Thread(new FilesManager(socket,p,false, controller));
                    t.start();
                }
                controller.enqueue(p);
                try {
                    ois.close();
                } catch (IOException ex) {
                    Logger.getLogger(ServerUDP.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
    }
    Sostanzialmente il server dovrebbe stampare System.out.println("1"); ogni volta che riceve un pacchetto. Per il primo funziona, quando mando un secondo pacchetto la stampa avviene due volte. Quindi è come se il server entri in socket.receive(packet) due volte invece che una.
    L'eccezione viene lanciata da queste due righe:
    codice:
    ois = new ObjectInputStream(new ByteArrayInputStream(buffer));
    codice:
    p = (Packet) ois.readObject();
    Spero possiate darmi qualche consiglio. Ringrazio in anticipo

  2. #2
    Utente di HTML.it
    Registrato dal
    Jan 2012
    Messaggi
    31
    Salve! A quanto pare nessuno su questo forum ha mai avuto bisogno di trasferire file in UDP
    Io comunque ho risolto il mio problema. Esiste un protocollo TFTP che fa proprio ciò di cui avevo bisogno.
    http://en.wikipedia.org/wiki/Trivial...nsfer_Protocol
    Ed ecco una piccola implementazione in java.
    http://www.vorlesungen.uni-osnabruec...28Nov96/1.html
    Con queste classi si può solo trasferire un file da un servertftp ad un client tftp, ma ovviamente smanettando un pò il codice si potrà fare molto molto di più. Spero possa essere d'aiuto a qualcuno. Ringrazio lo stesso

  3. #3
    Grandeee. Grazie. Io ho bisogno proprio della stessa cosa e se non era per te che postavi problema e soluzione non ci cavavo le gambe.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.