Java - DataInputStream readUTF返回EOFException

时间:2014-01-17 16:34:10

标签: java swing sockets

我正在尝试创建一个简单的Socket Client-Server对,其中一个人会发送消息,但在我的一个run方法中,我这样做:String servermessage = new String(dis.readUTF());并返回java.io.EOFException什么是我做错了,有没有更好的方法在客户端和serversocket之间发送消息?

编辑:我现在知道当输入流到达所有字节之前的末尾时会发生EOFException,但我不明白它们的意思,有人可以澄清吗?

服务器:

public class Server {
    String host = "localhost";
    int port = 2484;
    static boolean launched = false;

    ServerSocket server;
    static DataOutputStream dos;

    public static void main(String args[]) throws IOException {
        final Server serv = new Server();

        final Thread socketThread = new Thread(new Runnable() {
            ServerSocket server;

            @Override
            public void run() {
                try {
                    Server serv = new Server();
                    ServerSocket server = new ServerSocket(serv.port);
                    System.out.println("Server: Launched!");

                    Socket client = server.accept();
                    OutputStream clientOut = client.getOutputStream();
                    DataOutputStream dos = new DataOutputStream(clientOut);

                    dos.close();
                    clientOut.close();
                    server.close();
                    client.close();

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

                }
            }
        });

        Thread input = new Thread(new Runnable() { //Thread that reads from an input from the console
            Server serv = new Server();

            @Override
            public void run() {
                Scanner scanner = new Scanner(System.in);
                System.out.println("Type Something!");

                while (true) {
                    String input = scanner.next();

                    if (input.equalsIgnoreCase("Start")) {
                        if(!launched) { //Checks if the ServerSocket server is already launched, and if not, launches it
                            System.out.println("Launching the socket!");
                            socketThread.start();
                            input = scanner.next();

                            launched = true;
                        } else if(launched){
                            System.err.println("ServerSocket is already Launched!");

                        }
                    } else if (input.equalsIgnoreCase("Send")) {
                        System.out.println("What would you like to send?");
                        input = null;
                        input = scanner.next();
                        System.out.println("Server: " + input);

                        try {
                            dos.writeUTF(input); //Sends the input to the client

                        } catch (IOException e) {
                            System.err.println("Unable to send the message");
                            e.printStackTrace();

                        }
                    } else {
                        System.err.println("Unknown Input!");
                        input = null;
                    }
                }
            }
        });

        input.start();
    }
}

SocketManager:

package Socket_Swing_Test_2;

public class SocketManager implements Runnable {
    ClientMain c = new ClientMain();
    ClientSwing cs = new ClientSwing();

    Socket client;
    PrintWriter output;
    BufferedReader input;
    InputStream clientIn;
    DataInputStream dis;



    String host = "localhost";
    int port = 2484;

    JTextArea textArea = cs.textArea;

    public SocketManager(ClientMain c) {
        this.c = c;

        try {
            client = new Socket(host, port);
            System.out.println("SocketManager: Connected to the server!");

            clientIn = client.getInputStream();
            dis = new DataInputStream(clientIn);

        } catch (Exception e) {
            System.err.println("Client: Socket failed to Connect");
            e.printStackTrace();

        }
    }

    public synchronized void send(String message) {
        try {
            output.println(message);
            output.flush();

        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    public synchronized void connect() {
        try {
            client = new Socket(host, port);
        } catch (Exception e) {
            System.err.println("Client: Socket failed to Connect");
            e.printStackTrace();
        }
    }

    public synchronized void close() {
        try {
            client.close();

        } catch (Exception e) {
            System.err.println("Unable to close the socket!");
            e.printStackTrace();
        }

        clientIn = null;
        dis = null;
        System.gc(); // Garbage Collector, recycles unused objects that are taking up RAM

    }

    public synchronized boolean checkConnection() {
        return client.isConnected();

    }

    public synchronized void listenStream() { //Unused, keeping for reference
        try {
            while (input.ready()) {
                System.out.println(input.readLine());

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public synchronized Socket getSocket() {
        return client;

    }

    public void receive() {

    }

    @Override
    public void run() { 
        System.out.println("SocketManager.run: Is running!");

        try {
            //Read for a message from the server

            System.out.println("SocketManager.run: Checking for messages from the server");
            String servermessage = new String(dis.readUTF());
            textArea.append(servermessage);

        } catch (Exception e) {
            e.printStackTrace();

        } finally {

            try {
                client.close();
                close();

            } catch (Exception e) {
                e.printStackTrace();

            }
        }
    }
}

你们可能不需要,但提供以防万一

ClientSwing:

public class ClientSwing extends JFrame {
    JPanel panel;
    JTextArea textArea;
    JTextField textField;

    int WIDTH = 640;
    int HEIGHT = 480;

    public ClientSwing() {
        super("Client");

        panel = new JPanel();
        panel.setLayout(null);

        textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setBounds(0, 0, WIDTH, HEIGHT - 15);
        textArea.setFont(new Font("Impact", Font.PLAIN, 13 + 1/2));

        textField = new JTextField();
        textField.setLocation(0, HEIGHT - 47);
        textField.setSize(WIDTH, 20);
        textField.addActionListener(new ActionListener() { //I AM RIGHT HERE
            @Override
            public void actionPerformed(ActionEvent e) {
                String input = textField.getText();

                if(input.equalsIgnoreCase("Launch")) {
                    SwingWorker socketLaunch = new SwingWorker() {

                        @Override
                        protected Object doInBackground() throws Exception {
                            ClientMain main = new ClientMain();
                            main.run();
                            return null;
                        }
                    };

                    socketLaunch.execute();
                }
            }
        });

        panel.add(textArea);
        panel.add(textField);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(WIDTH, HEIGHT);
        setLocationRelativeTo(null);
        setResizable(false);

        add(textField);
        add(textArea);
    }
}

ClientMain:

public class ClientMain extends JFrame {
    private SocketManager network;
    int a = 1;

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

            @Override
            public void run() {
                ClientSwing window = new ClientSwing();
                window.setVisible(true);
            }
        }); 
    }

    public void run() {
        network = new SocketManager(new ClientMain());

        Thread thread = new Thread(network);
        thread.run();
    }

    public SocketManager getSocketM() {
        return network;
    }
}

追踪:

java.io.EOFException
    at java.io.DataInputStream.readUnsignedShort(Unknown Source)
    at java.io.DataInputStream.readUTF(Unknown Source)
    at java.io.DataInputStream.readUTF(Unknown Source)
    at Socket_Swing_Test_2.SocketManager.run(SocketManager.java:114)
    at java.lang.Thread.run(Unknown Source)
    at Socket_Swing_Test_2.ClientMain.run(ClientMain.java:26)
    at Socket_Swing_Test_2.ClientSwing$1$1.doInBackground(ClientSwing.java:46)
    at javax.swing.SwingWorker$1.call(Unknown Source)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at javax.swing.SwingWorker.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:1)

来自Data Streams的教程:

  

请注意DataStreams通过捕获EOFException来检测文件结束条件,而不是测试无效的返回值。

数据流为原始数据值提供二进制I / O;没有任何方法可以阅读选择代表结束的价值。