问下大家,有没有遇见Connection reset这种异常,连接重置,首先我排除了,程序的问题,搜了很多关于的这个问题的解释,但几种情况我也排除了,确实不知道怎么处理了。所以求教大家。程序就是毕老师的网络编程中图片上传,编译时没问题,运行出了问题。
import java.io.*;
import java.net.*;
class PicClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.1.101",10011);
FileInputStream fis = new FileInputStream("C:\\1.jpg");
OutputStream out = s.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len = fis.read(buf))!=-1)
{
out.write(buf,0,len);
}
//告诉服务端数据已经写完。
s.shutdownOutput();
InputStream in = s.getInputStream();
byte[] bufIn = new byte[1024];
int num = in.read(bufIn);
System.out.println(new String(bufIn,0,num));
fis.close();
s.close();
}
}
/*
服务端
*/
class PicServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10011);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"::::");
InputStream in = s.getInputStream();
FileOutputStream fos = new FileOutputStream("2.jpg");
byte[] buf = new byte[1024];
int len = 0;
while((len=in.read(buf))!=-1);
{
fos.write(buf,0,len);
System.out.println(len);
}
OutputStream out = s.getOutputStream();
out.write("上传成功".getBytes());
fos.close();
s.close();
ss.close();
}
}
该贴已经同步到 王--明的微博 |
|