黑马程序员技术交流社区
标题:
Exception in thread "main" java.net.SocketException: Connection reset
[打印本页]
作者:
smart2586
时间:
2013-8-27 23:53
标题:
Exception in thread "main" java.net.SocketException: Connection reset
本帖最后由 smart2586 于 2013-8-28 08:46 编辑
客户端
import java.io.*;
import java.net.*;
class PicC
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.0.100",10070);
FileInputStream fis = new FileInputStream("c:\\1.jpg");
OutputStream os = s.getOutputStream();
BufferedReader brIn =
new BufferedReader(new InputStreamReader(s.getInputStream()));
byte[] by = new byte[1024*10];
int len = 0;
while((len=fis.read(by))!=-1)
{
os.write(by,0,len);
}
//告诉服务端数据已写完
s.shutdownOutput();
String str = brIn.readLine();
so.p(str);
}
}
复制代码
服务端
public class ergs {
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(10070);
Socket socket = ss.accept();
String ip = socket.getInetAddress().getHostAddress();
so.p(ip+"......connected");
FileOutputStream fos = new FileOutputStream("c:\\1_1.jpg");
InputStream in = socket.getInputStream();
//向客户端反馈信息
PrintWriter pw =
new PrintWriter(socket.getOutputStream(),true);
Date d = new Date();
byte[] b =new byte[1024];
int len = 0;
//循环读取socket输入流中的数据
while ((len = in.read(b))!=-1)//这句出异常 {
//将数据写到目标文件
fos.write(b, 0, len);
}
so.p(ip+":上传成功");
pw.write("在 "+d.toString()+" 上传成功"+"\r\n");
pw.close();
}
}
复制代码
服务端出问题了,异常 Exception in thread "main" java.net.SocketException: Connection reset朋友说是关流的问题,就把有些关流的代码删了,,,但问题好像不再那里
作者:
C-STAL白鳍豚
时间:
2013-8-28 09:31
请确保客户端的文件存在, 并且指定的服务器IP地址正确. 理论上应该是没问题, 可以加上try...catch..来处理已知的异常
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2