一、前提
多台客户端 / 服务器 之间传递实体类的序列化对象
需要实现四个类,即服务器类,线程类,客户端类及实体类
注:实体类需实现接口:implements Serializable
二、服务器类
服务器类,需要实现两个类:ServerSocket 和 Socket 。且 ServerSocket 初始化需要给个参数(端口号)
.accept() 方法是阻塞当前 socket 对象(socket 在最后是需要关闭的)。处于等待状态,直到客户端发过来信息唤醒它
1 public class Servlet {
2
3 public static void main(String[] args) {
4 // 服务器
5 try {
6
7 ServerSocket serverSocket = new ServerSocket(5000);
8 Socket socket = new Socket();
9
10 // 无限循环,服务器保持开启状态
11 while(true){
12 socket = serverSocket.accept();
13 ScoktThread st = new ScoktThread(socket);
14 st.start();
15 }
16 } catch (IOException e) {
17 e.printStackTrace();
18 }
19 }
20 }
三、线程类
在有参构造方法中初始化 Socket 对象。此线程类根据启动该线程对象来对该对象进行一系列操作。
1 public class ScoktThread extends Thread{
2
3 private Socket socket;
4
5 public ScoktThread(Socket socket) {
6 super();
7 this.socket = socket;
8 }
9
10 @Override
11 public void run() {
12 // TODO Auto-generated method stub
13 super.run();
14 InputStream is = null;
15 ObjectInputStream ois =null;
16 OutputStream os =null;
17
18 try {
19 // 获取客户端发来的信息
20 is = socket.getInputStream();
21 ois = new ObjectInputStream(is);
22 User user = (User) ois.readObject();
23 System.out.println("成功接收到客户端信息:"+user.getUserName()+"-"+user.getPwd());
24
25 // 获取客户端信息
26 InetAddress id = socket.getInetAddress();
27 String ip = id.getHostAddress();
28 System.out.println("客户 ip 地址:"+ip);
29
30 // 反馈信息
31 String restr = "我是服务器,欢迎你加入";
32 os = socket.getOutputStream();
33 os.write(restr.getBytes());
34
35 } catch (IOException e) {
36 e.printStackTrace();
37 } catch (ClassNotFoundException e) {
38 e.printStackTrace();
39 } finally {
40 try {
41 ois.close();
42 is.close();
43 socket.close();
44 } catch (IOException e) {
45 e.printStackTrace();
46 }
47 }
48 }
49 }
四、客户端类
客户端类,需要实现 Socket 类。初始化时需要两个参数:要连接服务器的 ip 地址(localhost,即127.0.0.1 是本地地址)+对应的端口号
1 public static void main(String[] args) {
2 // 客户端
3 Socket socket;
4 try {
5 socket = new Socket("localhost",5000);
6
7 OutputStream os = socket.getOutputStream();
8 ObjectOutputStream oos = new ObjectOutputStream(os);
9 User user = new User("小居","居先生");
10 oos.writeObject(user);
11
12 // 成功发送后需要暂时关闭输出流
13 socket.shutdownOutput();
14
15 InputStream in = socket.getInputStream();
16 BufferedReader br = new BufferedReader(new InputStreamReader(in));
17 String str = null;
18 while((str = br.readLine())!=null){
19 System.out.println(str);
20 }
21
22 br.close();
23 in.close();
24 oos.close();
25 os.close();
26 socket.close();
27
28
29 } catch (UnknownHostException e1) {
30 e1.printStackTrace();
31 } catch (IOException e1) {
32 e1.printStackTrace();
33 }
34 }
五、实体类(不重要)
实体类仅需注意,要实现 Serializable 接口。
1 /**
2 * 用户类
3 * @author ice_debj
4 *
5 */
6 public class User implements Serializable{
7
8 /**
9 *
10 */
11 private static final long serialVersionUID = 3144628657162072787L;
12 private String userName;
13 private String pwd;
14
15 public User() {
16 super();
17 }
18 public User(String userName, String pwd) {
19 super();
20 this.userName = userName;
21 this.pwd = pwd;
22 }
23
24 public String getUserName() {
25 return userName;
26 }
27 public void setUserName(String userName) {
28 this.userName = userName;
29 }
30 public String getPwd() {
31 return pwd;
32 }
33 public void setPwd(String pwd) {
34 this.pwd = pwd;
35 }
36 }
六、总结