方法writeUTF()导致java.net.SocketException:Socket关闭

时间:2017-08-26 14:39:42

标签: java sockets socketexception

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

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)或{ {1}}等等。

我完全不知道出了什么问题。插座为什么关闭?我的代码中甚至没有output.writeUTF(word)。有没有人对它有任何想法?非常感谢你!

1 个答案:

答案 0 :(得分:0)

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