黑马程序员技术交流社区

标题: 在并发上传图片的程序中遇到的关于死循环的问题 [打印本页]

作者: 孙国军    时间: 2012-4-27 00:00
标题: 在并发上传图片的程序中遇到的关于死循环的问题
  1. /*
  2. 需求:
  3.         通过TCP的Socket和ServerSocket,向服务端上传图片;
  4. */
  5. import java.io.*;
  6. import java.net.*;

  7. class SocketClient
  8. {
  9.         public static void main(String[] args)
  10.         {
  11.                 File file=new File("yuanye.jpg");
  12.                 if (!file.exists())
  13.                 {
  14.                         throw new RuntimeException("你所关联的图片不存在");
  15.                 }
  16.                 BufferedInputStream bufi=null;
  17.                 try
  18.                 {
  19.                         bufi=new BufferedInputStream(new FileInputStream(file));       
  20.                 }
  21.                 catch (FileNotFoundException fe)
  22.                 {
  23.                         throw new RuntimeException("您所关联的文件不存在");
  24.                 }
  25.                 Socket s=null;
  26.                 try
  27.                 {
  28.                         s=new Socket("192.168.0.4",10000);
  29.                 }
  30.                 catch (UnknownHostException ue)
  31.                 {
  32.                         throw new RuntimeException("你说要连接的服务器地址有误");
  33.                 }
  34.                 catch (IOException ie)
  35.                 {
  36.                         throw new RuntimeException("套接字建立失败");
  37.                 }
  38.                 BufferedOutputStream bufo=null;
  39.                 try
  40.                 {
  41.                         bufo=new BufferedOutputStream(s.getOutputStream());

  42.                         byte[] buf=new byte[1024];

  43.                         int len=0;

  44.                         while ((len=bufi.read(buf))!=-1)
  45.                         {
  46.                                 bufo.write(buf,0,len);
  47.                                 bufo.flush();

  48.                         }                //为什么这会变成死循环啊???
  49.                         System.out.println("到这里..............");
  50.                         s.shutdownOutput();

  51.                         byte[] b=new byte[1024];

  52.                         int num=s.getInputStream().read(b);

  53.                         System.out.println(new String(b,0,num));
  54.                 }
  55.                 catch (IOException ie)
  56.                 {
  57.                         throw new RuntimeException("文件上传失败");
  58.                 }               
  59.         }               
  60. }


  61. class ClientThread implements Runnable
  62. {
  63.         private Socket s;
  64.         public ClientThread(Socket s)
  65.         {
  66.                 this.s=s;
  67.         }
  68.         public void run()
  69.         {
  70.                 File file=new File("yuanye复件.jpg");

  71.                 InputStream is=null;               
  72.                 try
  73.                 {
  74.                         is=s.getInputStream();                       
  75.                 }
  76.                 catch (IOException ie)
  77.                 {
  78.                         throw new RuntimeException("获取用户端读取流失败");
  79.                 }
  80.                 BufferedOutputStream bufo=null;
  81.                 try
  82.                 {
  83.                         bufo=new BufferedOutputStream(new FileOutputStream(file));

  84.                         byte[] buf=new byte[1024];

  85.                         int len=0;

  86.                         while ((len=is.read(buf))!=-1)
  87.                         {
  88.                                 bufo.write(buf,0,len);
  89.                                 bufo.flush();
  90.                         }
  91.                         OutputStream os=s.getOutputStream();

  92.                         os.write("上传成功".getBytes());
  93.                 }
  94.                 catch (IOException ie)
  95.                 {
  96.                         throw new RuntimeException("文件上传失败");
  97.                 }
  98.                 finally
  99.                 {
  100.                         try
  101.                         {
  102.                                 if (bufo!=null)
  103.                                 {
  104.                                         bufo.close();
  105.                                 }
  106.                         }
  107.                         catch (IOException i)
  108.                         {
  109.                                 throw new RuntimeException("输出流关闭失败");
  110.                         }
  111.                         try
  112.                         {
  113.                                 s.close();
  114.                         }
  115.                         catch (IOException i)
  116.                         {
  117.                                 throw new RuntimeException("关闭客户端失败");
  118.                         }
  119.                 }
  120.         }
  121. }

  122. class  SocketServer
  123. {
  124.         public static void main(String[] args) throws Exception
  125.         {
  126.                 ServerSocket ss=new ServerSocket(10000);
  127.                
  128.                 while (true)
  129.                 {
  130.                         Socket s=ss.accept();

  131.                         new Thread(new ClientThread(s));
  132.                 }
  133.         }
  134. }
复制代码
问题在48行处,while(){}发生了死循环;

作者: 孙国军    时间: 2012-4-27 08:57
怎么一直没有人回答啊???
作者: 孙国军    时间: 2012-4-27 09:27
孙国军 发表于 2012-4-27 08:57
怎么一直没有人回答啊???

人  了   ,那些大师都跑哪去聊啊
作者: 丁佼    时间: 2012-4-27 10:01
  new Thread(new ClientThread(s)).start(),现成是要运行才可以的。。。。

============================================================

运行你的代码,果然卡起,怀疑是客户端关闭网络输出流的时候出错了,更改代码:
                System.out.println("到这里..............");
                s.shutdownOutput();
                System.out.println("到这里2..............");//更改1
发现可以输出 "到这里2.............." 可以得知是正确的输出文件了,打开文件夹发现服务端根本没有建立“yuanye复件.jpg”文件,怀疑服务端file流写错,查看一圈,没错。。。。
回到服务端主函数,想查看下是不是建立socket时错了,突然发现。。。你线程根本就没有start()啊。。

============================================================

这样竟然也可以编译通过。。java容忍度是在很高了。。。还是有什么专门的使用意义,求高手解释。
难道是
       Thread t = new Thread(new ClientThread(s));
       t.start();


...................2分钟后,求人不如求己,放到服务端代码里试一下,还真是正确运行了,java牛B了。。。。
以上,回答完毕。




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