最近学习socket
自己写了个C/S
写好后发现
如果我用client上传文件到server
第一次是成功的
但第二次再上传的话
报错socket closed但我全篇代码都没有调用socket.close()方法?
这是为何?

解决方案 »

  1.   

    你代码里肯定有socket.close()了。
      

  2.   

    真的没有
    我用ctrl+f 都close
    都是I/O流的地方用到的。。
      

  3.   


    public class Method
    {
    // 发送方法
    public void sender(Socket _socket, String _filePath, Boolean _sendFile)
    throws IOException
    {
    // 如果发送的是文件
    if (_sendFile == true)
    {
    File file = new File(_filePath);
    // 读文件
    DataInputStream dis = new DataInputStream(new BufferedInputStream(
    new FileInputStream(file)));
    // 写文件到输出流
    DataOutputStream dos = new DataOutputStream(
    _socket.getOutputStream());
    dos.writeBoolean(true); int bufferSize = 1024;
    byte[] buf = new byte[bufferSize]; while (true)
    {
    int read = 0;
    if (dis != null)
    {
    read = dis.read(buf);
    } if (read == -1)
    {
    break;
    }
    dos.write(buf, 0, read);
    }
    dis.close();
    dos.flush();
    dos.close();
    System.out.println("file send done!!!\r\n\r\n\r\n");
    } // 如果发送的是请求
    else if (_sendFile == false)
    {
    DataOutputStream dos = new DataOutputStream(
    _socket.getOutputStream());
    dos.writeBoolean(false);
    dos.flush();
    dos.close();
    } } // 接收方法
    public void receiver(Socket _socket, String _filePath) throws IOException
    {
    // 接收输入流
    DataInputStream dis = new DataInputStream(new BufferedInputStream(
    _socket.getInputStream()));
    Boolean getFile = dis.readBoolean(); // 如果收到的是文件,保存
    if (getFile == true)
    {
    DataOutputStream dos = new DataOutputStream(
    new BufferedOutputStream(new BufferedOutputStream(
    new FileOutputStream(_filePath)))); int bufferSize = 1024;
    byte[] buf = new byte[bufferSize];
    while (true)
    {
    int read = 0;
    if (dis != null)
    {
    read = dis.read(buf);
    } if (read == -1)
    {
    break;
    }
    dos.write(buf, 0, read);
    }
    dos.flush();
    dos.close();
    System.out.println("file receive success!!!\r\n\r\n\r\n"); }

    // 如果收到的是请求,发送所需文件
    else if(getFile == false)
    {
    this.sender(_socket, _filePath, true);
    } }
      

  4.   

    public class Method
    {
    // 发送方法
    public void sender(Socket _socket, String _filePath, Boolean _sendFile)
    throws IOException
    {
    // 如果发送的是文件
    if (_sendFile == true)
    {
    File file = new File(_filePath);
    // 读文件
    DataInputStream dis = new DataInputStream(new BufferedInputStream(
    new FileInputStream(file)));
    // 写文件到输出流
    DataOutputStream dos = new DataOutputStream(
    _socket.getOutputStream());
    dos.writeBoolean(true); int bufferSize = 1024;
    byte[] buf = new byte[bufferSize]; while (true)
    {
    int read = 0;
    if (dis != null)
    {
    read = dis.read(buf);
    } if (read == -1)
    {
    break;
    }
    dos.write(buf, 0, read);
    }
    dis.close();
    dos.flush();
    dos.close();
    System.out.println("file send done!!!\r\n\r\n\r\n");
    } // 如果发送的是请求
    else if (_sendFile == false)
    {
    DataOutputStream dos = new DataOutputStream(
    _socket.getOutputStream());
    dos.writeBoolean(false);
    dos.flush();
    dos.close();
    } } // 接收方法
    public void receiver(Socket _socket, String _filePath) throws IOException
    {
    // 接收输入流
    DataInputStream dis = new DataInputStream(new BufferedInputStream(
    _socket.getInputStream()));
    Boolean getFile = dis.readBoolean(); // 如果收到的是文件,保存
    if (getFile == true)
    {
    DataOutputStream dos = new DataOutputStream(
    new BufferedOutputStream(new BufferedOutputStream(
    new FileOutputStream(_filePath)))); int bufferSize = 1024;
    byte[] buf = new byte[bufferSize];
    while (true)
    {
    int read = 0;
    if (dis != null)
    {
    read = dis.read(buf);
    } if (read == -1)
    {
    break;
    }
    dos.write(buf, 0, read);
    }
    dos.flush();
    dos.close();
    System.out.println("file receive success!!!\r\n\r\n\r\n"); }

    // 如果收到的是请求,发送所需文件
    else if(getFile == false)
    {
    this.sender(_socket, _filePath, true);
    } }
      

  5.   

    、public class Client
    { /**
     * @param args
     * @throws IOException
     * @throws UnknownHostException
     */
    public static void main(String[] args) throws UnknownHostException,
    IOException
    {
    // TODO Auto-generated method stub String IP = "192.168.1.102";
    int PORT = 30000;
    Socket client = new Socket(IP, PORT);
    String filePath = "E:\\TEST\\Client\\contact.txt";

        Method method = new Method(); int readInt = 65535; while (readInt != 0)
    {
    System.out.println("would do you want to do ?");
    System.out.println("===============================");
    System.out.println("for upload,     input 1");
    System.out.println("for download ,  input 2");
    System.out.println("for quit,       input 0");
    System.out.println("===============================");
    System.out.println("plz make a choice:"); Scanner in = new Scanner(System.in);
    readInt = in.nextInt(); if (readInt == 1)
    {
    //send file
    method.sender(client, filePath, true);
    } else if (readInt == 2)
    {
    //send file request
    method.sender(client, filePath, false);
    //get file
    DataInputStream dis = new DataInputStream(new BufferedInputStream(
    client.getInputStream()));
    do
    {
    method.receiver(client, filePath);
    }while(dis == null);
    } else if(readInt == 0)
    {
    break;
    }else
    {
    System.out.println("wrong choice!!!\r\n\r\n\r\n");
    }
    }
    }}
      

  6.   

    public class Server
    { /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException
    {
    // TODO Auto-generated method stub
            String filePath = "E:\\TEST\\Server\\contact.txt";
    ServerSocket ss = new ServerSocket(30000);

    System.out.println("waiting client ..."); while(true)
    {
    Socket server = ss.accept();
    System.out.println("client has been linked!!!"); Method method = new Method();
    method.serverManager(server, filePath);
    }

    }}
      

  7.   

    dos.close()
    流关闭,Socket就关闭。
      

  8.   

    public class Method
    {
    // 发送方法
    public void sender(Socket _socket, String _filePath, Boolean _sendFile)
    throws IOException
    {
    // 如果发送的是文件
    if (_sendFile == true)
    {
    File file = new File(_filePath);
    // 读文件
    DataInputStream dis = new DataInputStream(new BufferedInputStream(
    new FileInputStream(file)));
    // 写文件到输出流
    DataOutputStream dos = new DataOutputStream(
    _socket.getOutputStream());
    dos.writeBoolean(true); int bufferSize = 1024;
    byte[] buf = new byte[bufferSize]; while (true)
    {
    int read = 0;
    if (dis != null)
    {
    read = dis.read(buf);
    } if (read == -1)
    {
    break;
    }
    dos.write(buf, 0, read);
    }
    dis.close();
    dos.flush();
    dos.close();
    System.out.println("file send done!!!\r\n\r\n\r\n");
    } // 如果发送的是请求
    else if (_sendFile == false)
    {
    DataOutputStream dos = new DataOutputStream(
    _socket.getOutputStream());
    dos.writeBoolean(false);
    dos.flush();
    dos.close();
    } } // 接收方法
    public void receiver(Socket _socket, String _filePath) throws IOException
    {
    // 接收输入流
    DataInputStream dis = new DataInputStream(new BufferedInputStream(
    _socket.getInputStream()));
    Boolean getFile = dis.readBoolean(); // 如果收到的是文件,保存
    if (getFile == true)
    {
    DataOutputStream dos = new DataOutputStream(
    new BufferedOutputStream(new BufferedOutputStream(
    new FileOutputStream(_filePath)))); int bufferSize = 1024;
    byte[] buf = new byte[bufferSize];
    while (true)
    {
    int read = 0;
    if (dis != null)
    {
    read = dis.read(buf);
    } if (read == -1)
    {
    break;
    }
    dos.write(buf, 0, read);
    }
    dos.flush();
    dos.close();
    System.out.println("file receive success!!!\r\n\r\n\r\n"); }

    // 如果收到的是请求,发送所需文件
    else if(getFile == false)
    {
    this.sender(_socket, _filePath, true);
    } } public void serverManager(Socket _socket, String _filePath)
    throws IOException
    {
    this.receiver(_socket, _filePath);
    }}
      

  9.   

    dos.flush()就可以将数据发出去。
      

  10.   

    我把close注释掉以后
    文件传过去是空的 没数据的
      

  11.   

    while (true)
    {
    int read = 0;
    if (dis != null)
    {
    read = dis.read(buf);
    } if (read == -1)
    {
    break;
    }
    dos.write(buf, 0, read);
    }这段代码写的大大的有问题