求问以下怎么实现网络编程中客户端向服务器传输多个对象(就是一个对象集合)?感谢!!
我这里只写了一个传输成功了多个报错客户:
@Override
public void send(Collection<Environment> c) throws Exception {
// TODO Auto-generated method stub
String host = "127.0.0.1";
int port = 8888;
socket = new Socket(host, port);
oos = new ObjectOutputStream(socket.getOutputStream());
Iterator<Environment> iterator = c.iterator();
//while(iterator.hasNext()) {
Environment next = iterator.next();
//System.out.println(next);
oos.writeObject(next);
oos.flush();
//}
}
服务:
@Override
public void reciver() throws Exception {
// TODO Auto-generated method stub
int port = 8888;
server = new ServerSocket(port);
System.out.println("服务器启动,监听端口"+port+",等待客户端的连接");
socket = server.accept();
System.out.println("服务器接收到客户端的连接:"+socket);
bis = new ObjectInputStream(socket.getInputStream());
int len = -1;
//while((len = bis.read()) != -1) {
Object object = bis.readObject();
Environment e = (Environment)object;
System.out.println(e);
//}
}