Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: DATAGRAMSOCKET - problem with transfer file in UDP

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default DATAGRAMSOCKET - problem with transfer file in UDP

    Hi, i'm trying to do a program that send a single file from a client to a default directory of a Server.
    The program use the datagramSocket and datagramPacket for the trasfer via UDP.
    The client sent the packet of the file correctly and the server start the receiving but every time the
    Server class crashes after it's receiving 4/5 packet (exactly 8192 byte)
    Can you hel me?
    then i put the code of the 2 class and the 2 output.
    (I'm italian and i translate the output =) )

    OUTPUT CLIENT:

    PROGRAM TRANSFER PACKAGES
    name of file is : Doc1.pdf
    his dimension: 11408 byte

    *** start sending
    - 1024 byte
    - 2048 byte
    - 3072 byte
    - 4096 byte
    - 5120 byte
    - 6144 byte
    - 7168 byte
    - 8192 byte
    - 9216 byte
    - 10240 byte
    - 11264 byte
    - 11408 byte
    *** FILE SUCCESFULL SENDED***
    BUILD SUCCESSFUL (total time: 3 seconds)



    OUTPUT SERVER

    PROGRAM TRANSFER PACKAGES
    START NEW CONNECTION
    Directory: C:\prova\
    Server waiting in port: 9876
    Waiting Client...
    Name of file is : Doc1.pdf
    Dimension : 11408
    His directory : C:\prova\Doc1.pdf
    Start receiving process
    1 - file create : C:\prova\Doc1.pdf QUI FUNZIONA
    2 - until this point all ok: C:\prova\Doc1.pdf
    OK :C:\prova\Doc1.pdf
    - 1024 byte
    OK :C:\prova\Doc1.pdf
    - 2048 byte
    OK :C:\prova\Doc1.pdf
    - 3072 byte
    OK :C:\prova\Doc1.pdf
    - 4096 byte
    OK :C:\prova\Doc1.pdf
    - 5120 byte
    OK :C:\prova\Doc1.pdf
    - 6144 byte
    OK :C:\prova\Doc1.pdf
    - 7168 byte
    OK :C:\prova\Doc1.pdf
    - 8192 byte

    In this point the program go in loop and didn't anything.
    I don't understand how can i resolve it =(
    Please help me

    --- Update ---

    SERVER


    package udp_pacchetti;
     
    import java.io.ByteArrayInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.UnknownHostException;
     
     
     
     
    public class UDP_Server_pacchetti {
     
        DatagramSocket dataSocket;
        DatagramPacket dataPacket;
     
        private int porta;
        private String destinazione;
     
        /****   METODO PRINCIPALE  *****/
        public void connessioneRicezione() throws IOException {
     
            int read = 0;
            int tot = 0;
            String nome = null;
     
            int dimensione = 0;
            porta = 9876;
     
                destinazione = "C:\\prova\\";
                System.out.println("PROGRAMMA DI TRASFERIMENTO IN PACCHETTI");
                System.out.println("AVVIO NUOVA CONNESSIONE");
                System.out.println("La cartella di destinazione è " +destinazione);
     
                // Creo la connessione 
                try {
                    dataSocket = new DatagramSocket(porta);
                    System.out.println("Server in attesa sulla porta: " +porta);
     
                } catch (IOException e) {
                    e.printStackTrace();
                } 
     
                byte[] buf = new byte[1024];
                System.out.println("Attendo un Client ... ");
                dataPacket = new DatagramPacket(buf, buf.length);
     
     
                String nome_file = acquisisci_nome(nome);
                System.out.println("Il nome del file é: " +nome_file);
                int dimensione_file = acquisisci_dimensione(dimensione);
                System.out.println("La dimensione del file é: " +dimensione_file);
     
                File file = new File(destinazione+nome_file);
     
                //FileOutputStream fos = new FileOutputStream(file, true);
     
                String percorso_finale = file.getAbsolutePath();
                System.out.println("Il suo percorso sarà : " +percorso_finale);
                // QUI AVVIAMO IL METODO PER LA RICEZIONE DEL FILE
                ricevi_File(file, nome_file, dimensione_file);
     
        } // FINE CONNESSIONE
     
     
        private void ricevi_File(File file, String name, int dimension) throws IOException 
        {
            System.out.println("Inizio processo di ricezione");
     
            String nome = name;
            int dimensione = dimension;
            int read = 0;
            int tot = 0;
     
            String percorso = file.getAbsolutePath();
            byte[] buffer = new byte[dimensione];
     
            FileOutputStream outputStream = new FileOutputStream(file);
            System.out.println("1 - file create:" +percorso+ " QUI FUNZIONA");
     
            try{
     
                System.out.println("2 - until this point all ok:" +percorso);
     
                while (tot <= dimensione) { 
                    DatagramPacket datapacchetto = new DatagramPacket(buffer, buffer.length);
                    try{
                        dataSocket.receive(datapacchetto);
                    } catch (java.net.SocketException e){
                        System.out.println("ERROR");
                    }
                    System.out.println("OK :" +percorso);
                    tot += 1024;
                    System.out.println("  - "+tot+ " byte");
                    outputStream.write(datapacchetto.getData(), 0, datapacchetto.getLength());
                    outputStream.flush();
     
                }  
            }catch (java.net.SocketException e)
            {
                    System.out.println("*** ERRORE ***");
     
            }
            System.out.println("*** FILE RICEVUTO CON SUCCESSO ***");
     
     
        }
     
        private String acquisisci_nome(String nome) throws IOException {
     
                dataSocket.receive(dataPacket);
     
                //Acquisisco le informazioni del client che si collega
                InetAddress address = dataPacket.getAddress();           
                String client = address.getHostName();          
                int port = dataPacket.getPort();
     
                String name = (new String(dataPacket.getData()).trim()); 
                return name;
        }
     
        private int acquisisci_dimensione(int dimensione) throws IOException {
     
                byte[] buf = new byte[1024];  
                dataSocket.receive(dataPacket);
     
                String str = new String(dataPacket.getData(), 0, dataPacket.getLength());    
                int dim = Integer.parseInt(str);
                return dim;
        }
     
     
        public static void main(String[] args) throws IOException
            {
                UDP_Server_pacchetti udpServerPac = new UDP_Server_pacchetti();
                udpServerPac.connessioneRicezione();
            }
     
    }


    --- Update ---

    CLIENT

    package udp_pacchetti;
     
     
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    import javax.swing.JFileChooser;
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
     
    /**
     *
     * @author ste
     */
    public class UDP_Client_pacchetti {
     
        DatagramSocket dataSocket;
        DatagramPacket dataPacket;
     
        String nome;
        int dimensione;
        String dimension;
        private InetAddress indirizzoServer;
        private int porta;
     
        public void Start() throws IOException {
     
                porta = 9876;
                indirizzoServer = InetAddress.getByName("127.0.0.1");  
     
                // QUI SELEZIONO IL FILE
                JFileChooser fileChooser = new JFileChooser();
                int n = fileChooser.showOpenDialog(null);
                File file = fileChooser.getSelectedFile(); 
                String path_file = file.getAbsolutePath();
     
                // E NE ACQUISISCO I DATI
                nome = file.getName();
                String nome_stringa = nome;
                System.out.println("PROGRAMMA DI TRASFERIMENTO IN PACCHETTI");
                System.out.println("il nome da inviare è: " +nome_stringa);
                dimensione = (int) file.length();
                dimension = String.valueOf(dimensione);
                String dimensione_stringa = dimension;
     
                //System.out.println("Il nome del fileselezionato è: " +nome);
                System.out.println("La sua dimensione :  " +dimensione_stringa+ " byte");
                System.out.println("Percorso: " +path_file);
     
                invia_Nome(nome_stringa);
                invia_Dimensione(dimensione_stringa);
                invia_File(file, nome, path_file, dimensione );
        }
     
        private void invia_File(File file, String nome, String path_file, int dimensione) throws IOException {
            System.out.println("*** Inizio invio file");
            dataSocket = new DatagramSocket();
            try {
     
                byte[] buf = new byte[1024];
                int read,tot=0;
                FileInputStream inputStream = new FileInputStream(file);
     
     
                // INVIO I PACCHETTI 
                while ((read = inputStream.read(buf)) != -1 ){
                    tot += read;
                    System.out.println("  - "+tot+ " byte");
                    dataPacket = new DatagramPacket(buf, buf.length, indirizzoServer, 9876);
                    dataSocket.send(dataPacket);
                }
                System.out.println("*** FILE INVIATO CON SUCCESSO ***");    
            } catch (FileNotFoundException e) {
                e.printStackTrace();    
            }
     
     
        }   // FINE INVIA_FILE   
     
        private void invia_Nome(String nome) throws IOException {
     
            byte [] name = {0};
            dataSocket = new DatagramSocket();
            dataPacket = new DatagramPacket (name, name.length, indirizzoServer, porta);
            String nome_file = nome;
            System.out.println("Il nome del file è: " +nome_file);        
            name  = nome_file.getBytes();
            dataPacket.setData(name);
            dataPacket.setLength(name.length);
     
            // INVIO DEL PACCEHTTO SUL SOCKET
            dataSocket.send(dataPacket);
        }// FINE INVIA NOME
     
        private void invia_Dimensione (String  dimensione_stringa) throws IOException {
     
            byte [] dimension = {0};
            String dimensione_file = dimensione_stringa;
            dataSocket = new DatagramSocket();
            dataPacket = new DatagramPacket (dimensione_file.getBytes(), dimensione_file.length(), indirizzoServer, porta);
     
            System.out.println("La dimensione del file è: " +dimensione_file);        
     
            // INVIO DEL PACCEHTTO SUL SOCKET
            dataSocket.send(dataPacket);
            dataSocket.close();
        }// FINE INVIA DIMENSIONE
     
     
     
     
     
     
        public static void main(String[] args) throws IOException
            {
                UDP_Client_pacchetti udpClientPac = new UDP_Client_pacchetti();
                udpClientPac.Start();
            }
     
    }


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: DATAGRAMSOCKET - problem with transfer file in UDP

    You have a while-loop in your program. But I can not understand what it is supposed to do since I dont speak the language it is written in.
    Maybe you try to explain to us, perhaps in comments, what you are doing. You should also try to go over your loop and see when it will exit and when it will not.

    By the way, UDP is not a safe protocol. Data can be lost when using UDP and maybe this might be your problem.

Similar Threads

  1. Checksum in UDP datagramSocket
    By cicco.ste90 in forum Java Networking
    Replies: 0
    Last Post: February 7th, 2013, 11:44 AM
  2. File transfer from Client to Server side
    By highlander141 in forum Java Networking
    Replies: 1
    Last Post: August 29th, 2012, 08:53 AM
  3. RESTful WebService and XML file transfer
    By Heisen in forum Web Frameworks
    Replies: 1
    Last Post: July 5th, 2012, 10:58 AM
  4. Send file via DatagramSocket
    By ToshX in forum Java Networking
    Replies: 1
    Last Post: December 2nd, 2011, 03:13 PM
  5. Program with transfer value from one class to another
    By Adam22 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 6th, 2011, 12:26 AM