Java Reference
In-Depth Information
32
System.out.print("R");
// Reading progress indicator
33
}
34
System.out.println();
// End progress indicator line
35
36
sock.close();
// Close the socket and its streams
37
fileIn.close();
// Close file streams
38
fileOut.close();
39
}
40
41
private static void sendBytes(Socket sock, InputStream fileIn)
42
throws IOException {
43
OutputStream sockOut = sock.getOutputStream();
44
int bytesRead;
// Number of bytes read
45
byte[] buffer = new byte[BUFSIZE]; // Byte buffer
46
while ((bytesRead = fileIn.read(buffer)) != −1) {
47
sockOut.write(buffer, 0, bytesRead);
48
System.out.print("W");
// Writing progress indicator
49
}
50
sock.shutdownOutput();
// Finished sending
51
}
52
}
CompressClient.java
1.
Application setup and parameter parsing:
lines 0-14
2.
Create socket and open files:
lines 16-21
3.
Invoke
sendBytes()
to transmit bytes:
lines 23-24
4.
Receive the compressed data stream:
lines 26-34
The while loop receives the compressed data stream and writes the bytes to the output
file until an end-of-stream is signaled by a
−
1 from
read()
.
5.
Close socket and file streams:
lines 36-38
6.
sendBytes()
:
lines 41-51
Given a socket connected to a compression server and the file input stream, read all of
the uncompressed bytes from the file and write them to the socket output stream.
Get socket output stream:
line 43
Send uncompressed bytes to compression server:
lines 44-49
The while loop reads from the input stream (in this case from a file) and repeats the
bytes to the socket output stream until end-of-file, indicated by
−
1 from
read()
. Each
write is indicated by a “W” printed to the console.
Search WWH ::
Custom Search