DataInputStream的使用,读阻塞

今天写了个程序,观看HTTP上传文件的流格式,发现:

DataInputSteam的 readFully(byte b[ ] )方法,当不能完全填充b[ ]时, 会阻塞程序的执行,直到读到足够的数据为止,

程序才继续执行下去!

代码:

 

import  java.io.BufferedReader;
import  java.io.DataInputStream;
import  java.io.DataOutputStream;
import  java.io.EOFException;
import  java.io.IOException;
import  java.io.InputStreamReader;
import  java.io.OutputStream;
import  java.net.ServerSocket;
import  java.net.Socket;

public   class  HttpServer  extends  Thread  {
    
private int SERVER_PORT=3221;
    
private ServerSocket s=null;
    
public HttpServer() throws IOException{
       
        s
=new ServerSocket(SERVER_PORT);
        
    
}

    
public HttpServer(int SERVER_PORT) throws IOException{
            
this.SERVER_PORT=SERVER_PORT;
            s
=new ServerSocket(SERVER_PORT);
            
        
    }

    
    
public void run(){
        
try {
            
            
while(true){
            Socket socket
=s.accept();
            System.out.println(
"响应客户端:"+System.currentTimeMillis());
            
new Answer2Client(socket).start();
            }

            
        }
 catch (IOException e) {
            System.out.println(
"HttpServer-->run()方法中出错");
            e.printStackTrace();
        }

        
    }

     
    
public static void main(String sssp[]) throws Exception{
        HttpServer server
=null;
        
if(sssp.length!=0)
            server
=new HttpServer(Integer.parseInt(sssp[0]));
        
else
           server
=new HttpServer();
        
        server.start();
    }

    
}

class  Answer2Client  extends  Thread {
    
private Socket client=null;
    
private byte[] b=new byte[10240];
    
public Answer2Client(Socket ss){
         
this.client=ss;
    }

    
    
    
public void run(){
        
try {
            BufferedReader  datain
=new BufferedReader(new InputStreamReader(client.getInputStream()));
            
while(true){
                
        
//    datain.readFully(b);
            String line=datain.readLine();
            
if(line==null)break;
                System.out.println(line);
            
            }
//while
            
            System.out.println(
"  结--------束");
            
        }
catch(IOException e) {
            System.out.print(
"  结束");
            e.printStackTrace();
        }

        
//响应
        
        DataOutputStream dataout;
        
try {
            dataout 
= new DataOutputStream(client.getOutputStream());
            String time
=System.currentTimeMillis()+" 时间time";
            dataout.writeChars(
"<html>"+time+"</html>");
            dataout.flush();
            dataout.close();
            client.shutdownOutput();
            client.shutdownInput();    
            client.close();
    
            
        }
 catch (IOException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

    
        
    }

}

程序不能正长结束,不知道怎么关闭套界字,和流

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值