简体   繁体   English

套接字编程中的ProgressMonitorInputStream

[英]ProgressMonitorInputStream in Socket Programming

I have a socket connected client and server. 我有一个套接字连接的客户端和服务器。 I tried to implement progressMonitorInputStream() to server, the data is transferred perfectly but the Progress Monitor is not visible. 我尝试对服务器实施progressMonitorInputStream(),数据传输完美,但是进度监视器不可见。 I tried implementing it for reading from a file only one side, there the progress monitor works fine. 我尝试实现它以便仅从一侧读取文件,因此进度监视器工作正常。 Below are both codes 以下是两个代码

Server: 服务器:

    public class Server implements ActionListener, PropertyChangeListener {

    DataInputStream dis;
    ProgressMonitor pm;
    ProgressMonitorInputStream pmis;
    JFrame frm;
    Server s;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {

                new Server().createAndShowUI();
            }
        });
    }

    private void createAndShowUI() {
        frm = new JFrame();
        JPanel panel = new JPanel();

        JButton st = new JButton("Start");

        st.setPreferredSize(new Dimension(80, 25));

        panel.add(st);

        panel.setSize(500, 50);

        frm.add(panel);
        frm.setLocationRelativeTo(null);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.pack();
        frm.show();

        st.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        frm.dispose();
        try {
            ServerSocket ss = new ServerSocket(5555);
            Socket soc = ss.accept();
            dis = new DataInputStream(soc.getInputStream());
            pmis = new ProgressMonitorInputStream(frm, "Progress", dis);
            pm = pmis.getProgressMonitor();
//            pm.setMillisToPopup(1);
            Task task = new Task();
            task.addPropertyChangeListener(s);
            task.execute();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if ("progress".equals(evt.getPropertyName())) {
            int progress = (Integer) evt.getNewValue();
            pm.setProgress(progress);

        }
    }

    class Task extends SwingWorker<Void, Void> {

        @Override
        protected Void doInBackground() throws Exception {
            int progress = 0;
            setProgress(0);
            int x;
            while ((x = pmis.read()) != -1) {
                System.out.print((char) x);
                setProgress(progress);
                progress++;                
            }
            pmis.close();
            return null;
        }

        @Override
        public void done() {
            pm.close();
        }        

    }
}

The above code is the problematic one. 上面的代码是有问题的。 The working code is below: 工作代码如下:

Proper working code with File Input Stream on one side 一侧带有文件输入流的正确工作代码

    public class ProgressMonitorOwn implements ActionListener,
        PropertyChangeListener {

    JFrame frm;
    Task task;
    DataInputStream dis;
    ProgressMonitorInputStream pmis;
    JButton but;
    ProgressMonitor pm;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ProgressMonitorOwn().createAndShowUI();
            }
        });
    }

    private void createAndShowUI() {
        frm = new JFrame();
        JPanel panel = new JPanel();
        but = new JButton("Show");
        but.setPreferredSize(new Dimension(100, 25));

        panel.add(but);
        panel.setSize(200, 100);

        frm.add(panel);
        frm.setSize(200, 100);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setLocationRelativeTo(null);
        frm.show();
        but.addActionListener(this);
    }

    class Task extends SwingWorker<Void, Void> {

        @Override
        protected Void doInBackground() throws Exception {
            int x;
            int prog = 0;
            while ((x = pmis.read()) != -1) {
                System.out.print((char) x);
                setProgress(prog);
                Thread.sleep(100);
                prog++;
            }
            return null;

        }

        @Override
        public void done() {
            but.setEnabled(true);
            pm.close();
            System.exit(0);
        }

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        but.setEnabled(false);
        try {
            FileInputStream fis = new FileInputStream("D:\\test\\test.txt");
            dis = new DataInputStream(fis);
            pmis = new ProgressMonitorInputStream(frm, "ProgressTest", dis);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
        task = new Task();
        task.addPropertyChangeListener(this);
        task.execute();
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if ("progress" .equals(evt.getPropertyName())) {
            int progress = (Integer) evt.getNewValue();
            pm = pmis.getProgressMonitor();
            pm.setProgress(progress);
        }
    }
}

I am not able to figure out why Progress Monitor is displayed in FileInputStream Program but not on socket program. 我无法弄清楚为什么“进度监视器”显示在FileInputStream程序中,而不显示在套接字程序上。 Any ideas? 有任何想法吗?

You need to call setMaximum() on the ProgressMonitor of the ProgressMonitorInputStream to the expected size of the download. 您需要在ProgressMonitorProgressMonitorInputStream上调用setMaximum()以达到预期的下载大小。 Otherwise it doesn't know what 'progress' means. 否则,它不知道“进度”是什么意思。

You don't have to call setProgress() with a ProgressMonitorInputStream . 您不必使用ProgressMonitorInputStream调用setProgress() It does that automatically. 它会自动执行。

You don't need the DataInputStream. 您不需要DataInputStream.

You should read more than a byte at a time. 您一次应该读一个字节以上。 Read into a byte[] buffer. 读入byte[]缓冲区。

Call frm.setVisible(true); 调用frm.setVisible(true); to make is visible. 使可见。 Change in both the classes. 改变两个班级。

Remove frm.show() because it is a deprecated method . 删除frm.show()因为它已被弃用 Look here Component#show() . 在这里看Component#show()

Sample code: 样例代码:

private void createAndShowUI() {
   frm = new JFrame();
   ...
   // call in the end after adding all the components
   frm.setVisible(true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM