Sending compressed data via socket
I have to make a log server in java, and one task is to send the data compressed. Now I am sending it line by line in plain text, but I must compress it. The server handle "HTTP like" request. For example, I can get a log sending "GET xxx.log". This will entablish a TCP connection to the server, the server 开发者_JS百科response with a header and the log, and close the connection. The client, reads line by line and analyzes each LOG entry. I tried some ways without success. My main problem is that I don't know where each line ends(in the client size). Any idea?
Use a GZIPOutputStream at the server and a GZIPInputStream at the client, with an InputStreamReader around that and a BufferedReader around that. Then just read lines.
The thing that would make the most sense is something like this
- Client sends GET foo.log
- Server sends "Size: 15042\n"
- Server sends the compressed log file, whole
- Client parses the Size: header (reading until a newline) and then knows how many bytes to expect
- Client then reads all those bytes from the server (15042 in the example)
- Client decompresses the received data
- Client then processes the log (decompressed you'll find the newlines)
You might take a look at the HTTP Range header. The client can request a range of bytes, and the server will return only those bytes. Use GZIP compression.
You could have an action that you call from the client that will provide an index, with byte ranges, prior to requesting part of the log.
There is a lot of overhead with HTTP headers, so you might consider sending 100 lines at a time or more.
Finally, look at chunked transfer encoding, which will allow you to stream the data to the client.
精彩评论