0

我从套接字 inputStream 读取时遇到问题。但只在课堂上使用一种方法。这是我的代码:服务器:

private void copyFile(String mainPath, String path) {
    outS.println("Sending file!");
    outS.println(path);
    outS.flush();

    BufferedInputStream fis = null;
    OutputStream os;

    File file = new File(mainPath);
    String str;
    byte[] buffer = new byte[4096];
    long numOfChunks = file.length() / 4096, num = 0, lng;
    try {
        fis = new BufferedInputStream(new FileInputStream(file));
        os = socket.getOutputStream();
        outS.println(numOfChunks);
        fis.read(buffer, 0, 4096);
        os.write(buffer, 0, buffer.length);
        os.flush();
        num++;
    } catch (FileNotFoundException ex) {
        System.out.println("<ERROR> Clerk, copyFile: File not found.");
        System.out.println(ex);
    } catch (IOException ex) {
        System.out.println("<ERROR> Clerk, copyFile: Could not write or read file.");
        System.out.println(ex);
    } finally {
        try {
            fis.close();
        } catch (IOException ex) {
            Logger.getLogger(Clerk.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    waitEnd();
}

客户:

private void copyFile(String destination) {
    FileOutputStream fos = null;
    long numOfChunks, num = 0;
    SBuffer sBuffer;
    byte[] buffer = new byte[4096];
    InputStream is;
    try {
        fos = new FileOutputStream(new File(destination));
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        is = socket.getInputStream();
        numOfChunks = Long.parseLong(inS.readLine());
        System.out.println(num + "/" + numOfChunks);
        int bytesRead = is.read(buffer, 0, buffer.length);
        bos.write(buffer, 0, bytesRead);
        num++;
    } catch (FileNotFoundException ex) {
        System.out.println("<ERROR> Client, copyFile: File not found.");
        System.out.println(ex);
    } catch (IOException ex) {
        System.out.println("<ERROR> Client, copyFile: Could not write or read file.");
        System.out.println(ex);
    } finally {
        try {
            fos.close();
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    outS.println("end");
    outS.flush();
}

让我解释几件事。一切正常,但客户端在线int bytesRead = is.read(buffer, 0, buffer.length)会卡住,因为流中没有任何内容(is.available = 0)。我不知道为什么。虽然我多次从服务器发送它。

而且我无法关闭流,因为套接字也会关闭。

方法 waitEnd() 等待套接字输入流中的字符串“end”。

我在互联网上搜索了很多教程和东西,但没有人帮助。

建立连接的代码:

服务器:

public void run() {
    try {
        ssocket = new ServerSocket(2332);
        Socket socket = ssocket.accept();
        clerk = new Clerk(socket, mainPath);
        (new Thread(clerk)).start();
    } catch (IOException ex) {
    }
    try {
        ssocket.close();
    } catch (IOException ex) {
    }
}


public Clerk(Socket socket, String path) {
    this.socket = socket;
    mainTree = new FileTree(path);
}

public Client(String address, String[] dirs) {
    try {
        socket = new Socket(address, 2332);
    } catch (UnknownHostException ex) {
    } catch (IOException ex) {
    }
}

客户和职员运行相同的方法:

    @Override
public void run() {
    try {
        inS = new BufferedReader(iss);
        outS = new PrintWriter(socket.getOutputStream(), true);
        oos = new ObjectOutputStream(socket.getOutputStream());
        iss = new InputStreamReader(socket.getInputStream());
        ois = new ObjectInputStream(socket.getInputStream());
    } catch (IOException ex) {
    }
    msg();
    try {
        inS.close();
        iss.close();
        outS.close();
        ois.close();
        oos.close();
        socket.close();
    } catch (IOException ex) {
    }
}
4

1 回答 1

0

解决。我只为文件传输创建了第二个套接字。

于 2012-12-27T20:44:56.973 回答