0

ファイルをパケットに分割するクライアントサーバープログラムを作成しています。ファイルを送信して開くと、ファイルは空です。私の問題は、その空のファイルです。コードを修正して機能させるにはどうすればよいですか。ファイルを分離したいのですが、パケットの 1 つに何か問題が発生した場合は、別の PC から送信する必要があります。Javaは苦手ですが、スキルアップに努めています。ここに両方の​​コードがあります:
サーバー部分:

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

public class PacketServer {
    public static void main(String[] args) throws IOException {
        // create socket
        ServerSocket servsock = new ServerSocket(29311);
        while (true) {
            System.out.println("Waiting...");

            Socket sock = servsock.accept();
            System.out.println("Accepted connection : " + sock);

            // sendfile
            File myFile = new File("D://test.txt");
            System.out.println(myFile.length());

            byte[] mybytearray = new byte[(int) myFile.length()];

            PrintWriter output = new PrintWriter(sock.getOutputStream(), true);
            output.println(myFile.length());

            FileInputStream fis = new FileInputStream(myFile);
            BufferedInputStream bis = new BufferedInputStream(fis);

            bis.read(mybytearray, 0, mybytearray.length);

            OutputStream os = sock.getOutputStream();
            System.out.println("Sending...");

            long now = System.currentTimeMillis();
            int obshto = (int) myFile.length();
            int b = obshto / 60;
            int c = obshto % 60;// ako file ne e kraen broi paketi ot 60 byte
            System.out.println(c);
            int packet;
            int gpacket;
            for (packet = 1; packet <= b; packet++) {

                System.out.println("izprashta packet:" + packet
                        + " s golemina " + 60 * packet + " bit / "
                        + myFile.length() + " w moment: " + now);
                os.write(mybytearray, 0, 60);
                if ((c != 0) && (b == packet)) {
                    gpacket = c;
                    System.out.println("izprashta packet:" + packet
                            + " s golemina " + (c) + " bit / "
                            + myFile.length() + " w moment: " + now);
                    os.write(mybytearray, 0, c);
                }
            }
            os.flush();
            sock.close();
        }
    }
}

および
クライアント部分:

import java.net.*;
import java.util.Scanner;
import java.io.*;

public class PacketClient {
    public static void main(String[] args) throws IOException {

        int filesize = 6022386;
        long start = System.currentTimeMillis();

        Socket sock = new Socket("127.0.0.1", 29311);
        System.out.println("Connecting...");
        Scanner input = new Scanner(sock.getInputStream());

        String response = input.nextLine();
        int Fsize = Integer.parseInt(response);

        // receive file
        byte[] mybytearray = new byte[filesize];
        InputStream is = sock.getInputStream();
        FileOutputStream fos = new FileOutputStream("D://Receive/test.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        // obshto packets kam momenta
        int obshto = Fsize;
        int b = obshto / 60;
        int c = obshto % 60;
        int bytesRead;
        int current;

        for (b = obshto / 60; b >= 1; b--) {

            bytesRead = is.read(mybytearray, (b * 60), 60);
            current = bytesRead;
            long now1 = System.currentTimeMillis();

            do {
                bytesRead = is.read(mybytearray, current,
                        (mybytearray.length - current));
                System.out.println("bytesRead " + bytesRead);

                if (bytesRead <= Fsize)
                    current += bytesRead;

                System.out.println("bytesRead " + bytesRead);
            } while (bytesRead > -1);
            System.out.println("poluchava packet:" + b + " s golemina" + 60 * b
                    + " bit / " + Fsize + " w moment: " + now1);

            bos.write(mybytearray, 0, 60);
            if ((c != 0) && (b == 1)) {

                bytesRead = is.read(mybytearray, 0, c);
                current = bytesRead;
                long now = System.currentTimeMillis();

                do {
                    bytesRead = is.read(mybytearray, current,
                            (mybytearray.length - current));
                    // System.out.println("bytesRead " + bytesRead);

                    if (bytesRead <= Fsize)
                        current += bytesRead;

                    // System.out.println("bytesRead "+ bytesRead);
                } while (bytesRead > -1);
                System.out.println("poluchava packet:" + (b + 1)
                        + " s golemina" + c + " bit / " + Fsize + " w moment: "
                        + now);

                bos.write(mybytearray, 0, c);
            }

            bos.flush();

            long end = System.currentTimeMillis();
            System.out.println(end - start);

            bos.close();
            sock.close();
        }

    }
}
4

2 に答える 2

1

提案の共通点

  • 自分が何をしているのか本当にわかっていない限り、同じストリームにテキストとバイナリを混在させないでください。これは混乱を招くだけです。
  • Apache Commons IOUtils のような既存のライブラリを使用するか、それらを読んでどのように機能するかを確認してください。
  • 読みやすいようにコードをフォーマットします。
  • あなたのコードは必要以上に複雑で、その理由は明らかではありません。何60のためにあるのですか?

?私たちがそれが何であるかを理解するために、a で質問を明確にしていただけますか。

于 2012-06-24T17:47:54.280 に答える
0

この方法を試してください。データがトランスポート OSI 層 (TCP/IP プロトコル) によって処理されている間は、データをパケットに分割する必要はありません。

サーバ:

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

public class FileServer {
    public static void main(String[] args) throws IOException {
        // Create socket
        ServerSocket servsock = new ServerSocket(13267);
        while (true) {
            System.out.println("Waiting...");

            Socket sock = servsock.accept();
            System.out.println("Accepted connection : " + sock);

            // Send file
            File myFile = new File("source.pdf");
            byte[] mybytearray = new byte[(int) myFile.length()];
            FileInputStream fis = new FileInputStream(myFile);
            BufferedInputStream bis = new BufferedInputStream(fis);
            bis.read(mybytearray, 0, mybytearray.length);
            OutputStream os = sock.getOutputStream();
            System.out.println("Sending...");
            os.write(mybytearray, 0, mybytearray.length);
            os.flush();
            sock.close();
        }
    }
}

クライアント:

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

public class FileClient {
    public static void main(String[] args) throws IOException {
        int filesize = 6022386; // filesize temporary hardcoded

        long start = System.currentTimeMillis();
        int bytesRead;
        int current = 0;
        // Localhost
        Socket sock = new Socket("127.0.0.1", 13267);
        System.out.println("Connecting...");

        // Receive file
        byte[] mybytearray = new byte[filesize];
        InputStream is = sock.getInputStream();
        FileOutputStream fos = new FileOutputStream("source-copy.pdf");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bytesRead = is.read(mybytearray, 0, mybytearray.length);
        current = bytesRead;

        do {
            bytesRead = is.read(mybytearray, current,
                    (mybytearray.length - current));
            if (bytesRead >= 0)
                current += bytesRead;
        } while (bytesRead > -1);   

        bos.write(mybytearray, 0, current);
        bos.flush();
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        bos.close();
        sock.close();
    }
}
于 2012-06-24T17:51:49.667 に答える