Home > Back-end >  Zip file through the socket network transmission, but transfer files will appear damage in the past.
Zip file through the socket network transmission, but transfer files will appear damage in the past.

Time:11-29

Problems: I defined a byte array data: byte [], used for temporary read bytes of data, when the size of the byte array is set to 8192 transfer files no problem, should be 8192 bytes BufferedInputStream and BufferedOutputStream the default cache size, when I let the size of the byte array is defined as the 8192 transfer files will appear when the number of the file is damaged, use BufferedInputStream (socket. GetInputStream (), databig); And BufferedOutputStream (socket. GetOutputStream databig); This two constructors should have put the default cache size changed, but the transfer is still in the file is damaged, here I don't know where is wrong, because if the cache size and I define the byte array size doesn't fit, that after I change the default cache size should be able to normal transport, but is not the case, request to explain the great god, thank you,


The receiver code:
Import the Java. IO. BufferedInputStream;
Import the Java. IO. A DataInputStream;
Import the Java. IO. The File;
Import the Java. IO. FileOutputStream;
The import java.net.ServerSocket;
The import java.net.Socket;

Public class Receive {
Final static String name="E: \ \ 2. Zip";//receiving storage location file
Final static int port=9999;
Final static int databig=8192;

Public static void main (String [] args) {
The File File=new File (name);
Byte [] data=https://bbs.csdn.net/topics/new byte [databig];
Long big=0;
Long count=0;

Try {
ServerSocket=new ServerSocket server (port);
System. The out. Println (" waiting for the sending end connection ");
The Socket Socket=server. The accept ();
System. The out. Println (" connection success ");
file.createNewFile();
FileOutputStream out=new FileOutputStream (file);
BufferedInputStream in=new BufferedInputStream (socket. GetInputStream (), databig);
A DataInputStream in2=new a DataInputStream (socket. GetInputStream ());
//receive file size
Big=in2. ReadLong ();
System. The out. Println (big);

While (true)
{
In. Read (data, 0, the data length);
Out. Write (data, 0, the data length);
System. The out. Println ((count/big * * 1.0 1.0) * 100);
Count +=databig;
}
}
The catch (Exception e) {
System. The out. Println (e. oString ());
}
System. The out. Println (" receive success ");
}
}

The sender code:
Import the Java. IO. BufferedOutputStream;
Import the Java. IO. DataOutputStream;
Import the Java. IO. The File;
Import the Java. IO. FileInputStream;
The import java.net.Socket;

Public class Send {
Final static String name="E: \ \ 2. Zip";
Final static String IP="19.168.43.01";
Final static int port=9999;
Final static int filebig=8192;

Public static void main (String [] args) {
The File File=new File (name);
Long big=file. The length ();
System. The out. Printf (" total file size (Byte) : ");
System. The out. Println (big);
Byte [] data=https://bbs.csdn.net/topics/new byte [filebig];
Long count=0;

Try {
The Socket Socket=new Socket (IP, port);
FileInputStream in=new FileInputStream (file);
BufferedOutputStream out=new BufferedOutputStream (socket. GetOutputStream (), filebig);
DataOutputStream out2=new DataOutputStream (socket. GetOutputStream ());
//send a file size
Out2. WriteLong (big);
Out2. Flush ();

While (in the read (data, 0, the data length).=1)
{
System. The out. Printf (" send a file percentage: ");
System. The out. Println ((count/big * * 1.0 1.0) * 100);
Out. Write (data, 0, the data length);
Out. The flush ();
Count +=filebig;
}

System. The out. Println (" send success ");
}
The catch (Exception e) {
e.printStackTrace();
}
}
}

CodePudding user response:

Because not receive complete package data, process has been disconnected, you look at the damage of the bales is less than before

CodePudding user response:

reference 1st floor Farmermark993 response:
due to packet data without receiving is completed, the process has been disconnected, you look at the damage bag is less than the previous bag

Hello, but why would a byte array is set to 8192 bytes can receive

CodePudding user response:

Transfer protocol definition is bad, the binary transmission without control characters (e.g., \ r \ n EOF control), must use length control,

You don't have a defined in the protocol transmission length, and the client shall, in accordance with the 8192 - byte read, there is a problem, of course,

Your transfer protocol, at least to transmit a protocol header, the simplest length can be 4 bytes of file, and convention, transmission of at least 64 bytes (this can change), insufficient data using 0,

Every time your sender to send at least 68 bytes (4 byte header, only the length of the file, 64 bytes minimum data), the receiving end, first read 68 bytes, length of analysis, if more than 68 bytes, continued to read, insufficient ends,

CodePudding user response:

reference tianfang reply: 3/f
transport protocol definition is not good, no binary transmission control characters (e.g., \ r \ n EOF control), must use length control,

You don't have a defined in the protocol transmission length, and the client shall, in accordance with the 8192 - byte read, there is a problem, of course,

Your transfer protocol, at least to transmit a protocol header, the simplest length can be 4 bytes of file, and convention, transmission of at least 64 bytes (this can change), insufficient data using 0,

Your each time the sender to send at least 68 bytes (4 byte header, only the length of the file, 64 bytes minimum data), the receiving end, first read 68 bytes, length of analysis, if more than 68 bytes, continued to read, insufficient ends,

Thank you for your help, I found the reason, because the last time can't read the file data fill byte array, lead to all zero padding, didn't check when written to the file, and then the content of the new file the back will be a heap of zero, cause the file is damaged,

CodePudding user response:

Oneself define a standard, fragmentation, and add a best end mark, such as 030201;
It received symbol represents the,
Verify the length of the right by the way, how to error or timeout is disconnected, or a particular section is not received,
  • Related