简体   繁体   中英

How to read bytes on receiver in socket stream (java)

I have set up a socket connection between a server and i client. Right now i'm trying to send data from my client to the server. Actually the data is a byte array which contain numbers in the index 14 to 27. An example of the array is here:

{27, 14, 16, 18, 20, 22, 14, 17, 15, 17} and so on.

Have made it as an byte array because the data have to be in bytes.

The difficulty is that when i sent a line from the array to the server, i don't know how to read it other than a string. And if it is a string, it return some weird numbers like the one you see in the picture.

在此处输入图片说明

Some code how i do it:

Sender

for (int i = 0; i < data.length; i++) {
        writer.write(data[i]);
    }

    writer.flush();
    writer.close(); 

Receiver

public void readResponse(Socket client) throws IOException{
  String userInput;
  BufferedReader stdIn = new BufferedReader(new InputStreamReader(client.getInputStream()));

  System.out.println("Response from client:");
  while ((userInput = stdIn.readLine()) != null) {
      System.out.println(userInput);

  }

}

My byte array is made like this:

private byte data[] = new byte[12];

if i change it to Byte with uppercase, i can read it with my code, but i'm not sure if it in bytes then? Have to use some math to calculate an average.

private Byte data[] = new Byte[12];

So, how do i read it?

Update:

So i understand that i'm going to use a different input/output stream. Right now i have changed it too a Datainput and output stream.

Code looks like this:

Server

public void readResponse(Socket client) throws IOException{

    DataInputStream input = null;

    byte data;

    try {
        input = new DataInputStream(client.getInputStream());
    }
    catch (IOException e) {
        System.out.println(e);
    }

    System.out.println("Response from client:");
    while ((data = input.readByte()) != -1)  {
        System.out.println(data);
    }

}

Client

public void sentData(Socket client) throws IOException{     

    DataOutputStream output = null;
    try {
        output = new DataOutputStream(client.getOutputStream());
    }
    catch (IOException e) {
        System.out.println(e);
    }

    for (int i = 0; i < data.length; i++) {
        output.write(data[i]);
    }

    output.flush();
    output.close();     
}

As you can see in my client, i want to sent a byte at a time to the server, but it still shows weird numbers like [?][?][?].

All the *Reader classes are meant for text data only. When working with binary data, just use the streams directly. In your case, just use BufferedInputStream instead of BufferedInputStreamReader.

There is also TCP protocol your program has to conform to, with it own buffering and flushing mechanism. This existence of this layer is non-obvious when you first code your Java program with raw byte stream.

I would suggest you either construct a deterministic protocol, eg using a marker byte like "000" to indicate the start of your transmission, and encoded payload that excludes the "000", and finally, "000" to terminate your transmission. (This still doesn't deal with loss in transmission well).

Alternatively, Google's Protocol Buffer , or Msgpack to help along with some of the intermediary process.

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