Java Reference
In-Depth Information
and writes the compressed data until the client performs a shutdown, causing the server read
to return
1, indicating an end-of-stream. The server then closes the connection and exits.
After the client calls shutdownOutput() , it needs to read any remaining compressed bytes from
the server.
Our compression client, CompressClient.java , implements the client side of the compres-
sion protocol. The uncompressed bytes are read from the file specified on the command line,
and the compressed bytes are written to a new file. If the uncompressed filename is “ data ”, the
compressed filename is “ data.gz ”. Note that this implementation works for small files, but that
there is a flaw that causes deadlock for large files. (We discuss and correct this shortcoming
in Section 5.2.)
CompressClient.java
0 import java.net.*; // for Socket
1 import java.io.*;
// for IOException and [File]Input/OutputStream
2
3 public class CompressClient {
4
5
public static final int BUFSIZE = 256; // Size of read buffer
6
7
public static void main(String[] args) throws IOException {
8
9
if (args.length != 3) // Test for correct # of args
10
throw new IllegalArgumentException("Parameter(s): <Server> <Port> <File>");
11
12
String server = args[0];
// Server name or IP address
13
int port = Integer.parseInt(args[1]); // Server port
14
String filename = args[2];
// File to read data from
15
16
// Open input and output file (named input.gz)
17
FileInputStream fileIn = new FileInputStream(filename);
18
FileOutputStream fileOut = new FileOutputStream(filename + ".gz");
19
20
// Create socket connected to server on specified port
21
Socket sock = new Socket(server, port);
22
23
// Send uncompressed byte stream to server
24
sendBytes(sock, fileIn);
25
26
// Receive compressed byte stream from server
27
InputStream sockIn = sock.getInputStream();
28
int bytesRead;
// Number of bytes read
29
byte[] buffer = new byte[BUFSIZE]; // Byte buffer
30
while ((bytesRead = sockIn.read(buffer)) != −1) {
31
fileOut.write(buffer, 0, bytesRead);
 
Search WWH ::




Custom Search