简体   繁体   中英

Send messages from client to server

I'm having problems sending messages from client to server and to server to other clients. Please can you give me a hand by telling me something. The problem is if I do an input (line 46) I can get an answer from the server (line 51) then continue this loop. But for example, if the server must notify a user that it's his turn, I wouldn't get the message until I try typing something.

while ((userInput = stdIn.readLine()) != null) {
    /* line 46 */ out.println(userInput);
    if (userInput.equals("quit")) {
        break;
    }

    /* line 51 */ lines = in.readLine();
    System.out.println(lines);

    System.out.println("input next command:");
}

You have to make (at least i dont know how else to do it) a separate thread where you get user input, then use message passing (just search "message passing java") for comunication. Server messages will not get blocked by input and you can tryReceive (or some name around that lines) on channel. In main thread query for message from server, then from input channel, sleep a little and repeat. When user presses enter, channel gets filled with his message, main thread takes it when passing by.

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