fis.close();
s.close();
}
}
class PicServer{
public static void main(String[] args) throws IOException{
ServerSocket ss = new ServerSocket(10007);
Socket s = ss.accept();
InputStream in = s.getInputStream();
FileOutputStream fos = new FileOutputStream("c:\\server.jpeg");
byte[] buf = new byte[1024];
int len = 0;
//循环结束不了
while((len = in.read(buf))!=-1){
fos.write(buf, 0, len);
}
OutputStream os = s.getOutputStream();
os.write("传输完成".getBytes());
fos.close();
s.close();
ss.close();
}
}
数据传输已完成但是会抛异常
异常信息:
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at com.itheima.day24.PicServer.main(TcpPicDemo.java:70)