转自:https://i.cnblogs.com/EditPosts.aspx?opt=1

前面两篇文章介绍了怎样建立Java Socket通信,这一篇说一下怎样使用Java Socket来传输对象。

首先需要一个普通的对象类,由于需要序列化这个对象以便在网络上传输,所以实现java.io.Serializable接口就是必不可少的了,如下:

 1 package com.googlecode.garbagecan.test.socket.sample3;  
 2  
 3 public class User implements java.io.Serializable {  
 4     private static final long serialVersionUID = 1L;  
 5     private String name;  
 6     private String password;  
 7  
 8     public User() {  
 9           
10     }  
11       
12     public User(String name, String password) {  
13         this.name = name;  
14         this.password = password;  
15     }  
16       
17     public String getName() {  
18         return name;  
19     }  
20  
21     public void setName(String name) {  
22         this.name = name;  
23     }  
24  
25     public String getPassword() {  
26         return password;  
27     }  
28  
29     public void setPassword(String password) {  
30         this.password = password;  
31     }  
32 } 

对于Server端的代码,代码中分别使用了ObjectInputStream和ObjectOutputStream来接收和发送socket中的InputStream和OutputStream,然后转换成Java对象,如下:

 1 package com.googlecode.garbagecan.test.socket.sample3;  
 2  
 3 import java.io.*;  
 4 import java.net.ServerSocket;  
 5 import java.net.Socket;  
 6 import java.util.logging.Level;  
 7 import java.util.logging.Logger;  
 8  
 9 public class MyServer {  
10  
11     private final static Logger logger = Logger.getLogger(MyServer.class.getName());  
12       
13     public static void main(String[] args) throws IOException {  
14         ServerSocket server = new ServerSocket(10000);  
15  
16         while (true) {  
17             Socket socket = server.accept();  
18             invoke(socket);  
19         }  
20     }  
21  
22     private static void invoke(final Socket socket) throws IOException {  
23         new Thread(new Runnable() {  
24             public void run() {  
25                 ObjectInputStream is = null;  
26                 ObjectOutputStream os = null;  
27                 try {  
28                     is = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));  
29                     os = new ObjectOutputStream(socket.getOutputStream());  
30  
31                     Object obj = is.readObject();  
32                     User user = (User)obj;  
33                     System.out.println("user: " + user.getName() + "/" + user.getPassword());  
34  
35                     user.setName(user.getName() + "_new");  
36                     user.setPassword(user.getPassword() + "_new");  
37  
38                     os.writeObject(user);  
39                     os.flush();  
40                 } catch (IOException ex) {  
41                     logger.log(Level.SEVERE, null, ex);  
42                 } catch(ClassNotFoundException ex) {  
43                     logger.log(Level.SEVERE, null, ex);  
44                 } finally {  
45                     try {  
46                         is.close();  
47                     } catch(Exception ex) {}  
48                     try {  
49                         os.close();  
50                     } catch(Exception ex) {}  
51                     try {  
52                         socket.close();  
53                     } catch(Exception ex) {}  
54                 }  
55             }  
56         }).start();  
57     }  
58 } 

Client也和Server端类似,同样使用ObjectOutputStream和ObjectInputStream来处理,如下:

 1 package com.googlecode.garbagecan.test.socket.sample3;  
 2  
 3 import java.io.BufferedInputStream;  
 4 import java.io.IOException;  
 5 import java.io.ObjectInputStream;  
 6 import java.io.ObjectOutputStream;  
 7 import java.net.Socket;  
 8 import java.util.logging.Level;  
 9 import java.util.logging.Logger;  
10  
11 public class MyClient {  
12       
13     private final static Logger logger = Logger.getLogger(MyClient.class.getName());  
14       
15     public static void main(String[] args) throws Exception {  
16         for (int i = 0; i < 100; i++) {  
17             Socket socket = null;  
18             ObjectOutputStream os = null;  
19             ObjectInputStream is = null;  
20               
21             try {  
22                 socket = new Socket("localhost", 10000);  
23       
24                 os = new ObjectOutputStream(socket.getOutputStream());  
25                 User user = new User("user_" + i, "password_" + i);  
26                 os.writeObject(user);  
27                 os.flush();  
28                   
29                 is = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));  
30                 Object obj = is.readObject();  
31                 if (obj != null) {  
32                     user = (User)obj;  
33                     System.out.println("user: " + user.getName() + "/" + user.getPassword());  
34                 }  
35             } catch(IOException ex) {  
36                 logger.log(Level.SEVERE, null, ex);  
37             } finally {  
38                 try {  
39                     is.close();  
40                 } catch(Exception ex) {}  
41                 try {  
42                     os.close();  
43                 } catch(Exception ex) {}  
44                 try {  
45                     socket.close();  
46                 } catch(Exception ex) {}  
47             }  
48         }  
49     }  
50 } 

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

 



posted on 2018-12-02 21:31  Sharpest  阅读(273)  评论(0编辑  收藏  举报