0

我正在制作一个客户端-服务器程序,它将文件分成数据包。当我发送文件时,当我打开它时它是空的。我的问题是那个空文件,我该如何更正代码才能工作。我想分离文件,如果其中一个数据包发生问题,则从另一台 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 回答