Java Socket实战之四 传输压缩对象

Java Socket实战之四 传输压缩对象本文地址:http://blog.csdn.net/kongxx/article/details/7259834JavaSocket实战之一单线程通信JavaSocket实战之二多线程通信JavaSocket实战之三传输对象上一篇文章说到了用JavaSocket来传输对象,但是在有些情况下比如网络环境不好或者对象比较大的情况下需要把数据对象进行压缩然后在传输,此时就需

本文地址:http://blog.csdn.net/kongxx/article/details/7259834

Java Socket实战之一 单线程通信

Java Socket实战之二 多线程通信

Java Socket实战之三 传输对象

上一篇文章说到了用Java Socket来传输对象,但是在有些情况下比如网络环境不好或者对象比较大的情况下需要把数据对象进行压缩然后在传输,此时就需要压缩这些对象流,此时就可以GZIPInputStream和GZIPOutputStream来处理一下socket的InputStream和OutputStream。

仍然需要一个实现了java.io.Serializable接口的简单Java对象

package com.googlecode.garbagecan.test.socket.sample4;

public class User implements java.io.Serializable {
	private static final long serialVersionUID = 1L;
	private String name;
	private String password;

	public User() {
		
	}
	
	public User(String name, String password) {
		this.name = name;
		this.password = password;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
}

在Server端使用,socket的InputStream首先被包装成GZIPInputStream,然后又被包装成ObjectInputStream,而socket的OutputStream首先被包装成GZIPOutputStream,然后又被包装成ObjectOutputStream,如下:

package com.googlecode.garbagecan.test.socket.sample4;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class MyServer {

	private final static Logger logger = Logger.getLogger(MyServer.class.getName());
	
	public static void main(String[] args) throws IOException {
		ServerSocket server = new ServerSocket(10000);

		while (true) {
			Socket socket = server.accept();
			socket.setSoTimeout(10 * 1000);
			invoke(socket);
		}
	}

	private static void invoke(final Socket socket) throws IOException {
		new Thread(new Runnable() {
			public void run() {
				GZIPInputStream gzipis = null;
				ObjectInputStream ois = null;
				GZIPOutputStream gzipos = null;
				ObjectOutputStream oos = null;
				
				try {
					gzipis = new GZIPInputStream(socket.getInputStream());
					ois = new ObjectInputStream(gzipis);
					gzipos = new GZIPOutputStream(socket.getOutputStream());
					oos = new ObjectOutputStream(gzipos);

					Object obj = ois.readObject();
					User user = (User)obj;
					System.out.println("user: " + user.getName() + "/" + user.getPassword());

					user.setName(user.getName() + "_new");
					user.setPassword(user.getPassword() + "_new");

					oos.writeObject(user);
					oos.flush();
					gzipos.finish();
				} catch (IOException ex) {
					logger.log(Level.SEVERE, null, ex);
				} catch(ClassNotFoundException ex) {
					logger.log(Level.SEVERE, null, ex);
				} finally {
					try {
						ois.close();
					} catch(Exception ex) {}
					try {
						oos.close();
					} catch(Exception ex) {}
					try {
						socket.close();
					} catch(Exception ex) {}
				}
			}
		}).start();
	}
}

Client也和Server端类似,同样要不socket的XXXStream包装成GZIPXXXStream,然后再包装成ObjectXXXStream,如下:

package com.googlecode.garbagecan.test.socket.sample4;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class MyClient {
	
	private final static Logger logger = Logger.getLogger(MyClient.class.getName());
	
	public static void main(String[] args) throws Exception {
		for (int i = 0; i < 10; i++) {
			Socket socket = null;
			GZIPOutputStream gzipos = null;
			ObjectOutputStream oos = null;
			GZIPInputStream gzipis = null;
			ObjectInputStream ois = null;
			
			try {
				socket = new Socket();
				SocketAddress socketAddress = new InetSocketAddress("localhost", 10000); 
				socket.connect(socketAddress, 10 * 1000);
				socket.setSoTimeout(10 * 1000);
				
				gzipos = new GZIPOutputStream(socket.getOutputStream());
				oos = new ObjectOutputStream(gzipos);
				User user = new User("user_" + i, "password_" + i);
				oos.writeObject(user);
				oos.flush();
				gzipos.finish();
				
				gzipis = new GZIPInputStream(socket.getInputStream());
				ois = new ObjectInputStream(gzipis);
				Object obj = ois.readObject();
				if (obj != null) {
					user = (User)obj;
					System.out.println("user: " + user.getName() + "/" + user.getPassword());
				}
			} catch(IOException ex) {
				logger.log(Level.SEVERE, null, ex);
			} finally {
				try {
					ois.close();
				} catch(Exception ex) {}
				try {
					oos.close();
				} catch(Exception ex) {}
				try {
					socket.close();
				} catch(Exception ex) {}
			}
		}
	}
}

最后测试上面的代码,首先运行Server类,然后运行Client类,就可以分别在Server端和Client端控制台看到接收到的User对象实例了。

今天的文章Java Socket实战之四 传输压缩对象分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/25481.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注