0

I think it's because when I multi-thread the client&server, the DataOutputStream and DataInputStream buffers I use get overwritten or something like that since the socket can only have 1 duplex connection.

Here's what I have for now:

Client Class in my client program:

public static void main(String args[]) throws UnknownHostException, IOException, InterruptedException {
        for (int i=0;i<2;i++) //change limit on i to change number of threads
        {
            new Thread(new ClientHandler(i)).start();
        }
        Thread.sleep(10000);

ClientHandler class in my client program: (Sends a value to the server, the server will echo it back).

public class ClientHandler implements Runnable {
public int clientNumber;
public ClientHandler(int i){
    this.clientNumber=i;
}
public void run() {
        Socket socket = null;
        try {
            socket = new Socket("localhost",9990);
            System.out.println("connected client number "+clientNumber);
            DataOutputStream output = new DataOutputStream(socket.getOutputStream());
            DataInputStream input = new   DataInputStream(socket.getInputStream());
            output.writeDouble((new Random()).nextDouble());
            System.out.println(input.readDouble());
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

Server Class in my server program:

    ServerSocket socket = new ServerSocket(9990);
    try { 
        while (true) {
            Socket threadSocket = socket.accept();
            new Thread(new ServerHandler(threadSocket)).start();
            Thread.sleep(10000);
}
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    finally {
        socket.close();
        }
    }
}

ServerHandler Class in my server program (receives value from client and echoes it back)

public class ServerHandler implements Runnable {
    private Socket socket;
    public ServerHandler(Socket socket) {
    this.socket = socket;
}

public void run() {
    while(true) {
      try {
            DataInputStream input = new DataInputStream(socket.getInputStream());
            DataOutputStream output = new DataOutputStream(socket.getOutputStream());
    double a = input.readDouble();
            output.writeDouble(a);

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

  }
}

}

So it's a pretty straight-forward implementation: create multiple threads of the client, and connect them to multiple threads of the server.

Everything works fine until the line:

double a = input.readDouble();

in my ServerHandler class.

I get an EOFException

I'm guessing it's because there can only be a single duplex connection between sockets. But if that's the case then how would I implement multi-threading of sockets at all?

So my question is: how can I get rid of the EOFException and allow myself to perform multi-threaded client-server socket interaction?

(preferably not changing much about my code because it's taken me a long time to get to this point).

4
  • Why do you need Thread.sleep(10000);? Plus, can you post the stack trace? Commented Oct 22, 2015 at 9:42
  • I had seen Thread.sleep(10000) in an example and just used it - not entirely sure what it's doing. How do I post the stack trace? Commented Oct 22, 2015 at 9:43
  • e.printStackTrace(); prints it in the console. Just copy-paste it in the question. Commented Oct 22, 2015 at 9:44
  • I actually just removed my while(true) loop in my ServerHandler class and randomly am no longer getting an error... it may actually be working! Will update! Commented Oct 22, 2015 at 9:46

1 Answer 1

1

The problem is that you share same Socket variable in ServerHandler for all threads:

private static Socket socket

Remove static keyword. Your ServerHandler will be something like this:

public static class ServerHandler implements Runnable {
    private Socket socket;

    public ServerHandler(Socket socket) {
        this.socket = socket;
    }

    public void run() {
        try {
            DataInputStream input = new DataInputStream(socket.getInputStream());
            DataOutputStream output = new DataOutputStream(socket.getOutputStream());
            double a = input.readDouble();
            output.writeDouble(a);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

hi, thank you for answering! I removed the static keyword, but I'm still getting EOFExeption when I try to run the code.
@user83676 can you update code in question with your fixes?
@user83676 hm your code of ServerHandler looks very strange, because there is no exception handling in run() method... In my version of ServerHandler I added IOException handling.
I actually just forgot to put that in my original question - I'll update it here as well.
Aha! Thanks for asking about that because it made me write in the while(true){} loop that was causing everything to not work... Code working perfectly now!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.