gpt4 book ai didi

java - 为什么通过套接字传输数据后,文件中的数据丢失了?

转载 作者:行者123 更新时间:2023-12-01 11:13:48 25 4
gpt4 key购买 nike

当尝试将文件上传到服务器或从服务器下载文件时,我为服务器和客户端编写了相同的代码。

从服务器下载工作正常,我的文件中没有丢失任何数据,但由于某种原因,上传文件时,并未传输所有数据。

例如,我客户端上的文件大小比服务器上的文件大小小。然后,当它在服务器上打开时,并非全部都在那里(因为并非全部都已收到)

服务器:

算法:

  • 从客户端获取消息
  • 客户端告诉服务器它想要发送文件(推送)

服务器读取文件存放位置,然后从客户端接收文件

  public static void GetClientMessage() {

while (true) {
try {

try {

try {
serverSocket = new ServerSocket(PORT_NUMBER);
} catch (IOException ex) {
System.out.println("GetClientMessage():serverSocket:IOException:ex " + ex);
SendBackException(ex.toString()); // Inform client
}
try {
System.out.println("Waiting for client");
socket = serverSocket.accept();
} catch (IOException ex) {
System.out.println("GetClientMessage():socket = serverSocket.accept():IOException:ex " + ex);
SendBackException(ex.toString()); // Inform client
}

bufOut = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
brffReadIn = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));

// 1 - Read Line (it is the flag)
flag = brffReadIn.readLine();
// 2 - Handle Flag
HandleClientMessage(flag);

// Make decisions based upon that message
} catch (IOException ex) {
System.out.println("GetClientMessage():IOException:ex: " + ex);
SendBackException(ex.toString()); // Inform client
}

socket.close();
serverSocket.close();

} // Close while loop
catch (IOException ex) {
System.out.println("GetClientMessage:serverSocket.close():IOException:ex " + ex);
}

}

}

public static void HandleClientMessage(String message) {
System.out.println("HandleClientMessage:message: '" + message + "'");
switch (message) {
case "push":
GetClientFile();
break;
case "open_cla":
OpenCla();
break;
case "kill_cla":
KillCla();
break;
case "get":
SendFile();
break;
default:
break;
}
}

// Gets path to where to place file on local
public static String GetPath() {

String filePath = " ";

try {

bufOut = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
brffReadIn = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));

filePath = brffReadIn.readLine();

System.out.println("Path to place file on local: " + filePath);

} catch (IOException ex) {
System.out.println(("GetPath():IOException:ex: " + ex));
}
return filePath;
}

public static void GetClientFile() {

// Get the location where to place the file on local
fileOnLocal = GetPath();

int count;
try {

File file = new File(fileOnLocal);
// Get the size of the file
long length = file.length();
byte[] bytes = new byte[16* 1024];
InputStream in = socket.getInputStream();
OutputStream out = new FileOutputStream(fileOnLocal);

while ((count = in.read(bytes)) > 0) {
System.out.println("strByteArray: " + strByteArray);
out.write(bytes, 0, count);
}
out.flush();

System.out.println("File Size in bytes: " + file.length());
if (file.length() < 5) {
System.out.println("FileClient:Error:File:" + fileOnLocal + " not found on server");
out.close();
in.close();
socket.close();
file.delete();
System.out.println("File:" + file.getAbsolutePath() + " deleted");
} else {
out.close();
in.close();
socket.close();
}

} catch (IOException ex) {
System.out.println(":FileClient:GetServerFile():IOException:ex:" + ex);
}
}

客户端代码:

客户端告诉服务器它想要“推送”文件,然后传递文件在服务器上的位置,然后传输文件

public void SendFlagToServer(String flag){

try {

bufOut = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
brffReadIn = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));

bufOut.write(flag);
bufOut.newLine();
bufOut.flush();

System.out.println(host + ":SendFlagToServer: " + flag);

} catch (IOException ex) {
Logger.Log((host + ":FileClient:SendFileToGetToServer():IOException:ex: " + ex));
}
}

执行此操作后,客户端会收到字节,但不是全部字节。我编码有错误吗?我的 byte[] 数组应该有不同的大小吗?这将在 Win7 和 Win8 上使用,将来可能还会在 Mac 上使用。

编辑:我明白了。我试图太快地发送一条消息,然后发送一串字节。

这解决了我的问题:

SendFlagToServer(fileLocaitonOnServer);
Thread.sleep(1000);
....

最佳答案

您将在第一个客户端连接后关闭套接字

socket.close();
serverSocket.close();

解决方案:

  • Once you accept a client socket, create a new Thread with the socket connection and handle all IO operations in that thread

  • Do not close serverSocket. Once you close serverSocket, no more client socket connections will be accepted.

您能提供您遇到的异常(exception)情况吗?

关于java - 为什么通过套接字传输数据后,文件中的数据丢失了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32080381/

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