黑马程序员技术交流社区
标题:
TCP问题 求各位进来指点下...
[打印本页]
作者:
张然龙
时间:
2014-6-1 21:38
标题:
TCP问题 求各位进来指点下...
本帖最后由 张然龙 于 2014-6-2 10:05 编辑
不管怎么试都不能跳出while循环,不管是上传端还是接收端都是如此 求各位解决一下,我用println打印了一下len的值 ,发现一直是1024.根本没出现过其他数字 这是为什么呢?而且我的图片完美的上传发送了过去....
import java.io.*;
import java.net.*;
class Dxc implements Runnable
{
public void run()
{
try
{
Socket s=new Socket("192.168.1.106",12000);
OutputStream os=s.getOutputStream();
FileInputStream fis=new FileInputStream("D:\\截图.bmp");
byte b[]=new byte[1024];
int len;
while ((len=fis.read(b))!=-1)
{
os.write(b,0,len);
os.flush();
}
System.out.println("上传端跳出循环");
s.shutdownOutput();
s.close();
fis.close();
} catch (Exception e){}
}
}
class Dxc2 implements Runnable
{
public void run()
{
try
{
ServerSocket ss=new ServerSocket(12000);
Socket s=ss.accept();
InputStream is=s.getInputStream();
byte b[]=new byte[1024*1024];
int len;
BufferedOutputStream bw=new BufferedOutputStream(new FileOutputStream("D:\\收到.bmp"));
while((len=is.read(b))!=-1)
{
bw.write(b,0,len);
bw.flush();
}
System.out.println("接收端跳出循环");
ss.close();
bw.close();
}catch(Exception e){}
}
}
public class Demo3
{
public static void main(String args[])throws Exception
{
new Thread(new Dxc2()).start();
Thread.sleep(200);
new Thread(new Dxc()).start();
}
}
复制代码
作者:
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
你是在逗我们玩吗?看看
package demo;
import java.io.*;
import java.net.*;
class Dxc implements Runnable
{
public void run()
{
try
{
Socket s=new Socket("192.168.1.101",12000);
OutputStream os=s.getOutputStream();
FileInputStream fis=new FileInputStream("D:\\1.jpg");
byte b[]=new byte[1024];
int len;
while ((len=fis.read(b))!=-1)
{
os.write(b,0,len);
System.out.println(len);
os.flush();
}
System.out.println("上传端跳出循环");
s.shutdownOutput();
s.close();
fis.close();
} catch (Exception e){}
}
}
class Dxc2 implements Runnable
{
public void run()
{
try
{
ServerSocket ss=new ServerSocket(12000);
Socket s=ss.accept();
InputStream is=s.getInputStream();
byte b[]=new byte[1024*1024];
int len;
BufferedOutputStream bw=new BufferedOutputStream(new FileOutputStream("D:\\收到.bmp"));
while((len=is.read(b))!=-1)
{
bw.write(b,0,len);
System.out.println(len);
bw.flush();
}
System.out.println("接收端跳出循环");
ss.close();
bw.close();
}catch(Exception e){}
}
}
public class Demo3
{
public static void main(String args[])throws Exception
{
new Thread(new Dxc2()).start();
Thread.sleep(200);
new Thread(new Dxc()).start();
}
}
复制代码
输出结果:
1024
1024
1024
1024
1024
829
829
上传端跳出循环
接收端跳出循环
复制代码
最后图片复制成功
作者:
张然龙
时间:
2014-6-1 23:38
.....我的和你的不一样啊....什么情况
作者:
张然龙
时间:
2014-6-1 23:41
无语了我都..我这个最后一个是1024,然后就没了,然后while循环就卡住了,上传端跳出循环这个控制台就没有...
作者:
张然龙
时间:
2014-6-2 10:04
睡一觉..重启下电脑...好了..无语.. 不好意思了大家..
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2