gpt4 book ai didi

java - 通过Java套接字发送多个文件

转载 作者:行者123 更新时间:2023-12-03 11:58:42 25 4
gpt4 key购买 nike

我试图让一个程序正常工作,在该程序中客户端将同一文件发送到服务器100次,然后检查以确保所有100个文件与原始文件相同。现在,我无法使它正常工作,因为服务器从客户端读取了任意数量的字节,这导致某些文件中的字节过多,我不确定如何继续。现在,它将正确发送所有100个文件,但其中一些文件中包含太多内容。
客户代码

String sentence;
InetAddress host = InetAddress.getLocalHost();
Socket clientSocket = new Socket(host, 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

long startTime, endTime, tGap, totalTime;
byte[] sendData;
int filesSent = 0;

File fs = new File("Test1.txt");

int fileLength = (int) fs.length();
totalTime = 0;

while (filesSent < 100) {
FileInputStream fin = new FileInputStream(fs);
int numBytes = 0;
System.out.println("Sending file to server...");
sendData = new byte[1024];
if (fileLength < 1024) {
numBytes = fin.read(sendData, 0, fileLength);
} else {
numBytes = fin.read(sendData, 0, 1024);
}

startTime = System.currentTimeMillis();
int count = 0;
while (numBytes != -1) {
count += numBytes;
System.out.println(numBytes);
sentence = new String(sendData);
outToServer.writeBytes(sentence);
numBytes = fin.read(sendData, 0, 1024);
}
System.out.println(count);
endTime = System.currentTimeMillis();
tGap = endTime - startTime;
totalTime = totalTime + tGap;
System.out.println("Finished run " + filesSent + " with a time of " + tGap);
filesSent++;
fin.close();
outToServer.flush();
}
clientSocket.close();

System.out.println("I am done to send " + filesSent + " times file, and the average time is: " + (double) totalTime / filesSent);
}
服务器代码
String clientSentence;
int filesRecieved = 0;
ServerSocket welcomeSocket = new ServerSocket(6789);
int numError = 0;
int numBytes;
char check = 'a';
System.out.println("I am starting now....");

long startTime, endTime, tGap, totalTime;
totalTime = 0;

Socket connectionSocket;
connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));

while(filesRecieved < 100) {
FileOutputStream outPut = new FileOutputStream("receivedFile" + filesRecieved + ".txt");
char[] receiveData = new char[1024];
numBytes = inFromClient.read(receiveData, 0, 1024);
while(numBytes != 1024){
numBytes += inFromClient.read(receiveData,0,1024-numBytes);
}
System.out.println("OG Bytes:" + numBytes);
startTime = System.currentTimeMillis();

int count = 0;
while (numBytes != -1 && count < 105942) {
try {
count += numBytes;
clientSentence = new String(receiveData, 0, numBytes);
outPut.write(clientSentence.getBytes(), 0, numBytes);
System.out.println(numBytes);
numBytes = inFromClient.read(receiveData, 0, 1024);
System.out.println(count);
}catch(Exception e){
break;
}
}
outPut.close();
endTime = System.currentTimeMillis();
tGap = endTime - startTime;
totalTime = totalTime + tGap;
System.out.println("I have received" + "receivedFile" + filesRecieved + ".txt using time = " + tGap);
filesRecieved++;
}

connectionSocket.close();

System.out.println("I am done now..." + "and the average time used to receive each copy is: " + totalTime / filesRecieved);
welcomeSocket.close();

最佳答案

TCP是字节流。它没有消息的概念。因此,您需要以一种这样的方式对文件数据进行构图,以使接收方知道一个文件在哪里结束而下一个文件在哪里开始。在这种情况下,您需要先发送文件大小,然后再发送文件数据,这样接收方才能知道每个文件有多少字节。
另外,您没有适当注意每次调用read()告诉您实际上已读取的字节数。您为它提供了很大的缓冲区,但是不能保证整个缓冲区都会被填满。特别是在文件末尾。
并且不要使用字符串传输二进制文件数据!
尝试类似这样的方法:
客户:

File fs = new File("Test1.txt");
long fileLength = fs.length();

InetAddress host = InetAddress.getLocalHost();
Socket clientSocket = new Socket(host, 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

long startTime, endTime, tGap, totalTime = 0;
byte[] sendData = new byte[1024];
int numBytes, filesSent = 0;

while (filesSent < 100) {
FileInputStream fin = new FileInputStream(fs);
long thisFileLength = fileLength;

System.out.println("Sending file to server...");
startTime = System.currentTimeMillis();

outToServer.writeLong(thisFileLength);

while (thisFileLength > 0) {
if (thisFileLength < sendData.length) {
numBytes = fin.read(sendData, 0, (int) thisFileLength);
} else {
numBytes = fin.read(sendData, 0, sendData.length);
}

System.out.println(numBytes);
if (numBytes < 1) {
break;
}

outToServer.write(sendData, 0, numBytes);
thisFileLength -= numBytes;
}

outToServer.flush();

endTime = System.currentTimeMillis();
tGap = endTime - startTime;
totalTime = totalTime + tGap;
System.out.println("Finished run " + filesSent + " with a time of " + tGap);
filesSent++;

fin.close();

if (thisFileLength > 0) {
break;
}
}

clientSocket.close();

System.out.println("I am done to send " + filesSent + " times file, and the average time is: " + (double) totalTime / filesSent);
服务器:
ServerSocket welcomeSocket = new ServerSocket(6789);
System.out.println("I am starting now....");

Socket connectionSocket;
connectionSocket = welcomeSocket.accept();
DataInputStream inFromClient = new DataInputStream(new BufferedInputStream(connectionSocket.getInputStream()));

long startTime, endTime, tGap, totalTime = 0;
byte[] receiveData = new byte[1024];
int numBytes, filesRecieved = 0;

while (filesRecieved < 100) {
string filename = "receivedFile" + filesRecieved + ".txt";
FileOutputStream outPut = new FileOutputStream(filename);

startTime = System.currentTimeMillis();

long fileLength = inFromClient.readLong();
System.out.println("OG Bytes: " + fileLength);

while (fileLength > 0) {
if (fileLength < receiveData.length) {
numBytes = inFromClient.read(receiveData, 0, (int) fileLength);
}
else {
numBytes = inFromClient.read(receiveData, 0, receiveData.length);
}

if (numBytes < 1) {
break;
}

try {
outPut.write(receiveData, 0, numBytes);
System.out.println(numBytes);
}
catch (Exception e) {
break;
}

fileLength -= numBytes;
}

outPut.close();

endTime = System.currentTimeMillis();
tGap = endTime - startTime;
totalTime = totalTime + tGap;
System.out.println("I have received " + filename + " using time = " + tGap);
filesRecieved++;

if (fileLength > 0) {
break;
}
}

connectionSocket.close();

System.out.println("I am done now... and the average time used to receive each copy is: " + totalTime / filesRecieved);
welcomeSocket.close();

关于java - 通过Java套接字发送多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64127458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com