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的代码中 只要把输出流一关  就会出错!  谁告诉我一下为什么? 谢谢回答!

解决方案 »

  1.   

    汗 又问题了  现在把output.close() 去掉也报错!
      

  2.   

    首先的一个习惯是close卸载finally里面
    然后看一下有没有写的权限
      

  3.   

    有写权限写在finally里也还是不行。奇怪的是 有的时候偶尔成功一次 后面就一直报那个异常!
      

  4.   

    我把你的代码拷过来,又写了一个客户端,试了一下,没问题,能用,只是:
    byte[] zijie=new byte[1024];           
    while(input.read(zijie)>0)
    {
        output.write(zijie);
        output.flush();
    }
    要注意最后一次读到的有可能是不满的,所以不能把整个zijie写出去,要用
    output.write(zijie,0,len);
    在循环外定义:
    int len=0;
    while((len=input.read(zijie))>0){
        ...
    }
    也就是读到多少写多少,否则最后一次是会把上一次的残留也写出去的。
    另外就是虽然没问题能运行,但还是要把关流的代码写在finally里比较安全
      

  5.   

    我的客户端代码是:
    /**
     * 
     */
    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();
    }
    }

    }}
      

  6.   

    用你的确实没有错了 不过不知道我的为啥有错! 下面是我的客户端代码:package ok;import java.io.*;
    import java.net.*;public class Frame1 {
        public static void main(String[] args) {
            try {
                Socket soc = new Socket("127.0.0.1", 1991);
                BufferedInputStream input = new BufferedInputStream(soc.
                        getInputStream());
                BufferedOutputStream output = new BufferedOutputStream(new
                        FileOutputStream(new File("d:\\1.bmp")));            byte[] zijie=new byte[1024];
                while(input.available()>0)
                {
                    input.read(zijie);
                    output.write(zijie);
                    output.flush();
                }            System.out.println("传完了");
                input.close();
                output.close();            if (soc.isClosed()==false)
                {
                    soc.close();
                }        } catch (Exception ex) {
                System.out.println("有错误:" + ex.getMessage());
            }
        }}