如何在 Java 中通过 tcp 发送数组列表? 我需要从客户端向服务器发送整数数组列表,反之亦然。
谢谢
最佳答案
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 通过 java 中的 tcp?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6712470/