| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1852 人关注过本帖
标题:求问以下怎么实现网络编程中客户端向服务器传输多个对象(就是一个对象集合 ...
取消只看楼主 加入收藏
帝师
Rank: 2
来 自:湖南
等 级:论坛游民
帖 子:166
专家分:92
注 册:2018-10-11
结帖率:92.31%
收藏
 问题点数:0 回复次数:3 
求问以下怎么实现网络编程中客户端向服务器传输多个对象(就是一个对象集合)?感谢!!
我这里只写了一个传输成功了多个报错
客户:
@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
快速回复:求问以下怎么实现网络编程中客户端向服务器传输多个对象(就是一个对象 ...
数据加载中...
 
   



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

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