首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >writeUTF()方法产生java.net.SocketException: Socket closed

writeUTF()方法产生java.net.SocketException: Socket closed
EN

Stack Overflow用户
提问于 2017-08-26 22:39:42
回答 1查看 203关注 0票数 0

我是用JAVA设计客户端和服务器的新手。现在,我正在尝试编写一个由GUI呈现的客户机,以便与我的字典服务器进行通信。客户端可以添加、删除或查询一个单词(三个按钮)。客户端代码如下:

代码语言:javascript
复制
public class DictionaryClient {

    private static String ip;
    private static int port;
    private DataInputStream input = null;
    private DataOutputStream output = null;

    public static void main(String[] args) {

        // IP and port
        ip = args[0];
        port = Integer.parseInt(args[1]);

        DictionaryClient client = new DictionaryClient();
        client.run(ip, port);
    }

    public void run(String ip, int port){

        GUI g = new GUI();

        try(Socket socket = new Socket(ip, port);) {

            // Output and Input Stream
            input = new DataInputStream(socket.getInputStream());
            output = new DataOutputStream(socket.getOutputStream());

            g.start();

            JButton c2 = (JButton)g.getComponent(2);
            JButton c3 = (JButton)g.getComponent(3);
            JButton c4 = (JButton)g.getComponent(4);

            c2.addActionListener(new ButtonAdd(g));
            c3.addActionListener(new ButtonRemove(g));
            c4.addActionListener(new ButtonQuery(g));

        }
        catch (UnknownHostException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        } 
    }


    //Button: Add
    public class ButtonAdd implements ActionListener{

        GUI g = null;


        //Constructor
        public ButtonAdd(GUI g){
            this.g = g;
        }

        public void actionPerformed(ActionEvent event) {
            JTextField t1 = (JTextField)g.getComponent(1);
            JLabel t6 = (JLabel)g.getComponent(6);
            String word = t1.getText();
            String definition = t6.getText();
            JLabel t5 = (JLabel)g.getComponent(5);

            try {
                output.writeInt(1);
                output.writeUTF(word);
                output.writeUTF(definition);
                output.flush();

                String message = input.readUTF();
                t5.setText("Status: ");
                t6.setText(message);
            } 
            catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    //Button: Remove
    public class ButtonRemove implements ActionListener{

        GUI g = null;


        //Constructor
        public ButtonRemove(GUI g){
            this.g = g;
        }

        public void actionPerformed(ActionEvent event) {
            JTextField t1 = (JTextField)g.getComponent(1);
            String word = t1.getText();
            JLabel t6 = (JLabel)g.getComponent(6);
            JLabel t5 = (JLabel)g.getComponent(5);

            try {
                output.writeInt(2);
                output.writeUTF(word);
                output.flush();

                t5.setText("Status: ");
                String message = input.readUTF();
                t6.setText(message);
            } 
            catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    //Button: Query
    public class ButtonQuery implements ActionListener{

        GUI g = null;

        //Constructor
        public ButtonQuery(GUI g){
            this.g = g;
        }

        public void actionPerformed(ActionEvent event) {

            JTextField t1 = (JTextField)g.getComponent(1);
            String word = t1.getText();
            JLabel t5 = (JLabel)g.getComponent(5);
            JLabel t6 = (JLabel)g.getComponent(6);

            try {
                output.writeInt(3);
                output.writeUTF(word);
                output.flush();

                String message = input.readUTF();
                t6.setText(message);
                t5.setText("Definition: ");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

然而,当我每次点击三个按钮中的一个时,它总是弹出一个异常:尝试向服务器发送消息的行上的java.net.SocketException: Socket closed,如output.writeInt(1)output.writeUTF(word)等。

我完全不知道哪里出了问题。为什么插座关闭?我的代码中甚至没有任何close()。有谁知道这件事吗?非常感谢!

EN

回答 1

Stack Overflow用户

发布于 2017-08-26 22:54:15

我想你可能想看看你的服务器端代码。在连接之后,插座很可能会在那一侧关闭。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45896565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档