<b>操纵Java实现网络传输数据压缩的实例</b>[Java编程]
本文“<b>操纵Java实现网络传输数据压缩的实例</b>[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
1:挑选大概成立一个新的套接字.(可以参看SUN´S的"成立一个典型的套接字").
2:成立一个服务器端的套接字
3:成立一个RMIClientSocketFactory
4:成立一个RMIServerSocketFactory
5:成立一个担当了UnicastRemoteObjec的远程对象,从而利用新的factories
按照这一大致的设法,我们来看每一步若何具体的实现.
步骤1: 成立ZipSocket
由于要举行Zip压缩,我们重新成立这样的套接字
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import java.net.Socket;
public class ZipSocket extends Socket {
private InputStream in;
private OutputStream out;
public ZipSocket() { super(); }
public ZipSocket(String host, int port) throws IOException {
super(host, port);
}
public InputStream getInputStream() throws IOException {
if (in == null) {
in = new ZipInputStream(super.getInputStream());
}
return in;
}
public OutputStream getOutputStream() throws IOException {
if (out == null) {
out = new ZipOutputStream(super.getOutputStream());
}
return out;
}
public synchronized void close() throws IOException {
OutputStream o = getOutputStream();
o.flush();
super.close();
}
}
步骤2: 成立ZipServerSocket
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException
public class ZipServerSocket extends ServerSocket{
public ZipServerSocket(int port) throws IOException {
super(port);
}
public Socket accept() throws IOException {
Socket socket = new ZipSocket();
implAccept(socket);
return socket;
}
}
步骤3:成立ZipClientSocketFactory
客户端的factory的成立必须遵守以下的情势:
import java.io.IOException;
import java.io.Serializable;
import java.net.Socket;
import java.rmi.server.RMIClientSocketFactory;
public class ZipClientSocketFactory
implements RMIClientSocketFactory, Serializable {
public Socket createSocket(String host, int port) throws IOException {
ZipSocket socket = new ZipSocket(host, port);
return socket;
}
}
步骤4:成立ZipServerSocketFactory
import java.io.IOException;
import java.io.Serializable;
import java.net.ServerSocket;
import java.rmi.server.RMIServerSocketFactory;
public class ZipServerSocketFactory
implements RMIServerSocketFactory, Serializable {
public ServerSocket createServerSocket(int port) throws IOException {
ZipServerSocket server = new ZipServerSocket(port);
return server;
}
}
步骤5: 成立一个担当了UnicastRemoteObjec的远程对象,从而利用新的factories.
public class YourRMIObject extends UnicastRemoteObject {
public YourRemoteObject( int port ) {
super( port, new ZipClientSocketFactory(), new ZipServerSocketFactory() );
}
// 剩下的是你自己的程序实现
}
目前你的通信数据得到了压缩.
以上是“<b>操纵Java实现网络传输数据压缩的实例</b>[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |