Socket Server在上传文件后抛出“Socket is closed”

时间:2014-06-09 12:13:57

标签: java sockets

我有一个Java服务器上传Android客户端的文件。它执行上传但是当它完成时,它会继续抛出:

Error::java.net.SocketException: Socket is closed

我搜索了很多代码,但没有找到任何东西。由于它是在上传文件后抛出的,我认为问题是在最后一个循环之后的代码结束,但我没有看错。任何想法的家伙?

class ClientThread extends Thread {
  // the socket where to listen/talk
  String Type;
  Socket socket;
  ObjectInputStream sInput;
  ObjectOutputStream sOutput;
  // my unique id (easier for deconnection)
  int id;

  // Constructore
  ClientThread(Socket socket) throws InterruptedException {
    // a unique id
    id = ++uniqueId;
    this.socket = socket;
    /* Creating both Data Stream */
    System.out.println("Thread trying to create Object Input/Output Streams");
    while (!jobdone) {
      try {
        // create output first
        sOutput = new ObjectOutputStream(socket.getOutputStream());
        sInput = new ObjectInputStream(socket.getInputStream());
        // read the username
        OutputStream os = socket.getOutputStream();
        FileInputStream fis = null;
        DataOutputStream dos = new DataOutputStream(os);

        String Request = (String) sInput.readObject();
        System.out.println(Request);

        String[] todoname = Request.split("\\@reza-hp");
        String name = todoname[0];
        System.out.println("Connecting...");

        File fil = new File("D://Users//ProfileImages//reza");

        System.out.println(fil);
        File[] Files = fil.listFiles();
        System.out.println(Files);
        for (int count = 0; count < Files.length; count++) {
          System.out.println(Files[count].getName());

        }

        os = socket.getOutputStream();
        dos = new DataOutputStream(os);

        dos.writeInt(Files.length);

        for (int count = 0; count < Files.length; count++) {
          dos.writeUTF(Files[count].getName());

        }
        for (int count = 0; count < Files.length; count++) {

          int filesize = (int) Files[count].length();
          dos.writeInt(filesize);
        }

        for (int count = 0; count < Files.length; count++) {

          int filesize = (int) Files[count].length();
          byte[] buffer = new byte[filesize];

          fis = new FileInputStream(Files[count].toString());
          BufferedInputStream bis = new BufferedInputStream(fis);

          // Sending file name and file size to the server
          bis.read(buffer, 0, buffer.length); // This line is
                                              // important
          dos.write(buffer, 0, buffer.length);
          dos.flush();
          fis.close();
        }

        dos.close();
        os.close();

      } catch (Exception e) {
        System.out.println("Error::" + e);
      }
    }

    Thread.sleep(5000);
    Server.shutdown();
  }
}

2 个答案:

答案 0 :(得分:2)

您应该使用ServerSocket侦听来自客户端应用程序的传入连接。简单的Socket非常直截了当。打开连接,交换数据,关闭连接,配置。你可以重新绑定你的套接字,但这不是你应该做的。

ServerSocket server=new ServerSocket(yourPort);
Socket clientSocket=server.listen(); // here you will block until incoming connection

查看文档以获取更多信息here

答案 1 :(得分:-1)

您的套接字已从其他人

关闭