带GUI的Java套接字客户端

时间:2018-03-04 10:51:17

标签: java swing sockets networking client

我正在尝试为我的学校项目构建一个客户端套接字应用程序,它允许您将文件发送到服务器。我构建它,我打开服务器,服务器在等待连接时正常工作。打开客户端并选择带有 JFileChooser 的文件并单击“发送”后,服务器会收到该文件。但是,如果我尝试选择一个文件并再次点击发送它就不起作用。有人可以帮忙吗?

服务器代码:

public class NewServer {

public static void main(String[] args) throws Exception {

    ServerSocket serverSocket = new ServerSocket(8090);
    while (true) {
        System.out.println("Waiting for client...");
        Socket sock = serverSocket.accept();
        System.out.println("Client connected!");

        DataInputStream in = new DataInputStream(sock.getInputStream());
        DataOutputStream out = new DataOutputStream(sock.getOutputStream());
        FileOutputStream fout = null;

        String ip = in.readUTF();
        String fileName = in.readUTF();
        System.out.println("User with IP " + ip + " is sending a file with name " + fileName);
        fout = new FileOutputStream(fileName);

        System.out.println("Receiving file...");
        int count;
        byte[] buffer = new byte[1024];
        while ((count = in.read(buffer)) > 0) {
            fout.write(buffer, 0, count);
        }
        System.out.println("File received!");
        out.close();
        in.close();
        fout.close();

    }
}

}

GUI:

public class Frame extends JFrame {

Frame() throws Exception {
    JFrame frame = new JFrame("Client");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 400);
    Socket sock = new Socket("localhost", 8090);

    JPanel panel = new JPanel();
    JButton send = new JButton("Send File");
    JButton select = new JButton("Select File");
    JLabel label = new JLabel();

    panel.setLayout(new GridLayout(3, 4, 5, 10));
    panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    frame.add(panel);
    panel.add(select);
    panel.add(send);
    panel.add(label);
    send.setEnabled(false);


    select.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            // optionally set chooser options ...
            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                label.setText(file.getName());
                send.setEnabled(true);
            } else {
                send.setEnabled(false);
            }
        }
    }
    );
    send.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            FileInputStream fin = null;
            DataInputStream in = null;
            DataOutputStream out = null;
            try {
                fin = new FileInputStream(label.getText());
                in = new DataInputStream(sock.getInputStream());
                out = new DataOutputStream(sock.getOutputStream());

                out.writeUTF(sock.getLocalAddress().toString());
                out.writeUTF(label.getText());
                out.flush();

                int count;
                byte buffer[] = new byte[1024];
                while ((count = fin.read(buffer)) > 0) {
                    out.write(buffer);
                }
            } catch (Exception ex) {
                ex.getMessage();
            } finally {
                try {
                    sock.close();
                    send.setEnabled(false);
                } catch (IOException ex) {
                    ex.getMessage();
                }

            }

        }

    });

}

}

在主要内容我只创建一个GUI实例:

public class NewClient {

public static void main(String[] args) throws Exception {

    Frame frame = new Frame();

}

}

如果有人能提供帮助那就太棒了!非常感谢你

1 个答案:

答案 0 :(得分:0)

sock.close();

第一次使用后关闭套接字。

你需要把

Socket sock = new Socket("localhost", 8090);

actionPerformed(ActionEvent e)的{​​{1}}方法中

send

根据下面的Gregoris反馈,我建议正确的线程不会调用send.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Socket sock = new Socket("localhost", 8090); FileInputStream fin = null; ... 侦听器来创建套接字,如果它正在关闭它,这是令人惊讶的。

底线是一个无法重复使用的封闭式插座,这绝对是功课。