| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 692 人关注过本帖
标题:Socket字节流与字符流混合应用?
只看楼主 加入收藏
xianfeng_28
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2005-12-6
收藏
 问题点数:0 回复次数:0 
Socket字节流与字符流混合应用?
我做网络通信一段时间,目前发现一个当TCP/Socket通信时,字节流与字符流混合应用出现一个奇怪问题,拿出来与大家讨论下:
服务端接收文件代码:
import *;
import *;

/**
 * <p>Title: </p>
 * <p>Description: Socket 服务端用来接收文件</p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class SocketReceFile extends Thread{
    private Socket socket;
    public SocketReceFile(Socket socket){
        this.socket = socket;
    }

    public void run(){
        rece();

    }

   
    public void rece(){
         br = null;
         dis = null;
        try{
            br = new (new (socket.getInputStream()));
            dis = new (new (socket.getInputStream()));
            String line = br.readLine();
            System.out.println("起始行:"+line);
            File file = new File("d:/SSTP/Yaoming.jpg");
            if(file.exists()){
                file.delete();
                file.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(file);
            int eve = 2048;
            byte[] buf = new byte[eve];
            int len = dis.read(buf,0,eve);
            int sum = 0;
            while(len != -1){
                System.out.println("len:"+len);
                sum = sum +len;
                fos.write(buf,0,len);
                fos.flush();
                len = dis.read(buf,0,eve);
            }
            System.out.println("sum:"+sum);
            System.out.println("文件接受完毕");
            socket.close();
            br.close();
            fos.close();
            dis.close();

        } catch(Exception ex){
            ex.printStackTrace();
        }

    }


}

客户端发送代码:


import *;
import *;


/**
 * <p>Title: </p>
 * <p>Description:Sokcet客户端用来发送文件 </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class SocketSendFile{
    public SocketSendFile(){
    }

    public static void main(String[] args){
        new SocketSendFile().sendByGzip("127.0.0.1",8312);
    }

    /**
     * 通过字节流发送文件,文件带包头,包头的发送与文件信息的发送需要休眠一段时间
     * @param ip
     * @param port
     */
    public void send(String ip,int port){
         pw = null;
         dos = null;
        Socket socket = null;
        try{
            socket = createClientSocket(ip,port);
            pw = new (socket.getOutputStream());
            dos = new (new (socket.getOutputStream()));
            pw.println("aaaaaaaaaaaaaaaaaaaaaa");
            pw.flush();
            File file = new File("d:/sstptest1.jpg");
            System.out.println("Lenth:" + file.length());
            FileInputStream fis = new FileInputStream(file);
            int eve = 2048;
            byte[] buf = new byte[eve];
            //休眠一段时间,如果不休眠,会导致服务端接受出错
            Thread.sleep(100);
            int len = fis.read(buf,0,eve);
            int sum = 0;
            while(len != -1){
                System.out.println("len:" + len);
                sum = sum + len;
                dos.write(buf,0,len);
                dos.flush();
                len = fis.read(buf,0,eve);
            }
            System.out.println("sum:" + sum);
            socket.close();
            pw.close();
            dos.close();

        } catch(Exception ex){
            ex.printStackTrace();
        }

    }

   
    /**
     * 创建Socket客户端
     * @return
     */
    public Socket createClientSocket(String hostip,int port) throws Exception{
        Socket socket = null;
        try{
            InetSocketAddress it = new InetSocketAddress(hostip,port);
            socket = new Socket();
            socket.connect(it,60 * 1000);
        } catch(Exception ex){
            throw ex;
        }
        return socket;
    }

}

问题说明:服务端接收没有问题,大家可以看到,客户端通过字符流发送数据后,需要休眠一段时间才能通过字节流发送文件,如果不休眠会导致服务端接受的文件不全。
讨论:1、字节流与字符流能混合应用吗,如数据需要带有包头。
      2、为什么一定要休眠呢?这样在实际环境的应用中是否存在不稳定因素?
搜索更多相关主题的帖子: 节流 应用 
2008-06-18 16:23
快速回复:Socket字节流与字符流混合应用?
数据加载中...
 
   



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

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