黑马程序员技术交流社区

标题: TCP问题 求各位进来指点下... [打印本页]

作者: 张然龙    时间: 2014-6-1 21:38
标题: TCP问题 求各位进来指点下...
本帖最后由 张然龙 于 2014-6-2 10:05 编辑

不管怎么试都不能跳出while循环,不管是上传端还是接收端都是如此 求各位解决一下,我用println打印了一下len的值 ,发现一直是1024.根本没出现过其他数字 这是为什么呢?而且我的图片完美的上传发送了过去....
  1. import java.io.*;
  2. import java.net.*;

  3. class Dxc implements Runnable
  4. {
  5.         public void run()
  6.         {
  7.                 try
  8.                 {
  9.                         Socket s=new Socket("192.168.1.106",12000);
  10.                         OutputStream os=s.getOutputStream();
  11.                         FileInputStream fis=new FileInputStream("D:\\截图.bmp");
  12.                         byte b[]=new byte[1024];
  13.                         int len;
  14.                         while ((len=fis.read(b))!=-1)
  15.                         {
  16.                                 os.write(b,0,len);
  17.                                 os.flush();
  18.                         }
  19.                         System.out.println("上传端跳出循环");
  20.                         s.shutdownOutput();
  21.                         s.close();
  22.                         fis.close();
  23.                 } catch (Exception e){}
  24.         }
  25. }
  26. class Dxc2 implements Runnable
  27. {
  28.         public void run()
  29.         {
  30.                 try
  31.                 {
  32.                         ServerSocket ss=new ServerSocket(12000);
  33.                         Socket s=ss.accept();
  34.                         InputStream is=s.getInputStream();
  35.                         byte b[]=new byte[1024*1024];
  36.                         int len;
  37.                         BufferedOutputStream bw=new BufferedOutputStream(new FileOutputStream("D:\\收到.bmp"));
  38.                         while((len=is.read(b))!=-1)
  39.                         {
  40.                                 bw.write(b,0,len);
  41.                                 bw.flush();
  42.                         }
  43.                         System.out.println("接收端跳出循环");
  44.                         ss.close();
  45.                         bw.close();
  46.                 }catch(Exception e){}
  47.         }
  48. }
  49. public class Demo3
  50. {
  51.         public static void main(String args[])throws Exception
  52.         {
  53.                 new Thread(new Dxc2()).start();
  54.                 Thread.sleep(200);
  55.                 new Thread(new Dxc()).start();
  56.         }
  57. }
复制代码





作者: 406957151@qq.co    时间: 2014-6-1 21:41
其实我就抢个沙发
作者: 张然龙    时间: 2014-6-1 21:43
406957151@qq.co 发表于 2014-6-1 21:41
其实我就抢个沙发

你不会是来刷金币的吧!......:Q
作者: 月光海    时间: 2014-6-1 23:07
你是在逗我们玩吗?看看
  1. package demo;
  2. import java.io.*;
  3. import java.net.*;

  4. class Dxc implements Runnable
  5. {
  6.         public void run()
  7.         {
  8.                 try
  9.                 {
  10.                         Socket s=new Socket("192.168.1.101",12000);
  11.                         OutputStream os=s.getOutputStream();
  12.                         FileInputStream fis=new FileInputStream("D:\\1.jpg");
  13.                         byte b[]=new byte[1024];
  14.                         int len;
  15.                         while ((len=fis.read(b))!=-1)
  16.                         {
  17.                                 os.write(b,0,len);
  18.                                 System.out.println(len);
  19.                                 os.flush();
  20.                         }
  21.                         System.out.println("上传端跳出循环");
  22.                         s.shutdownOutput();
  23.                         s.close();
  24.                         fis.close();
  25.                 } catch (Exception e){}
  26.         }
  27. }
  28. class Dxc2 implements Runnable
  29. {
  30.         public void run()
  31.         {
  32.                 try
  33.                 {
  34.                         ServerSocket ss=new ServerSocket(12000);
  35.                         Socket s=ss.accept();
  36.                         InputStream is=s.getInputStream();
  37.                         byte b[]=new byte[1024*1024];
  38.                         int len;
  39.                         BufferedOutputStream bw=new BufferedOutputStream(new FileOutputStream("D:\\收到.bmp"));
  40.                         while((len=is.read(b))!=-1)
  41.                         {
  42.                                 bw.write(b,0,len);
  43.                                 System.out.println(len);
  44.                                 bw.flush();
  45.                         }
  46.                         System.out.println("接收端跳出循环");
  47.                         ss.close();
  48.                         bw.close();
  49.                 }catch(Exception e){}
  50.         }
  51. }
  52. public class Demo3
  53. {
  54.         public static void main(String args[])throws Exception
  55.         {
  56.                 new Thread(new Dxc2()).start();
  57.                 Thread.sleep(200);
  58.                 new Thread(new Dxc()).start();
  59.         }
  60. }
复制代码

输出结果:
  1. 1024
  2. 1024
  3. 1024
  4. 1024
  5. 1024
  6. 829
  7. 829
  8. 上传端跳出循环
  9. 接收端跳出循环
复制代码

最后图片复制成功
作者: 张然龙    时间: 2014-6-1 23:38
.....我的和你的不一样啊....什么情况
作者: 张然龙    时间: 2014-6-1 23:41
无语了我都..我这个最后一个是1024,然后就没了,然后while循环就卡住了,上传端跳出循环这个控制台就没有...
作者: 张然龙    时间: 2014-6-2 10:04
睡一觉..重启下电脑...好了..无语.. 不好意思了大家..




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