前面两篇文章介绍了怎样建立Java Socket通信,这一篇说一下怎样使用Java Socket来传输对象。
创新互联公司成立十年来,这条路我们正越走越好,积累了技术与客户资源,形成了良好的口碑。为客户提供网站制作、网站建设、网站策划、网页设计、域名注册、网络营销、VI设计、网站改版、漏洞修补等服务。网站是否美观、功能强大、用户体验好、性价比高、打开快等等,这些对于网站建设都非常重要,创新互联公司通过对建站技术性的掌握、对创意设计的研究为客户提供一站式互联网解决方案,携手广大客户,共同发展进步。
首先需要一个普通的对象类,由于需要序列化这个对象以便在网络上传输,所以实现java.io.Serializable接口就是必不可少的了,如下:
- package com.googlecode.garbagecan.test.socket.sample3;
- 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端的代码,代码中分别使用了ObjectInputStream和ObjectOutputStream来接收和发送socket中的InputStream和OutputStream,然后转换成Java对象,如下:
- package com.googlecode.garbagecan.test.socket.sample3;
- import java.io.*;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- 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();
- invoke(socket);
- }
- }
- private static void invoke(final Socket socket) throws IOException {
- new Thread(new Runnable() {
- public void run() {
- ObjectInputStream is = null;
- ObjectOutputStream os = null;
- try {
- is = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
- os = new ObjectOutputStream(socket.getOutputStream());
- Object obj = is.readObject();
- User user = (User)obj;
- System.out.println("user: " + user.getName() + "/" + user.getPassword());
- user.setName(user.getName() + "_new");
- user.setPassword(user.getPassword() + "_new");
- os.writeObject(user);
- os.flush();
- } catch (IOException ex) {
- logger.log(Level.SEVERE, null, ex);
- } catch(ClassNotFoundException ex) {
- logger.log(Level.SEVERE, null, ex);
- } finally {
- try {
- is.close();
- } catch(Exception ex) {}
- try {
- os.close();
- } catch(Exception ex) {}
- try {
- socket.close();
- } catch(Exception ex) {}
- }
- }
- }).start();
- }
- }
Client也和Server端类似,同样使用ObjectOutputStream和ObjectInputStream来处理,如下:
- package com.googlecode.garbagecan.test.socket.sample3;
- import java.io.BufferedInputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.Socket;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- 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 < 100; i++) {
- Socket socket = null;
- ObjectOutputStream os = null;
- ObjectInputStream is = null;
- try {
- socket = new Socket("localhost", 10000);
- os = new ObjectOutputStream(socket.getOutputStream());
- User user = new User("user_" + i, "password_" + i);
- os.writeObject(user);
- os.flush();
- is = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
- Object obj = is.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 {
- is.close();
- } catch(Exception ex) {}
- try {
- os.close();
- } catch(Exception ex) {}
- try {
- socket.close();
- } catch(Exception ex) {}
- }
- }
- }
- }
***测试上面的代码,首先运行Server类,然后运行Client类,就可以分别在Server端和Client端控制台看到接收到的User对象实例了。
原文链接:http://blog.csdn.net/kongxx/article/details/7259827
当前题目:JavaSocket实战之三:传输对象
文章网址:http://www.shufengxianlan.com/qtweb/news45/224695.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联