回答
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;
public class SerializeOverSocket {
private static ExecutorService executorService =
Executors.newSingleThreadExecutor();
public static void main(String[] args) throws Exception {
// Start a server to listen for a client
executorService.submit(new Server());
Thread.sleep(100);
// Send an ArrayList from a client
ArrayList<Integer> integers =
new ArrayList<Integer>(Arrays.asList(1,2,3,4,5));
Socket s = new Socket();
s.connect(new InetSocketAddress("localhost", 1234));
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
out.writeObject(integers);
s.close();
}
static class Server implements Runnable {
public void run() {
try {
ServerSocket server = new ServerSocket(1234);
Socket clientSocket = server.accept();
ObjectInputStream in =
new ObjectInputStream(clientSocket.getInputStream());
Object o = in.readObject();
System.out.println("Received this object on the server: " + o);
clientSocket.close();
server.close();
executorService.shutdown();
} catch (IOException e) {
// TODO: Write me
throw new UnsupportedOperationException("Not written");
} catch (ClassNotFoundException e) {
// TODO: Write me
throw new UnsupportedOperationException("Not written");
}
}
}
}
查看序列化。 Java的ArrayList對象已經實現了可序列化的接口,所以你只需要學習如何使用它。
這個例子說明了如何寫一個ArrayList到一個文件,但相同的概念applys來發送它通過套接字。
抱歉,我不能訪問該網站:( – helloThere
對不起關於這一點,我的貼子發生了一些奇怪的事情,在這裏它是agian http://www.java2s.com/Tutorial/Java/0140__Collections/ArrayListimplementstheemptySeriali zableinterface.htm – xunil154
非常有幫助thanx;) – helloThere
的最簡單的方法是:
- 連載到
byte[]
(或直接寫入到輸出流如圖所示由Ryan) - 打開套接字
- 寫字節在客戶端
- 接收服務器上的字節
- 反序列化
byte[]
(或從Ryan所示的流中讀取)
到handle serialization使用ObjectOutputStream
和ObjectInputStream
。您也可以使用commons-lang SerializationUtils
,它們爲您提供單行序列化和反序列化。
我讀過你的評論上面,關於我的意思是魚:)但事情是,我已經讀了多少次流和套接字我不能從來沒有完全理解:( – helloThere
我知道這是一個非常古老的問題,但也許更新版本的Java lang使通過TCP發送對象比過去更容易。這看起來比較簡單。結帳下面的例子:你的TCP連接的
客戶端:你的TCP連接的
//All the imports here
//...
public class Client {
private ObjectInputStream in; //The input stream
private ObjectOutputStream out; //The output stream
private Socket socket; //The socket
public Client(){
//Initialize all the members for your
//Client class here
}
//Using your sendData method create
//an array and send it through your
//output stream object
public void sendData(int[] myIntegerArray) {
try {
//Note the out.writeObject
//this means you'll be passing an object
//to your server
out.writeObject(myIntegerArray);
out.flush();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
服務器端:
//All the imports here
//...
public class Server {
private ObjectInputStream in; //The input stream
private ObjectOutputStream out; //The output stream
private ServerSocket serverSocket; //The serverSocket
public Server(){
//Initialize all the members for your
//Server class here
}
//Using your getData method create
//the array from the serialized string
//sent by the client
public void getData() {
try {
//Do a cast to transform the object
//into the actual object you need
//which is an Integer array
int[] data = (int[]) in.readObject();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
- 1. Java - 通過TCP傳遞對象ArrayList
- 2. 通過的ArrayList JAVA
- 3. 在Java中通過TCP發送ArrayList <String[]>的最佳方法?
- 4. 在Java中創建通用ArrayList的ArrayList
- 5. 迭代通過的ArrayList中的Java
- 6. 通過套接字的Java TCP通信
- 7. Java,通過ArrayList循環
- 8. 通過ArrayList正在更新+ Java
- 9. 通過TCP發送到C的Java
- 10. 通過的ArrayList
- 11. 對象的ArrayList通過級Java
- 12. 如何通過ArrayList中獲得的ArrayList
- 13. C++,通過TCP
- 14. 通過TCP
- 15. Java - 通過TCP套接字傳輸BufferedImage
- 16. 用Java通過TCP發送JSON對象
- 17. java - TCP - 通過函數檢查數據
- 18. 通過ArrayList(Java)迭代遇到問題
- 19. 嘗試在java中通過tcp發送kilobyte消息時出錯
- 20. 在Java中沒有通過TCP接收所有消息?
- 21. 在java中通過TCP發送數據記錄
- 22. 在Java中通過TCP流式傳輸數組
- 23. 如何通過tor在java中創建tcp連接?
- 24. 在java中通過TCP發送混合字符串和字節
- 25. 如何在java中通過TCP發送OBJECT?
- 26. 無法通過TCP
- 27. 通過TCP客戶
- 28. 通過用戶輸入在ArrayList中查找對象 - Java
- 29. 通過在Java中傳遞參數來更新ArrayList
- 30. 在Java中,我什麼時候可以通過sparseArray使用ArrayList?
哦,你擁有他所有的魚:)我的回答只給他他的杆和一個告訴他如何釣魚) – Bozho
哈!那麼,我想這是真的。 +1。 –
這當然會爲他節省很多時間,但他可能只是複製粘貼它。 @OP在使用它之前嘗試理解示例。 – Bozho