黑马程序员技术交流社区

标题: 关于ServerServer中读取Socket中的InpuStream [打印本页]

作者: 康乐    时间: 2012-12-22 18:49
标题: 关于ServerServer中读取Socket中的InpuStream
本帖最后由 康乐 于 2012-12-23 20:42 编辑
  1. import java.io.*;
  2. import java.net.*;

  3. class TcpClient2
  4. {
  5.         public static void main(String[] args) throws Exception
  6.         {
  7.                 Socket s =  new Socket(InetAddress.getLocalHost(),10002);

  8.                 OutputStream out = s.getOutputStream();

  9.                 out.write("请求数据".getBytes());
  10.                
  11.                 InputStream in = s.getInputStream();
  12.                 byte[] buf = new byte[1024];
  13.                 int len = 0;
  14.                 while((len = in.read(buf))!=-1)
  15.                 {
  16.                         System.out.println(new String(buf,0,len));
  17.                 }

  18.                 s.close();
  19.         }
  20. }
  21. class TcpServer2
  22. {
  23.         public static void main(String[] args) throws Exception
  24.         {
  25.                 ServerSocket ss = new ServerSocket(10002);

  26.                 Socket s = ss.accept();

  27.                 String ip = s.getInetAddress().getHostAddress();
  28.                 System.out.println(ip+"connect");

  29.                 InputStream in = s.getInputStream();
  30.                 byte[] buf = new byte[1024];
  31.                
  32.                 int len = in.read(buf);
  33.                 System.out.println(new String(buf,0,len));
  34.                
  35.                 /*为什么这样写会阻塞?
  36.                
  37.                 int len = 0;
  38.                 while((len = in.read(buf))!= -1)
  39.                 {
  40.                         System.out.println(len+new String(buf,0,len));
  41.                 }
  42.                 */
  43.         
  44.                 OutputStream out = s.getOutputStream();
  45.                 out.write("收到请求,已处理".getBytes());

  46.                 s.close();
  47.                 ss.close();

  48.         }
  49. }
复制代码
代码如上,为什么在服务端读取Socket中的InputStream时,用while时,程序会阻塞?
int len = in.read(buf);
System.out.println(new String(buf,0,len));语句后面再加一句System.out.println(in.read(buf))也会阻塞。
为什么呢?
  int len = 0;
   while((len = in.available())>0)
   {
    in.read(buf);
    int count = len<buf.length?len:buf.length;
    System.out.print(new String(buf,0,count));
   }
这样能解决问题,可是,为什么呢

作者: 康乐    时间: 2012-12-22 19:01
在读取文件时,如果用
  1. int len = 0;

  2.              while((len = in.read(buf))!= -1)

  3.               {

  4.                         System.out.println(len+new String(buf,0,len));

  5.               }
复制代码
也不会阻塞,为什么偏偏在这不行呢
作者: 王进亮    时间: 2012-12-22 20:16
read()为阻塞式方法。你的客户端只有一条发送语句( 12行:out.write("请求数据".getBytes());),
服务端有两条接收语句(39行: int len = in.read(buf);45行:while((len = in.read(buf))!= -1))
当客户端发送信息时,服务端(39行)收到了数据,执行到45行时,客户端就没有再发数据,服务端(45行)就处于等待状态。下面的语句就不会执行、
客户端(17行)也在等待,这样就造成了两方都在等待.........









欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2