0
votes

I have to do a cluedo game for Uni, so we have a Server class and Clients that are connecting. For each Client connecting we want to start a own ServerThread with the socket of the Client that is connecting. This Thread just listens to incoming messages and tells the server class to send them back to the client(s). PROBLEM: each time a new client connects he is overwriting this ServerThread so there is always just one ServerThread and we would like to have one for each Client. We send JSON messages between the Clients and right now the receive message in the ServerThread reads only from the last connected socket. How can i solve this? i added my accept method in the Server i guess the mistake is there but could be anywhere. Thanks for your help! Mauritius

Server

public void accept() throws IOException{
        while(true){
            Socket socket = serverSocket.accept();
            Runnable r = new ServerThreadHandler(socket);
            Thread t = new Thread(r);
            t.start();
        }
    }

ServerThreadHandler:

public class ServerThreadHandler implements Runnable {
    static Socket socket=null;
    protected   User client;
    //private static  int i;
    private static BufferedReader in;
    private static OutputStreamWriter out;

    public  void createUser(String nick, String group, String[] ext) throws IOException{
        client = new User(nick, group, ext, null, false, 0, false, socket, socket.getPort());
    }

    /**
     * constructor-Method
     * @param socketS
     */
    ServerThreadHandler(Socket socketS){
        socket = socketS;
    }
    public void run(){
        Server.setThreadList(socket);
        in = createReader();
        out = createWriter();
        //and so on...
    }
}
3
a wild guess, do you have any static vairable in ServerThreadHandler? - nafas
public class ServerThreadHandler implements Runnable { static Socket socket=null; protected static User client; - Mauritius Julius Joseph Klein
get rid of static , that should solve ur problem :) - nafas
protected static User client; ---> protected User client; - nafas
Thanks for your help! I tried to do that but i have an createUser method in ServerthreadHandler, that is called in another class, so it has to be static or is there another way to call a method in another class. As well some reader and writer there. to get them they need to be static don't they? - Mauritius Julius Joseph Klein

3 Answers

2
votes

The logic mentioned in you code snippet will definitely create as many threads as the no. of clients that are connecting.

However, the possible reason might be, since the Socket variable in ServerThreadHandler is Static, all subsequent threads being created overwrite the same socket variable causing issue in the previously created thread which is using the socket variable.

You should consider, using non static variable for the Socket in ServerThreadHandler since any runnable class should hold a state and should not be using a static socket.

From what I understood from your question, createUser method is an instance method of ServerThreadHandler. Hence you must have created the instance of ServerThreadHandler to invoke createUser from another class. Hence you can access the socket variable even if it is an instance variable.

1
votes

Unfortunately the code has many design flaws:

The quickest fix I can suggest is to remove User class and move everything in the Handler class (or vice verca ? )

also make all your variable non-static

    static Socket socket=null;
    protected   User client;
    //private static  int i;
    private static BufferedReader in;
    private static OutputStreamWriter out;

they should be :

    Socket socket=null;
    protected   User client;
    //private int i;
    private BufferedReader in;
    private OutputStreamWriter out;
1
votes

The Socket member variable should be non-static. Ditto the reader and writer.

Never make a variable static unless you have a really good reason to do so and understand the consequences.