多臺客戶端 / 服務器 之間傳遞實體類的序列化對象服務器
須要實現四個類,即服務器類,線程類,客戶端類及實體類socket
注:實體類需實現接口:implements Serializableide
服務器類,須要實現兩個類:ServerSocket 和 Socket 。且 ServerSocket 初始化須要給個參數(端口號)this
.accept() 方法是阻塞當前 socket 對象(socket 在最後是須要關閉的)。處於等待狀態,直到客戶端發過來信息喚醒它spa
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 是本地地址)+對應的端口號3d
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 }
5、實體類(不重要)rest
實體類僅需注意,要實現 Serializable 接口。code
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 }