用TCP写了一个客户端可以向服务器上传文件的程序,主要代码如下
………………………客户端
Socket s = new Socket("127.0.0.1",8888);
FileInputStream fis = new FileInputStream("c:\\dat1.txt");
OutputStream os = s.getOutputStream();
byte[] buf = new byte[1024];
int len;
while((len = fis.read(buf))!= -1){
os.write(buf,0,len);
}
s.shutdownOutput();
………………………服务器端
ServerSocket ss = new ServerSocket(8888);
Socket s = ss.accept();
System.out.println("a Client connect!");
InputStream in = s.getInputStream();
FileOutputStream fos = new FileOutputStream("d:\\dat2.txt");
byte[] buf = new byte[1024];
int len;
while((len = in.read(buf))!= -1){
fos.write(buf,0,len);
}
………………………
程序可以复制文件成功,但是报错
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at java.net.SocketInputStream.read(SocketInputStream.java:90)
at io.Server.main(Server.java:26)
请问Connection reset错误怎么解决??? |