Java Socket實現(xiàn)的傳輸對象功能示例
本文實例講述了Java Socket實現(xiàn)的傳輸對象功能。分享給大家供大家參考,具體如下:
前面兩篇文章介紹了怎樣建立Java Socket通信,這里說一下怎樣使用Java Socket來傳輸對象。
首先需要一個普通的對象類,由于需要序列化這個對象以便在網(wǎng)絡(luò)上傳輸,所以實現(xiàn)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來接收和發(fā)送socket中的InputStream和OutputStream,然后轉(zhuǎn)換成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對象實例了。
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java Socket編程技巧總結(jié)》、《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
詳解SpringBoot中異步請求和異步調(diào)用(看完這一篇就夠了)
這篇文章主要介紹了SpringBoot中異步請求和異步調(diào)用問題,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04
解決Springboot2.1.x配置Activiti7單獨數(shù)據(jù)源問題
這篇文章主要介紹了Springboot2.1.x配置Activiti7單獨數(shù)據(jù)源問題,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09
Java concurrency集合之ConcurrentHashMap_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了Java concurrency集合之ConcurrentHashMap的相關(guān)資料,需要的朋友可以參考下2017-06-06
java網(wǎng)絡(luò)之基于UDP的聊天程序示例解析
這篇文章主要介紹了java網(wǎng)絡(luò)之基于UDP的聊天程序示例解析,文中通過步驟及示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2020-08-08
Java中的封裝性(包含this關(guān)鍵字,構(gòu)造器等)
這篇文章主要介紹了Java中的封裝性(包含this關(guān)鍵字,構(gòu)造器等)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03

