标题:求问以下怎么实现网络编程中客户端向服务器传输多个对象(就是一个对象集合 ...
只看楼主
帝师
Rank: 2
来 自:湖南
等 级:论坛游民
帖 子:166
专家分:92
注 册:2018-10-11
结帖率:92.31%
 问题点数:0 回复次数:6 
求问以下怎么实现网络编程中客户端向服务器传输多个对象(就是一个对象集合)?感谢!!
我这里只写了一个传输成功了多个报错
客户:
@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);
        //}
    }
搜索更多相关主题的帖子: 客户端 port socket Environment 对象 
2020-11-23 19:44
帝师
Rank: 2
来 自:湖南
等 级:论坛游民
帖 子:166
专家分:92
注 册:2018-10-11
得分:0 
我从新写了哈,我传输完了然后他给我抛了个
at (ObjectInputStream.java:2958)
    at (ObjectInputStream.java:1538)
    at (ObjectInputStream.java:431)
    at com.briup.smart.env.server.ServerImpl.reciver(ServerImpl.java:49)
    at com.briup.smart.env.server.ServerImpl.main(ServerImpl.java:21)
是为什么啊,我所有数据已经传输完毕了。
客户:
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();
                oos.writeObject(next);
            }
            
    }

服务器:
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());
        Object object = null;
        
        while((object = bis.readObject()) != null) {
            
            Environment e = (Environment)object;
            System.out.println(e);
            i++;
        }
    }

I am the voice of the next generation
Completely digital
Create synthetic auras
2020-11-23 20:13
帝师
Rank: 2
来 自:湖南
等 级:论坛游民
帖 子:166
专家分:92
注 册:2018-10-11
得分:0 


I am the voice of the next generation
Completely digital
Create synthetic auras
2020-11-23 20:14
帝师
Rank: 2
来 自:湖南
等 级:论坛游民
帖 子:166
专家分:92
注 册:2018-10-11
得分:0 
已经解决

I am the voice of the next generation
Completely digital
Create synthetic auras
2020-11-24 09:37
sssooosss
Rank: 9Rank: 9Rank: 9
等 级:禁止访问
威 望:3
帖 子:664
专家分:1115
注 册:2019-8-27
得分:0 
共同学习
2020-11-27 08:30
晓磊
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2020-11-24
得分:0 
整的挺好
2020-11-27 09:28
wd297333852
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2020-11-30
得分:0 
加油!一起学习
2020-11-30 07:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-503982-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.442127 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved