【问题标题】:Java socket-client with GUI带有 GUI 的 Java 套接字客户端
【发布时间】:2018-08-12 04:05:45
【问题描述】:

我正在尝试为我的学校项目构建一个客户端套接字应用程序,让您可以将文件发送到服务器。我构建它,打开服务器,服务器在等待连接时正常工作。在我打开客户端并使用 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();

    }
}

}

图形用户界面:

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();

}

}

如果有人可以提供帮助,那就太棒了!非常感谢

【问题讨论】:

    标签: java swing sockets networking client


    【解决方案1】:

    sock.close();

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

    你需要放

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

    sendactionPerformed(ActionEvent e) 方法中。

    send.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Socket sock = new Socket("localhost", 8090);
            FileInputStream fin = null;
            ...
    

    根据下面的 Gregoris 反馈,我建议actionPerformed 侦听器没有被正确的线程调用来创建套接字,如果它正在关闭它,这令人惊讶。

    底线是一个封闭的套接字,不能重复使用,这绝对是功课。

    【讨论】:

    • 您好,先生,我试图这样做,但是当我点击发送时程序崩溃了!
    • 你试过不关闭它吗?当我说“放置”时,我的意思是移动,也许你正在重新声明同一个变量?崩溃时的错误是什么?
    • 是的,如果我不关闭它,它将无法正确执行:(如果我将命令移到你所说的地方,当我点击发送按钮时,按钮会冻结并且整个程序没有响应我必须用 cltr alt del 关闭它
    • 如果你能重复这些错误会有所帮助。还有导入,所以我不必启动 IDE 来告诉我,只是为了得到你的错误。
    猜你喜欢
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 2011-10-17
    • 2013-05-14
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多