Java 当我发送以数据包分隔的文件时,新创建的文件是空的

Java 当我发送以数据包分隔的文件时,新创建的文件是空的,java,Java,我正在制作一个客户机-服务器程序,它将文件分成数据包。当我发送一个文件,当我打开它时,它是空的。我的问题是,空文件,我如何才能纠正代码,工作。我想分离文件,如果其中一个数据包出现问题,就从另一台PC发送。我不擅长java,但我正在努力提高我的技能。以下是两个代码: 服务器部分: import java.net.*; import java.io.*; public class PacketServer { public static void main(String[] args) t

我正在制作一个客户机-服务器程序,它将文件分成数据包。当我发送一个文件,当我打开它时,它是空的。我的问题是,空文件,我如何才能纠正代码,工作。我想分离文件,如果其中一个数据包出现问题,就从另一台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();
        }

    }
}
import java.net.*;
导入java.io.*;
公共类打包服务器{
公共静态void main(字符串[]args)引发IOException{
//创建套接字
ServerSocket servsock=新的ServerSocket(29311);
while(true){
System.out.println(“等待…”);
Socket sock=servsock.accept();
System.out.println(“接受的连接:+sock”);
//发送文件
File myFile=新文件(“D://test.txt”);
System.out.println(myFile.length());
字节[]mybytearray=新字节[(int)myFile.length()];
PrintWriter输出=新的PrintWriter(sock.getOutputStream(),true);
output.println(myFile.length());
FileInputStream fis=新的FileInputStream(myFile);
BufferedInputStream bis=新的BufferedInputStream(fis);
读取(mybytearray,0,mybytearray.length);
OutputStream os=sock.getOutputStream();
System.out.println(“发送…”);
long now=System.currentTimeMillis();
int obshto=(int)myFile.length();
int b=obshto/60;
int c=obshto%60;//ako文件ne e kraen broi paketi ot 60字节
系统输出打印ln(c);
int包;
int-gpacket;
对于(数据包=1;数据包=1;b--){
bytesRead=is.read(mybytearray,(b*60),60);
当前=字节读取;
long now1=System.currentTimeMillis();
做{
ByteRead=is.read(mybytearray,当前,
(mybytearray.length-电流);
System.out.println(“字节读取”+字节读取);
if(字节读取-1);
System.out.println(“poluchava数据包:+b+“s golemina”+60*b
+“位/”+Fsize+“w力矩:”+now1);
bos.write(mybytearray,0,60);
如果((c!=0)和&(b==1)){
bytesRead=is.read(mybytearray,0,c);
当前=字节读取;
long now=System.currentTimeMillis();
做{
ByteRead=is.read(mybytearray,当前,
(mybytearray.length-电流);
//System.out.println(“字节读取”+字节读取);
if(字节读取-1);
System.out.println(“poluchava数据包:”+(b+1)
+“s golemina”+c+“位/”+Fsize+“w力矩:”
+现在);
bos.write(mybytearray,0,c);
}
bos.flush();
long end=System.currentTimeMillis();
System.out.println(结束-开始);
bos.close();
sock.close();
}
}
}

常见的建议

  • 除非你真的知道你在做什么,否则不要在同一个流上混合文本和二进制。这只会导致混乱
  • 如果可以的话,可以使用现有的库,比如ApacheCommonsIOUTILS,或者阅读它们,看看它们是如何工作的
  • 格式化代码,使其可读
  • 您的代码比需要的复杂得多,原因也不清楚。
    60
    是做什么用的

你能用
把问题说清楚吗?
这样我们就知道它是什么了。

试试这种方法。当数据由传输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();
    }
}

整个数据包都没什么意义。底层TCP/IP协议栈将发送给流的字节,因为它希望将60个字节分组分组在一起,或者在中间剪切分组。只需发送需要发送的所有内容,并读取所有需要读取的无需数据包处理的内容。你做了什么?你能成功地读取一个文件吗?你能成功地将任何字符串发送到目标机器吗?你能成功地将任何字符串保存到一个文件吗?如果我不能将一个文件分割成真实的数据包。我能用什么命令来查看每个发送/接收的数据包?使用像Ethereal这样的网络嗅探器:这个程序是什么ike Wireshark?我想查看每个数据包的行为,这就是我将文件分为数据包的原因。你说的“pocket的行为”是什么意思?数据包何时发送/接收并丢弃。我想用这些信息生成跟踪输出文件。为什么忽略InputStream.read()的结果当你读一个文件的时候。它永远不应该被忽略。我不能接受你的想法,我已经告诉过我我是Java网络的新手。你能解释一下你的话吗?对不起,我正在尝试监视数据包,我用一个文件发送/接收数据包。你不能用纯Java监视数据包。你所知道的只是当操作系统返回一些东西给Java时。提示是TU的最大传输单位约为1500字节,如果你看到的数据超过1500字节,你就会看到合并的数据。你永远看不到TCP中丢弃的数据包,事实上你永远无法确定一个数据包从何处开始,另一个数据包从何处结束。仅仅因为你一次发送60字节并不意味着这就是有线电视上发生的情况。操作系统可能会阻止ine这是低效的,将写操作合并到高达1500字节的数据包中。Nagle的算法只是使用的一种策略。因此,我看到的这些时间不是真实的,但它们与数据包的真实时间有共同点吗?不太可能。如果你想准确地计算数据包的时间,你需要a)尽可能多地读取数据包