Java中Socket通信-客户端与服务端相互传输对象数据
場(chǎng)景
Java中Socket通信-服務(wù)端和客戶端雙向傳輸字符串實(shí)現(xiàn):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108488556
在上面實(shí)現(xiàn)服務(wù)端與客戶端雙向的通信傳輸字符串之后,客戶端與服務(wù)端怎樣相互傳輸對(duì)象數(shù)據(jù)。
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關(guān)注公眾號(hào)
霸道的程序猿
獲取編程相關(guān)電子書、教程推送與免費(fèi)下載。
實(shí)現(xiàn)
在服務(wù)端的Java項(xiàng)目中新建UserInfo對(duì)象
package com.badao;import java.io.Serializable;public class UserInfo implements Serializable {private long id;private String username;private String password;public UserInfo() {}public UserInfo(long id, String username, String password) {this.id = id;this.username = username;this.password = password;}public long getId() {return id;}public void setId(long id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;} }然后在main方法的下面新建交換對(duì)象的方法
??? //服務(wù)端與客戶端交換對(duì)象數(shù)據(jù)public static void exchangeObject() throws IOException, ClassNotFoundException {ServerSocket serverSocket = new ServerSocket(8088);Socket socket = serverSocket.accept();InputStream inputStream = socket.getInputStream();OutputStream outputStream = socket.getOutputStream();ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);for(int i=0;i<5;i++){UserInfo userInfo = (UserInfo) objectInputStream.readObject();System.out.println("在服務(wù)端打印" + (i + 1) + ":" + userInfo.getId() + " " + userInfo.getUsername() + " " + userInfo.getPassword());UserInfo newUserInfo = new UserInfo();newUserInfo.setId(i+1);newUserInfo.setUsername("公眾號(hào):霸道的程序猿"+(i+1));newUserInfo.setPassword("密碼"+(i+1));objectOutputStream.writeObject(newUserInfo);}objectOutputStream.close();objectInputStream.close();outputStream.close();inputStream.close();socket.close();serverSocket.close();}然后在main方法中調(diào)用該方法。
再來(lái)到客戶端的java項(xiàng)目,同樣新建一個(gè)UserInfo類,同上面一樣。
然后在main方法下新建一個(gè)同服務(wù)端交換數(shù)據(jù)的方法
??? //服務(wù)端與客戶端交換對(duì)象數(shù)據(jù)public static void exchangeObject() throws IOException, ClassNotFoundException {Socket socket = new Socket("localhost",8088);InputStream inputStream = socket.getInputStream();OutputStream outputStream = socket.getOutputStream();ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);for(int i=0;i<5;i++){UserInfo newUserInfo = new UserInfo();newUserInfo.setId(i+1);newUserInfo.setUsername("公眾號(hào):霸道的程序猿"+(i+1));newUserInfo.setPassword("密碼"+(i+1));objectOutputStream.writeObject(newUserInfo);UserInfo userInfo = (UserInfo) objectInputStream.readObject();System.out.println("在客戶端打印" + (i + 1) + ":" + userInfo.getId() + " " + userInfo.getUsername() + " " + userInfo.getPassword());}objectOutputStream.close();objectInputStream.close();outputStream.close();inputStream.close();socket.close();}然后在main方法中調(diào)用該方法。
然后依次運(yùn)行服務(wù)端的main方法和客戶端的main方法
?
需要注意
服務(wù)端先獲得ObjectlnputStream對(duì)象,客戶端就要先獲得ObjectOutputStream對(duì)象;
服務(wù)端先獲得ObjectOutputStream對(duì)象,客戶端就要先獲得ObjectInputStream對(duì)象。
總結(jié)
以上是生活随笔為你收集整理的Java中Socket通信-客户端与服务端相互传输对象数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java中Socket通信-客户端向服务
- 下一篇: Gradle在Windows下的下载安装