-2

我有一个 android 客户端和一个多线程 Java 服务器。该服务器最初是用 Python 编写的,运行良好,但现在我用 Java 重新编写了它,它似乎无法正常工作。下面是我的服务器代码。值得注意的是,Python 实现不是多线程的,但我认为无论如何我都不需要为此更改客户端。

import java.net.*;
import java.io.*;
import org.apache.commons.io.FileUtils;

public class MultiServerThread extends Thread {
    private Socket socket = null;

    public MultiServerThread(Socket socket) {
        super("MultiServerThread");
        this.socket = socket;
    }

    @Override
    public void run() {

        try {
            String path = "C:/Users/LandClan/Desktop/cubikal";
            int count = 0;
            DataOutputStream dos = new DataOutputStream(
                    new BufferedOutputStream(socket.getOutputStream()));
            DataInputStream dis = new DataInputStream(new BufferedInputStream(
                    socket.getInputStream()));

            File[] files = new File(path).listFiles();
            for (File file : files) {
                String filename = file.getName();
                String extension = filename.substring(
                        filename.lastIndexOf(".") + 1, filename.length());
                if ("png".equals(extension)) {
                    count += 1;
                }

            }
            System.out.println("Sending " + count + " files");
            dos.writeInt(count);
            byte[] temp = new byte[1024];
            int n = 0;
            for (File file : files) {
                String filename = file.getName();
                String extension = filename.substring(
                        filename.lastIndexOf(".") + 1, filename.length());
                if ("png".equals(extension)) {
                    FileInputStream fis = new FileInputStream(file);
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    byte fileContent[] = new byte[(int) file.length()];
                    bis.read(fileContent);
                    int dataLength = fileContent.length;
                    dos.writeInt(dataLength);
                    System.out.println(filename + " is " + dataLength
                            + " bytes long");
                    while ((dataLength > 0)
                            && (n = bis.read(temp, 0,
                                    (int) Math.min(temp.length, dataLength))) != -1) {
                        dos.write(temp, 0, n);
                        dos.flush();
                        dataLength -= n;
                    }
                    // System.out.println("Sent file "+filename);
                    fis.close();
                }
            }
            for (File file1 : files) {
                String filename = file1.getName();
                String extension = filename.substring(
                        filename.lastIndexOf(".") + 1, filename.length());
                if ("txt".equals(extension)) {
                    FileInputStream fis = new FileInputStream(file1);
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    byte fileContent[] = new byte[(int) file1.length()];
                    bis.read(fileContent);
                    int dataLength = fileContent.length;
                    dos.writeInt(dataLength);
                    System.out.println("file is " + dataLength + "long");
                    while ((dataLength > 0)
                            && (n = bis.read(temp, 0,
                                    (int) Math.min(temp.length, dataLength))) != -1) {
                        dos.write(temp, 0, n);
                        dos.flush();
                        dataLength -= n;
                    }
                    // System.out.println("Sent file");
                    fis.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这是服务器的第一部分

package server;

import java.net.*;
import java.io.*;

public class Server {
    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = null;
        boolean listening = true;

        try {
            serverSocket = new ServerSocket(4447);
        } catch (IOException e) {
            System.err.println("Could not liten on port: 4447.");
            System.exit(-1);
        }

        while (listening) {
            new MultiServerThread(serverSocket.accept()).start();
        }
        serverSocket.close();
    }
}
4

1 回答 1

0

DataOutputStream 似乎永远不会关闭,并且 DataInputStream 根本不会使用。

摆脱 DataInputStream ,并确保在完成后关闭 DataOutputStream 。

一个好方法是向finally{}try-catch的.try-catchfinally

DataOutputStream dos = null;
try
{
    DataOutputStream dos = new DataOutputStream(
                new BufferedOutputStream(socket.getOutputStream()));
    // do stuff
}
catch(IOException e)
{
    //stacktrace etc
}
finally
{
    if (dos != null) dos.close();
}

这种东西在 Java 中总是有点难看,尽管即将推出的版本可能会变得更好......

于 2012-09-21T19:13:33.267 回答