简体   繁体   中英

Java Thread Stopping Unexpectedly

Good evening!

I'm trying to code a Server/Client with java and Sockets.

The process of sending messages is working fine, but I don't know why, after I start the second client and I send a message with the first one, the first client gets stucked and doesn't send the messages anymore.

It has to be a Server-side problem, as I have debugged the whole code and found that the problem in the while, once it leaves it seems id doesn't gets back in again. My theory is that it is somehow stopped or maybe the iterator just stops functioning.

Could you check it out and give me some guidance? Have spent like 4h on this already.

Thank you very much!

EDIT: So as I have been asked I have reduced the code to what I think is the core of the problem. As I have checked by debugging, when exiting the while, the thread stops by itself.

Server :

new Thread() {
            public void run() {

                String inputLine;
                // Input Thread
                    while ((inputLine = clientSocket.read_str()) != null) {
                        // L'iterator ha de començar des del principi cada cop que que es comença el while
                        Iterator<MySocket> keyIterator = connections.keySet().iterator();
                        while (keyIterator.hasNext()) {
                            MySocket sock = keyIterator.next();
                            sock.write_str(inputLine);
                        }
                    }
                    clientSocket.close();

                    removeUser(clientSocket);
            }
        }.start();

I found the problem, thing is I was using clientSocket for everythread and I assumed it would work on different clientSockets, but it wasn't, so once the socket was updated, it could only write/read from that socket.

Stupid solution, sorry for the waste of time.

Thanks everyone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM