1

学校では、サーバーからクライアントにファイルを送信する必要があるプロジェクトがあります。問題は、サーバーからクライアントにファイルを転送するときに、サーバーが接続をシャットダウンすることです。これまでのコードは次のとおりです。

クライアント:

    public static void main(String argv[]) throws Exception {

    int port = 8888;  //default
    if (argv.length
            > 0) {
        port = Integer.parseInt(argv[0]);
    }

    Socket clientSocket = new Socket("127.0.0.1", port);
    PrintStream outToServer = new PrintStream(
            clientSocket.getOutputStream());
    BufferedReader inFromServer = new BufferedReader(
            new InputStreamReader(clientSocket.getInputStream()));



    File f = new File("dictionaryPart.txt");

    String serverCommand = inFromServer.readLine().toLowerCase();
    while (serverCommand != null) {
        System.out.println(serverCommand);
        switch (serverCommand) {
            case "velkommen":
                outToServer.println("Hej");
                break;
            case "file":
                f = copy(clientSocket, f);
                String matches = CrackerCentralized.checkFile(f);
                System.out.println(matches);
                outToServer.println(matches);
                break;
        }
        serverCommand = inFromServer.readLine().toLowerCase();
    }
}

public static File copy(Socket clientSocket, File f) {
    try {
        int filesize = 2022386;
        int bytesRead;
        int currentTot = 0;

        byte[] buffer = new byte[filesize];
        InputStream is = clientSocket.getInputStream();
        FileOutputStream fos = new FileOutputStream(f);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bytesRead = is.read(buffer, 0, buffer.length);
        currentTot = bytesRead;
        while (bytesRead != -1) {
            bytesRead = is.read(buffer, currentTot, (buffer.length - currentTot));
            if (bytesRead >= 0) {
                currentTot += bytesRead;
            }
        }
        bos.write(buffer, 0, currentTot);
        bos.flush();
        bos.close();
    } catch (IOException ex) {
        Logger.getLogger(Master_Slave_Sockets_Client.class.getName()).log(Level.SEVERE, null, ex);
    }
    return f;
}

サーバ:

    try {
        PrintStream outToClient = new PrintStream(connection.getOutputStream());
        OutputStream os = connection.getOutputStream();
        BufferedInputStream input = new BufferedInputStream(new FileInputStream(f));
        outToClient.println("file");
        final byte[] buffer = new byte[(int) f.length()];
        input.read(buffer, 0, buffer.length);
        os.write(buffer, 0, buffer.length);
        os.write(-1);
        os.flush();
        System.out.println(connection.isClosed());
        os.close();
        System.out.println(connection.isClosed());
    } catch (IOException ex) {
        Logger.getLogger(SocketController.class.getName()).log(Level.SEVERE, null, ex);
    }

接続が閉じ続ける理由を認識しています。ソケットの出力を閉じるには、次のように記述します。

output.close();

しかし、サーバーがクライアントの回答(一致/不一致)をリッスンし続けるためにこれを行う必要がある他の方法がわかりません。これにより、サーバーはさらにファイルを送信する必要があるかどうか、またはクライアントが成功したかどうかを認識できます..サーバーへの接続をシャットダウンせずにファイルで送信することさえ可能ですか? 過去2日間、一日中グーグルで検索しましたが、運がありません

読んでくれてありがとう。

4

2 に答える 2

1

返信してくれた madConan に感謝します。ここにコードを投稿して、他の人が将来使用できるようにします。

サーバーコード

    public void run() {
    try {
        PrintStream outToClient = new PrintStream(connection.getOutputStream());
        OutputStream os = connection.getOutputStream();
        BufferedInputStream input = new BufferedInputStream(new FileInputStream(f));
        outToClient.println("file");
        copy(input, os, f);
        System.out.println(connection.isClosed());
    } catch (IOException ex) {
        Logger.getLogger(SocketController.class.getName()).log(Level.SEVERE, null, ex);
    }
}

private static void copy(final InputStream is, final OutputStream os, File f) throws         IOException {
    final byte[] stop = "stop".getBytes();
    final byte[] buffer = new byte[(int) f.length()];
    is.read(buffer, 0, buffer.length);
    os.write(buffer, 0, buffer.length);
    os.write(stop);
    os.flush();
}

クライアントコード

    public static File recieveData(Socket clientSocket, File f) {
    try {
        InputStream in = clientSocket.getInputStream();
        FileOutputStream output = new FileOutputStream(f);
        byte[] content;
        byte[] bytes = new byte[1024 << 8];
        int bytesRead;
        while (true) {
            if (recieveStop(f)) {
                removeStop(f);
                break;
            }
            bytesRead = in.read(bytes);
            output.write(bytes, 0, bytesRead);
        }
    } catch (IOException ex) {
        Logger.getLogger(Master_Slave_Sockets_Client.class.getName()).log(Level.SEVERE, null, ex);
    }
    return f;
}

public static boolean recieveStop(File f) {
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(f));
        String currentLine;
        String lastLine = "";
            while ((currentLine = br.readLine()) != null) {
                lastLine = currentLine;
            } 
        if (lastLine.equals("stop")) {
            return true;
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Master_Slave_Sockets_Client.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
            Logger.getLogger(Master_Slave_Sockets_Client.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
        try {
            br.close();
        } catch (IOException ex) {
            Logger.getLogger(Master_Slave_Sockets_Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return false;
}

public static void removeStop(File f) {
    try {
        RandomAccessFile raFile = new RandomAccessFile(f, "rw");
        long length = raFile.length();
        raFile.setLength(length - 4);
        raFile.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Master_Slave_Sockets_Client.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Master_Slave_Sockets_Client.class.getName()).log(Level.SEVERE, null, ex);
    }

}

これが同じ問題を抱えている他の人に役立つことを願っています。

于 2013-10-30T06:39:46.377 に答える