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