4 Replies - 2325 Views - Last Post: 18 April 2016 - 09:00 AM Rate Topic: -----

#1 binTrav   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-April 16

Send Multiple Files ( Java Sockets )

Posted 18 April 2016 - 07:57 AM

Good morning,

I am having difficulty with my chat application in Java that I am creating in context of being able to send multiple files.. Example; I send my.txt which is successful then I go to send hello.txt which is not successful... first time always works but second third etc. does not.. I do not understand what is going wrong here this is my first app with Sockets so still kind of a rookie at it.. Below are the two methods essentially handling the executions of send/receive if you need to see the main class in context of the while true loop let me know and ill happily post anything thats required, thanks everyone for your help in advance, excited to get this thing going and learn!



public void sendSound() throws IOException {

        connection2 = new Socket(InetAddress.getByName(serverIP), 1990);
        System.out.println("send voice");
        OutputStream output2 = new ObjectOutputStream(connection2.getOutputStream());
        output2.flush();
        InputStream input2 = new ObjectInputStream(connection2.getInputStream());
        System.out.println(input2);

        DataOutputStream dos = null;
        try {
            dos = new DataOutputStream(connection2.getOutputStream());
        } catch (IOException ex1) {
            Logger.getLogger(EchoRun.class.getName()).log(Level.SEVERE, null, ex1);
            System.out.println("error creating data input stream ");
        }
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(voiceOutput);

        } catch (FileNotFoundException ex1) {
            Logger.getLogger(EchoRun.class.getName()).log(Level.SEVERE, null, ex1);
            System.out.println("error in creating file input stream");

        }

        int count = 0;
        byte[] buffer = new byte[4096];

        try {
            while ((count = fis.read(buffer)) > 0) {
                dos.write(buffer, 0, count);
                dos.flush();
                System.out.println(fis);
            }
        } catch (IOException ex1) {
            Logger.getLogger(EchoRun.class.getName()).log(Level.SEVERE, null, ex1);
            System.out.println("error in loop writing");
        }

        try {

            fis.close();
        } catch (IOException ex1) {
            Logger.getLogger(EchoRun.class.getName()).log(Level.SEVERE, null, ex1);
            System.out.println("error closing fis");
        }
        try {
            dos.flush();
            dos.close();

        } catch (IOException ex1) {
            Logger.getLogger(EchoRun.class.getName()).log(Level.SEVERE, null, ex1);
            System.out.println("or closing dos");
        }

    }

}


class downloadVoiceFile implements Runnable {

    /*
    
    Play audio clip VARS
     */
    private final int BUFFER_SIZE = 128000;
    private File soundFile;
    private AudioInputStream audioStream;
    private AudioFormat audioFormat;
    private SourceDataLine sourceLine;

    AudioSystem clip;
    Echo echo;
    ServerSocket server_download;
    Socket connection_download;
    FileOutputStream fos;
    DataInputStream dis;
    OutputStream output;
    InputStream input;

    @Override
    public void run() {
        System.out.println("downloading file.........");

        try {
            server_download = new ServerSocket(1990, 100);
        } catch (IOException ex) {
            Logger.getLogger(downloadVoiceFile.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            connection_download = server_download.accept();
        } catch (IOException ex) {
            Logger.getLogger(downloadVoiceFile.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            output = new ObjectOutputStream(connection_download.getOutputStream());
            output.flush();
        } catch (IOException ex) {
            Logger.getLogger(downloadVoiceFile.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            input = new ObjectInputStream(connection_download.getInputStream());
        } catch (IOException ex) {
            Logger.getLogger(downloadVoiceFile.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            dis = new DataInputStream(connection_download.getInputStream());
        } catch (IOException ex) {
            Logger.getLogger(downloadVoiceFile.class.getName()).log(Level.SEVERE, null, ex);
        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("voiceOut.wav");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(downloadVoiceFile.class.getName()).log(Level.SEVERE, null, ex);
        }

        byte[] buffer = new byte[4096];
        System.out.println(buffer.length);
        int read = 0;
        try {
            while ((read = dis.read(buffer)) > 0) {
                fos.write(buffer, 0, read);
            }
        } catch (IOException ex) {
            Logger.getLogger(downloadVoiceFile.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            fos.close();
            dis.close();
            output.close();
            input.close();
        
            playStart();

            int reply = JOptionPane.showConfirmDialog(null, "You Have Received A Voice Messsage", "Voice Clip Received",
                    JOptionPane.YES_NO_OPTION);
            if (reply == JOptionPane.YES_OPTION) {
                System.out.println("yes");
                playVoiceFile();

            } else {
                System.out.println("no");
                File f = new File("voiceOut.wav");
                boolean bool = false;
                bool = f.delete();
            }

        } catch (IOException ex) {
            Logger.getLogger(downloadVoiceFile.class.getName()).log(Level.SEVERE, null, ex);
        }

    }




Is This A Good Question/Topic? 0
  • +

Replies To: Send Multiple Files ( Java Sockets )

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Send Multiple Files ( Java Sockets )

Posted 18 April 2016 - 08:46 AM

Quote

first time always works but second third etc. does not..


Quote

   @Override
    public void run() {
        System.out.println("downloading file.........");


That will only run once (unless you loop it). The creation of the server socket should not be in the loop though.

This post has been edited by g00se: 18 April 2016 - 08:47 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#3 binTrav   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-April 16

Re: Send Multiple Files ( Java Sockets )

Posted 18 April 2016 - 08:50 AM

So essentially


public void run(){

server_download = new ServerSocket(1990, 100);

while(true){

my logic to do




}


}
Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Send Multiple Files ( Java Sockets )

Posted 18 April 2016 - 08:54 AM

Yes
Was This Post Helpful? 0
  • +
  • -

#5 binTrav   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-April 16

Re: Send Multiple Files ( Java Sockets )

Posted 18 April 2016 - 09:00 AM

Perfect works.. thanks for showing me
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1