-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ますが、DataOutputStreamをの外部で宣言して、try-catchで表示されるようにする必要がありますfinally

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 に答える