简体   繁体   中英

How to receive messages from server to client, without prompt from client

So I am making a basic chat application. This is a snippet of the code from the client. Here I am sending information to the server and then getting a reply.

However, sometimes the server will send messages to the client without requiring the client to send information to the server first. How do I implement this?

while (true){    
        String sentence;
        BufferedReader inFromUser =
            new BufferedReader(new InputStreamReader(System.in));
        sentence = inFromUser.readLine();

        // send to server
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        outToServer.writeBytes("\n");

        // create read stream and receive from server
        BufferedReader FromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        System.out.println(FromServer.readLine());


    }

For example, the server might send random bits of information to me at random intervals. But I can only receive them when I send the server something first. But if I implement something likes this instead (shown below), I will no longer be able to send information to the server and can only act as a receiver. But I want to be able to send information and receive information when necessary without having to send something first.

while (true){    

        // receive from server only
        BufferedReader FromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        System.out.println(FromServer.readLine());


    }

What I changed in reply to first comment. I followed your code so this is what I did. However, i still can't seem to receive messages spontaneously ie i still need to press enter or type in an input to receive any server information sent at random times:

BufferedReader FromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    while (true) {  
        // Check if there's anything to receive
        while (FromServer.ready()) {
            // receive from server
            System.out.println(FromServer.readLine());
        }

        String sentence;
        BufferedReader inFromUser =
        new BufferedReader(new InputStreamReader(System.in));
        sentence = inFromUser.readLine();

        // write to server
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        outToServer.writeBytes(sentence + '\n');

// Send data to the server here if required.
    }

Change it to something like this:-

BufferedReader FromServer = 
    new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromUser =
    new BufferedReader(new InputStreamReader(System.in));

while (true) {  
    // Check if there's anything to receive
    while (FromServer.ready()) {
        // receive from server
        System.out.println(FromServer.readLine());
    }
    if (inFromUser.ready()) {
        int ch = inFromUser.read();

        // write to server
        outToServer.writeChar(ch);
    }


}

Now the client will read anything that's there to be read, and can send data if/when required.

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