Software caused connection abort: socket write error?
package ok;
import java.net.*;
import java.io.*;
public class Application1 {
public static void main(String args[]) {
try {
ServerSocket server = new ServerSocket(1991);
Socket soc = server.accept();
BufferedOutputStream output = new BufferedOutputStream(soc.
getOutputStream());
BufferedInputStream input = new BufferedInputStream(new
FileInputStream(new File(
"C:\\Documents and Settings\\Administrator\\My Documents\\1.bmp ")));
byte[] zijie=new byte[1024];
while(input.read(zijie)> 0)
{
output.write(zijie);
output.flush();
}
input.close();
output.close(); //把这个去掉就没错了
soc.close();
server.close();
} catch (Exception ex) {
System.out.println( "有错误发生了! " + ex.getMessage());
}
}
}
上面是我写的传图片的代码。 在运行的时候会报
Software caused connection abort: socket write error 这个错误?
可是如果把output.close();去掉后就不会有错了 事实上在我写的所有有关socket的代码中 只要把输出流一关 就会出错! 谁告诉我一下为什么? 谢谢回答!
[解决办法]
我的客户端代码是:
/**
*
*/
package ok;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @author arkwrightzhn
*
*/
public class Application2 {
/**
* @param args
*/
public static void main(String[] args) {
Socket socket=null;
InputStream is=null;
FileOutputStream fos=null;
try {
socket=new Socket( "localhost ",1991);
is=socket.getInputStream();
fos=new FileOutputStream( "C:\\Documents and Settings\\Administrator\\My Documents\\2.bmp ");
byte[] bs=new byte[1024];
int len=0;
//int i=0;
while((len=is.read(bs))> 0){
//i++;
//System.out.println(i);
fos.write(bs,0,len);
fos.flush();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(fos!=null) fos.close();
if(is!=null) is.close();
if(socket!=null) socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}