有人可以建议如何通过套接字将多个序列化对象通过套接字发送到服务器吗? 目前我的服务器始终只是第一个对象。
客户端:
public void communicate() throws UnknownHostException, IOException {
carClientSocket = new Socket("localhost", 4444);
carClientOR = new ObjectOutputStream(carClientSocket.getOutputStream());
Car[] CarList = new Car[11];
for ( int i = 1; i <=10; i++ ) {
//Car alpha;
CarList[i] = new Car("Alpha Romeo","GTI",1999+i);
}
for ( int i =1; i<= 10; i++) {
carClientOR.writeObject(CarList[i]);
carClientOR.flush();
//carClientOR.reset();
}
carClientSocket.close();
}
服务器端
public void communicate() throws IOException, ClassNotFoundException {
CarServerSocket = new ServerSocket(4444);
System.out.println("Server is ready and waiting for connection from client..\n");
while (true) {
carSocket = CarServerSocket.accept();
System.out.println("Server Connected");
carServerIR = new ObjectInputStream(carSocket.getInputStream());
Car alpha1 = (Car) carServerIR.readObject();
System.out.println("Object received from car clinet: " + alpha1.getCarBrad() + " " + alpha1.getCarModel() + " " + alpha1.getCarYear());
}
//carSocket.close();
//CarServerSocket.close();
}
答案 0 :(得分:0)
问题出在服务器端,因为调用function applySentenceCase()
{
var selector = "toto";
return selector.charAt(0).toUpperCase() + selector.substr(1).toLowerCase();
}
在循环内。它应该在循环之前完成一次,然后你应该得到其他对象:
carSocket = CarServerSocket.accept();