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).

user83676
  • 339
  • 1
  • 9
  • 15
  • Why do you need Thread.sleep(10000);? Plus, can you post the stack trace? – Burkhard Oct 22 '15 at 09: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? – user83676 Oct 22 '15 at 09:43
  • e.printStackTrace(); prints it in the console. Just copy-paste it in the question. – Burkhard Oct 22 '15 at 09: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! – user83676 Oct 22 '15 at 09:46

1 Answers1

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();
        }
    }
}
Ιναη ßαbαηιη
  • 3,410
  • 3
  • 20
  • 41