黑马程序员技术交流社区
标题:
编译通过,运行出错,多线程客户端
[打印本页]
作者:
Mr.南郭
时间:
2015-3-11 23:24
标题:
编译通过,运行出错,多线程客户端
class PicThread implements Runnable
{
private Socket s;
PicThread(Socket s)
{
this.s = s;
}
public void run()
{
int count = 1;
String ip = s.getInetAddress().getHostAddress();
try
{
System.out.println(ip+"connected.....");
InputStream in = s.getInputStream();
File file = new File(ip+"("+(count)+")"+".jpg");
while(file.exists())
file = new File(ip+"("+(count++)+")"+".jpg");
FileOutputStream fos = new FileOutputStream(file);
byte [] bufin = new byte[1024];
int len = 0;
while((len = in.read(bufin)) != -1)
{
fos.write(bufin,0,len);
}
s.shutdownOutput();
// 建立Socket的输出流输出反馈给客户端的信息
OutputStream os = s.getOutputStream();
os.write("上传成功".getBytes());
// 关闭资源
fos.close();
s.close();
}
catch(Exception e)
{
throw new RuntimeException(ip+":上传失败");
}
}
}
运行结果:
Exception in thread "Thread-0" java.lang.RuntimeException: 192.168.1.100:上传失败
at PicThread.run(PicUpload.java:42)
at java.lang.Thread.run(Thread.java:745)
作者:
Mr.南郭
时间:
2015-3-12 08:11
解决了,在run()方法里的Socket不能关闭,否则客户端执行一次后一直等待,因为它要多线程执行,所以应该把 s.shutdownOutput()一句去掉
作者:
lijifeng
时间:
2015-3-12 16:15
自己就解决了 ,厉害啊
作者:
Mr.南郭
时间:
2015-3-19 20:36
lijifeng 发表于 2015-3-12 16:15
自己就解决了 ,厉害啊
:lol有时候就是卡着了,稍微回过神就好了
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2