문제

I am trying to display messages in my Java application sent as bytes. However, I can't make my application show each message when they arrive.

Instead, it shows all the messages in a single String when the client disconnects. Sending a "Hello" message first, and a "Bye" message last gives me "HelloBye".

How can I display each message when they arrive at the server using bytes?

Here's the server thread code:

public void run() {

    while (true) {

        try {

            int data;
            String string = new String();

            while ( (data = inputStream.read() ) != -1) {
                char thisChar = (char) data;
                string = string + thisChar;
            }

            System.out.println("Here is the message: " + string);

            if (inputStream.read() == -1) break;


        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    try {
        socket.close();

    } catch (IOException ex) {
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
    }

    System.out.println("Client session has ended");
}

And here's the client sending messages:

    clientSocket.getOutputStream().write("Hello".getBytes());
    clientSocket.getOutputStream().write("Bye".getBytes());
도움이 되었습니까?

해결책

One way to do is adding a delimiter character at the end of each message and deal with this delimiter in your inputStream.read() loop.

You can also specify the length of the message and appending the length at the beginning of the message. Usually we decide a message format and we stick with it.

i.e. Message format:

=========================================================
= Length (2 bytes)     |           Message              =
=========================================================

You might want to have a look at this link

Delimit data coming from socket

다른 팁

There are 2 ways to resolve your problem.

First of all, you can send the length (4 bytes (to present one integer), for instance) of the message before its bytes are sent. Read these four bytes on the other side to assemble the integer back and you know how much bytes should be read until the message is passed completely.

The second way is more convenient. Use DataInput(Output)Stream over socket streams and use readLine() method to read each message. See for example:

http://journals.ecs.soton.ac.uk/java/tutorial/networking/sockets/readingWriting.html

Actually you are doing 2 way handshaking means only client sends data to server.

You need to do 3 way hand shaking means server will send a Acknowledgement after receiving data.

Message no       Client                              Server
    1           Send Message                  Receive Message
    2           Receive Acknowledgement       Send Acknowledgement of receiving
    3           Send Message                  Receive Message   

See this tutorial Socket Programming with TCP.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top