EOF- and SocketException in simple server client program

I wrote a simple server client program today, but I have a problem and don’t know why. Here are my 3 important classes(all methods are called properly):

    public class Server {
    private ServerSocket server;
    private ArrayList<Client> clients = new ArrayList<Client>();
    public Server(int port) {
        try {
            server = new ServerSocket(port);
            server.setSoTimeout(900000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        acceptClients();
        Main.frame.log("Server started on port "+port);
    }
    public void acceptClients() {
        Thread t = new Thread() {
            @Override
            public void run() {
                System.out.println("Accepting");
                while(true) {
                    try {
                        Socket client = server.accept();
                        System.out.println("Accepted: "+client.getLocalSocketAddress());
                        clients.add(new Client(client));
                    } catch (IOException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            };
        };
        t.start();
    }

}

    public class Client {
    private static Socket client;
    private static DataOutputStream out;
    private static DataInputStream in;
    public static void main(String[] args) throws UnknownHostException, IOException {
        client = new Socket("localhost", 1567);
        client.setSoTimeout(900000);
        in = new DataInputStream(client.getInputStream());
        out = new DataOutputStream(client.getOutputStream());
        receive();
        send("Hi I am a client!");
    }

    public static void receive() {
        Thread t = new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        String data = in.readUTF();
                        System.out.println("Received: "+data);
                        send("Test");
                    } catch (IOException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            };
        };
        t.start();
    }

    public static void send(String data) {
        try {
            System.out.println("Sending");
            out.writeUTF(data);
            System.out.println("Sent: "+data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

The client handler of my server:

    public class Client {
    Socket socket;
    DataOutputStream out;
    DataInputStream in;
    public Client(Socket client) {
        socket = client;
        try {
            socket.setSoTimeout(900000);
            in = new DataInputStream(socket.getInputStream());
            out = new DataOutputStream(socket.getOutputStream());
            receive();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void receive() {
        Thread t = new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        process(in.readUTF());
                        in.close();
                        sleep(100);
                    } catch (IOException | InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            }
        };
        t.start();
    }
    public void send(String data) {
        try {
            out.writeUTF(data);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void process(String data) {
        Main.frame.log("Received--> "+data);
        send("I received your message!");
    }

}

When I execute the server and the client everything is fine until the client sends the second message. Then I get this error from the server:

java.net.SocketException: socket closed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.io.DataInputStream.readUnsignedShort(Unknown Source)
    at java.io.DataInputStream.readUTF(Unknown Source)
    at java.io.DataInputStream.readUTF(Unknown Source)
    at de.julian.factoryserver.net.Client$1.run(Client.java:33)

And this error from my client:

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 ftc.Client$1.run(Client.java:28)

I hope someone can help me!

Answer

In class Server, in method send you seem to have closed the outputstream, dont close it, just flush it

 public void send(String data) {
        try {
            out.writeUTF(data);
            out.close(); // remove this and replace it with out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

also in recieve you seem to prematurely close the inputstream

private void receive() {
        Thread t = new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        process(in.readUTF());
                        in.close(); // remove this
                        sleep(100);
                    } catch (IOException | InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            }
        };
        t.start();
    }

Apply the above fixes and that should fix your exceptions.

Leave a Reply

Your email address will not be published. Required fields are marked *