0

I have the following server

public class handlerfereastra extends JFrame implements ActionListener {

    Socket sc;
    JFileChooser jf1,jf2;

    public handlerfereastra(Socket sc)
    {
        this.sc=sc;
        setSize(500,500);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container x=getContentPane();
        x.setLayout(new FlowLayout());
        JButton butrec=new JButton("Select path to receive");
        JButton butsen=new JButton("Select file to send");
        butrec.addActionListener(this);
        butsen.addActionListener(this);
        x.add(butrec);
        x.add(butsen);
        setVisible(true);
        new receptioneaza(sc).start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getActionCommand().compareTo("Select path to receive")==0)
        {
                jf1=new JFileChooser();
                jf1.showOpenDialog(this);

        }
        else if(e.getActionCommand().compareTo("Select file to send")==0)
        {
            jf2=new JFileChooser();
            jf2.showOpenDialog(this);
            try {
                BufferedInputStream bus=new BufferedInputStream(new FileInputStream(jf2.getSelectedFile()));
                PrintStream ps=new PrintStream(sc.getOutputStream());
                int count;
                byte[]bs=new byte[2048];
                System.out.println(jf2.getSelectedFile());
                while((count=bus.read(bs))>0)
                {
                    ps.write(bs,0,count);
                    ps.flush();
                }
                ps.flush();
                bus.close();
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }
}

and here is my client class

public class receptioneaza extends Thread {
    Socket sc;
    public receptioneaza(Socket sc)
    {
        this.sc=sc;
    }

    @Override public void run()
    {
        while(true)
        {
            try {
                InputStream in=sc.getInputStream();
                byte[]buf=new byte[2048];
                File fl=new File("salveaza");
                FileOutputStream fos=new FileOutputStream(fl);
                byte[] buffer = new byte[1024];
                int count;
                    while((count=in.read(buffer)) >0){
                    fos.write(buffer,0,count);
                    fos.flush();
                }
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

I want to send files from server to client and the other way.The problem is that when i send a file from server is goes well but if i send a second one from the server i don't see that the file modified.It remains as the first file i send.The same goes for client.What makes them to remain unmodified after i send it for the first time?

vsminkov
  • 10,912
  • 2
  • 38
  • 50
User124235
  • 147
  • 8
  • i think that it never exits that while... it is right? – User124235 Sep 06 '16 at 19:15
  • You need to implement an application specific protocol to signal StartOf,EndOf file. Send "START filename=myfile.txt, length=12345\n" header string first, lots of bytes, once length num of bytes is read you know a file was completed. Or something else more complicated if need to send multiple files concurrently interleaving chunk. – Whome Sep 06 '16 at 19:19
  • ok but where is the problem? i don't figure out where is the problem – User124235 Sep 06 '16 at 19:19
  • it doesn't exit that while or what? it doesn't get the end of file?? – User124235 Sep 06 '16 at 19:20
  • You are using an old traditional JavaIO where read() function blocks until data is available. If you read past of the last byte it waits for new data indefinitely. Use NIO unblocked such as this one http://stackoverflow.com/questions/11745686/java-nio-client/27130710#27130710 – Whome Sep 06 '16 at 19:21
  • ohh, i didn't know that.thank you for new information,now i know where it fails – User124235 Sep 06 '16 at 19:22
  • @Whome NIO by itself is not a solution to this problem. – user207421 Sep 06 '16 at 22:46
  • @EJP I believe it was obvious already in my two previous comments they are to read together so to speak. NIO nonblocking and application level packet syntax is easy to implement. – Whome Sep 07 '16 at 05:27

0 Answers0