利用Java实现网络传输数据压缩的实例
当前位置:以往代写 > JAVA 教程 >利用Java实现网络传输数据压缩的实例
2019-06-14

利用Java实现网络传输数据压缩的实例

利用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() );
}
// 剩下的是你本身的措施实现
}

此刻你的通信数据获得了压缩.

    关键字:

在线提交作业