Java Reference
In-Depth Information
Shut down the socket output stream: line 50
After reading and sending all of the bytes from the input file, shut down the output
stream, notifying the server that the client is finished sending. The close will cause a
1 return from read() on the server.
To implement the compression server, we simply write a protocol and factory for our
threaded server architecture. Our protocol implementation, CompressProtocolFactory.java ,
implements the server-side compression protocol using the GZIP compression algorithm. The
server receives the uncompressed bytes from the client and writes them to a GZIPOutputStream ,
which wraps the socket's output stream.
CompressProtocolFactory.java
0 import java.net.*; // for Socket
1 import java.io.*; // for IOException and Input/OutputStream
2 import java.util.*; // for ArrayList
3 import java.util.zip.*; // for GZIPOutputStream
4
5 public class CompressProtocolFactory implements ProtocolFactory {
6
7
public static final int BUFSIZE = 1024;
// Size of receive buffer
8
9
public Runnable createProtocol(final Socket clntSock, final Logger logger) {
10
return new Runnable() {
11
public void run() {
12
CompressProtocolFactory.handleClient(clntSock, logger);
13
}
14
};
15
}
16
17
public static void handleClient(Socket clntSock, Logger logger) {
18
ArrayList entry = new ArrayList();
19
entry.add("Client address and port="+
20
clntSock.getInetAddress().getHostAddress() + ":" +
21
clntSock.getPort());
22
entry.add("Thread="+Thread.currentThread().getName());
23
24
try {
25
// Get the input and output streams from socket
26
InputStream in = clntSock.getInputStream();
27
GZIPOutputStream out = new GZIPOutputStream(clntSock.getOutputStream());
28
29
byte[] buffer = new byte[BUFSIZE];
// Allocate read/write buffer
30
int bytesRead;
// Number of bytes read
31
// Receive until client closes connection, indicated by −1 return
 
Search WWH ::




Custom Search