简体   繁体   中英

Send Byte array then String then Byte over socket Java

I need to send a text message to server over Java socket and then to send a byte array and then a string etc... What I've developed until now is working but the client manages to read only the first string that has been sent.

From the server side: I send byte array using BufferedOutputStream , and PrintWriter to send string.

The problem is that the client and the server are not synchronized, I mean the server send string then byte array then string without waiting for the client to consume each needed byte.

I mean the scenario is NOT like this:

Server        Client 
Send String   read String
Send byte     read byte

But it is like this:

Server        Client
Send String   
Send byte
Send String   
Send byte 

               read String   
               read byte
               read String   
               read byte

Something that could be useful is that I know exactly the size of each string and each byte array to be read.

Here is the methods used to send string and byte array respectively:

   // Send String to Client
    // --------------------------------------------------------------------
    public  void sendStringToClient (
                         String response, 
                         PrintWriter output) {
    try {
        output.print(response); 
        output.flush();

    } catch(Exception e) {
        e.printStackTrace();
    }
    System.out.println("send Seeder String : " + response);
    }

    // Send Byte to Client
    // --------------------------------------------------------------------
    public  void sendByteToClient (
                           byte[] response, 
                           BufferedOutputStream output) {
    try {
        output.write(response, 0, response.length); 
        //System.out.println("send : " + response);
    } catch (IOException e) {
        e.printStackTrace();
    }
    }

Here is the methods used to read string and byte array respectively:

 public byte[] readInByte(int size) {

byte[] command = new byte[size];    
try {
    this.inByte.read(command);
} catch (IOException e) {
    e.printStackTrace();
}
return command;
}

public String readInString(int size) {
char[] c = new char[size];
try{
    this.inString.read(c, 0, size);
} catch (IOException e) {
    e.printStackTrace();
}
return String.valueOf(c);
}

Something that could be useful is that i know exactly the size of each string to be read and each byte array to be read.

Exactly. That's very common. Basically you length-prefix each message - and you might want to provide more header information than that (is it a string or a byte array message, for example).

You could represent the message length (always in bytes) either as a fixed number of bytes (eg 4, assuming you never need more than 4GB messages) or use a 7-bit encoded integer (where you send 7 bits of the length in each byte, and the top bit just indicates whether this is the last byte of the length or not).

Once you've got message lengths, you're basically set - you've effectively divided the stream of data into self-describing blocks. Job done.

(As an aside, I'd avoid using PrintWriter due to its exception-swallowing nature. You won't actually need the writer once you're doing this though, as you'll probably want to convert each String into a byte array anyway, to count its length in bytes before you send it. Remember to specify the encoding!)

I'd be really tempted to convert the data to JSON format and pipe it over http. You get a ton of benefits including ready made http servers and clients for just about every platform on earth along with the JSON interop not to mention all the built in error handling and recovery processing.

The drawbacks would be the additional overhead of http and the JSON encoding. You didn't mention if this was a UDP or TCP socket so that could be an additional drawback if you were trying to go connectionless.

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