gpt4 book ai didi

Java,通过套接字发送大文件消耗太多CPU周期并且速度慢

转载 作者:行者123 更新时间:2023-12-02 04:34:51 24 4
gpt4 key购买 nike

我正在尝试JAVA,并在网上发现了这个问题。

Java sending and receiving file (byte[]) over sockets .

出于好奇,我使用了已接受答案中的代码,以及我发现与问题类似的其他代码。我尝试了接受的答案,是的,它有效并且速度非常快。但问题是存档文件已损坏。这是我尝试过的其他代码。我的实验代码的缺点是它消耗 CPU 周期并且比接受的答案花费更多的时间(而且我不知道为什么会这样)。这是我的代码。有人可以帮助我进一步优化和改进这段代码吗?

接受答案所花费的时间 = 4 Mb 文件 11 毫秒。我的实验花费的时间 = 相同文件 4 秒。

服务器.java

public class Server implements Runnable {

private ServerSocket serverSocket = null;
private Socket socket = null;
private ObjectInputStream inStream = null;

public Server() {

}

@Override
public void run() {

try {
serverSocket = new ServerSocket(4445);
socket = serverSocket.accept();
DataInputStream dIn = new DataInputStream(socket.getInputStream());
OutputStream os = socket.getOutputStream();
DataOutputStream outToClient = new DataOutputStream(os);

System.out.println("Connected");
File myFile = new File("lib1.zip");
long flength = myFile.length();
System.out.println("File Length"+flength);
outToClient.writeLong(flength);
FileInputStream fis;
BufferedInputStream bis;
byte[] mybytearray = new byte[8192];
fis = new FileInputStream(myFile);
bis = new BufferedInputStream(fis);
int theByte = 0;
System.out.println("Sending " + myFile.getAbsolutePath() + "(" + myFile.length() + " bytes)");
while ((theByte = bis.read()) != -1) {
outToClient.write(theByte);
// bos.flush();
}
/*int count;
BufferedOutputStream bos= new BufferedOutputStream(os);
while ((count = bis.read(mybytearray))>0) {
bos.write(mybytearray, 0, count);
}*/

bis.close();
socket.close();

} catch (SocketException se) {

System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
Thread t = new Thread(new Server());
t.start();
}
}

接收文件.java

 public class RecieveFile {

public final static int SOCKET_PORT = 4445; // you may change this
String SERVER = "127.0.0.1"; // localhost
ArrayList<String> logmsg = new ArrayList<>();
public static void main(String[] args) {
new RecieveFile();
}

public RecieveFile() {
try (Socket sock = new Socket(SERVER, SOCKET_PORT)) {

System.out.println("Connecting...");
try (OutputStream os = sock.getOutputStream(); DataOutputStream outToServer = new DataOutputStream(os)) {
try (DataInputStream dIn = new DataInputStream(sock.getInputStream())) {
long fileLen, downData;
int bufferSize = sock.getReceiveBufferSize();

long starttime = System.currentTimeMillis();
File myFIle = new File("lib1.zip");
try (FileOutputStream fos = new FileOutputStream(myFIle); BufferedOutputStream bos = new BufferedOutputStream(fos)) {
fileLen = dIn.readLong();
/*for (long j = 0; j <= fileLen; j++) {
int tempint = is.read();
bos.write(tempint);
}*/
downData = fileLen;
int n = 0;
byte[] buf = new byte[8192];
while (fileLen > 0 && ((n = dIn.read(buf, 0, buf.length)) != -1)) {
bos.write(buf, 0, n);
fileLen -= n;
// System.out.println("Remaining "+fileLen);
}
/*while ((n = dIn.read(buf)) > 0) {
bos.write(buf, 0, n);
}*/
bos.flush();
long endtime = System.currentTimeMillis();
System.out.println("File " + myFIle.getAbsolutePath()
+ " downloaded (" + downData + " bytes read) in " + (endtime - starttime) + " ms");

}
}
}
} catch (IOException ex) {
Logger.getLogger(RecieveFile.class.getName()).log(Level.SEVERE, null, ex);
}

}
}

最佳答案

您一次复制一个字节。这很慢。您还声明了一个字节数组但没有使用它。试试这个:

int count;
byte[] buffer = new byte[8192]; // or more, double or quadruple it

while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}

关于Java,通过套接字发送大文件消耗太多CPU周期并且速度慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30966894/

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