A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© weimoliang 中级黑马   /  2014-5-14 23:30  /  1203 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 weimoliang 于 2014-5-14 23:39 编辑

1.浏览器(除360浏览器,这个屌丝一个。)输入 地址 ,如 127.0.0.1:8989
2.服务端Java代码。打印出http请求协议,然后回应客户一个信息!

看代码:
  1. public class MyServer {

  2.         public static void main(String[] args) {
  3.                 try {
  4.                         ServerSocket ss = new ServerSocket(8989);
  5.                         while(true){
  6.                                 new Thread(new ConcrrunetServer(ss.accept())).start();
  7.                         }
  8.                 } catch (IOException e) {
  9.                         // TODO Auto-generated catch block
  10.                         e.printStackTrace();
  11.                 }
  12.         }
  13.         
  14. }
  15. class ConcrrunetServer implements Runnable{
  16.         private Socket s;
  17.         public ConcrrunetServer(Socket s) {
  18.                 this.s = s;
  19.         }


  20.         @Override
  21.         public void run() {
  22.                 //读取浏览器头信息
  23.                 BufferedInputStream in  = null;
  24.                 //响应浏览器
  25.                 PrintWriter out = null;
  26.                 try {
  27.                         in = new BufferedInputStream(s.getInputStream());
  28.                         out = new PrintWriter(s.getOutputStream());
  29.                         byte[] buf = new byte[1024];
  30.                         
  31.                         int len = 0;
  32.                         while((len = in.read(buf)) != -1){ //问题一,循环读取貌似不能定下来,阻塞到这里了
  33.                                 System.out.println(new String(buf,0,len));
  34.                         }
  35.                         
  36. //                        int len = in.read(buf);
  37. //                        System.out.println(new String(buf,0,len));
  38.                         
  39.                         out.print("<h1>welcom to you !!!</h1>");
  40.                         
  41.                 } catch (IOException e) {
  42.                         // TODO Auto-generated catch block
  43.                         e.printStackTrace();
  44.                 }finally{
  45.                         out.close();//问题2,为什么先关闭输出流浏览器才能得到信息
  46.                         try {
  47.                                 if(in != null)
  48.                                 in.close();
  49.                                 //out.close();
  50.                                 s.close();
  51.                         } catch (IOException e) {
  52.                                 // TODO Auto-generated catch block
  53.                                 e.printStackTrace();
  54.                         }
  55.                         
  56.                         
  57.                 }
  58.                
  59.         }
  60.         
  61. }
复制代码
问题:1.循环读取貌似不能停下来,用下面那个注释掉的就没提了。
           2.输出流为什么要这输入了之前先关闭!


4 个回复

倒序浏览
没做实验,
我可以回答你第二个问题,一般包装流写的都是缓冲区,需要刷新或者直接关闭,才可以写数据
回复 使用道具 举报
本帖最后由 weimoliang 于 2014-5-14 23:40 编辑
vihuela 发表于 2014-5-14 23:36
没做实验,
我可以回答你第二个问题,一般包装流写的都是缓冲区,需要刷新或者直接关闭,才可以写数据 ...

我是说 为什么要在输入流之前关闭!
回复 使用道具 举报
weimoliang 发表于 2014-5-14 23:39
我是说 为什么要在输入流之前关闭!

打印流也要关?你不关没数据?
回复 使用道具 举报
vihuela 发表于 2014-5-14 23:45
打印流也要关?你不关没数据?

统统的关了,你还是没明白什么意思!我是说PrintWriter为什么要在BufferedInputStream之前关闭。不在之前也没数据返回!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马