【发布时间】:2012-11-15 12:12:33
【问题描述】:
我在客户端服务器图像传输中遇到了奇怪的行为。当前服务器总是在从缓冲区中读取 32768 字节时停止,总文件大小为 36556 字节。 while 循环没有结束,但它不会打印其中的任何 print 语句,也没有抛出异常。我尝试了几种不同大小的字节缓冲区,甚至大于图像大小也不能解决问题。
客户代码:
private static void writePhoto(Socket socket) throws IOException {
OutputStream outputStream = new BufferedOutputStream(socket.getOutputStream());
String file_path = "E:\\Eclipse\\Workspace\\DNI_PsuedoClient\\" +
"src\\main\\resources\\BuckyBadger.jpg";
try {
InputStream stream = new FileInputStream(file_path);
System.out.println(stream.available());
try {
byte[] buffer = new byte[1024];
int readData;
int i = 1;
while((readData=stream.read(buffer))!=-1){
System.out.println(readData + " " + i);
outputStream.write(buffer,0,readData);
i++;
}
System.out.println("done writing to buffer");
}catch (IOException e) {
System.err.println("File Error");
e.printStackTrace();
System.exit(-1);
}finally {
stream.close();
}
} finally {
}
}
服务器代码
private static java.io.File createFile(Socket client) throws IOException {
System.out.println("Starting to create file");
InputStream stream = new BufferedInputStream(client.getInputStream());
System.out.println("1");
// Create file from the inputStream
java.io.File Recieved_File = new java.io.File(thread_ID + ".jpg");
System.out.println(thread_ID + ".jpg");
System.out.println("2");
try {
OutputStream outputStream = new FileOutputStream(Recieved_File);
System.out.println("3");
try {
byte[] buffer = new byte[1024];
System.out.println("4");
int readData;
int i = 1;
while ((readData = stream.read(buffer)) != -1) {
System.out.println("start");
outputStream.write(buffer, 0, readData);
System.out.println(readData + " " + i + " " + (i * readData));
i++;
System.out.println("end while loop");
}
} finally {
System.out.println("5");
outputStream.close();
System.out.println("6");
}
} finally {
}
System.out.println("Created file");
return Recieved_File;
}
如您所见,我曾多次尝试在服务器代码中使用打印语句来找出问题所在。任何见解都会非常有帮助。
为了进一步参考,我有完全相同的服务器代码用于与 Windows 手机的交互,手机拍摄照片并将其上传到服务器。我现在正在更改代码以在多个线程上运行,并首先使用模拟交互的 Java 客户端在 Java 中对其进行测试。 java 客户端正在尝试将存储在驱动器上的照片发送到服务器。客户端似乎工作正常,因为它将整张照片放入缓冲区。
我写了一个协议,这里是:
public DNI_Protocol(Socket socket, String threadID) {
client = socket;
thread_ID = threadID;
}
public String processInput(String theInput) {
String theOutput = null;
if (state == WAITING) {
System.out.println(theInput);
if (theInput.equals("Upload")) {
state = UPLOAD_PHOTO;
theOutput = "Send Photo";
} else if (theInput == "View") {
state = VIEW;
theOutput = "Send Request";
} else {
theOutput = "Waiting";
}
} else if (state == UPLOAD_PHOTO) {
// if (theInput != "Sending Photo") {
// TODO: Throw a state exception with a message
// return "exit";
// }
setupDrive();
try {
Image_Handle = createFile(client);
System.out.println("Uploading file");
uploadedFile = Drive_Interface.uploadFile(false, Image_Handle, drive);
System.out.println("file Uploaded");
google_ID = uploadedFile.getId();
System.out.println(google_ID);
Image_Handle.delete(); // We are done with the file so we can delete it
System.out.println("deleted file");
} catch (IOException e) {
e.printStackTrace();
}
theOutput = "Send Keywords";
state = UPLOAD_KEYWORDS;
} else if (state == UPLOAD_KEYWORDS) {
if (theInput != "Sending Keywords") {
// TODO: Throw a state exception with a message
return "exit";
}
// TODO: Add code to get keyword arraylist and upload information to
// the database
theOutput = "exit";
}
return theOutput;
}
【问题讨论】:
-
您期望发生什么?您的代码无法让客户端阻止服务器继续等待更多数据。所以服务器一直在等待。 (当您设计用于在这两个程序之间进行通信的协议时,您应该回答的两个问题是“客户端如何告诉服务器它拥有整个文件?”和“服务器如何知道它拥有整个文件?”文件?”)也许您的意思是关闭客户端中的连接?
-
@GagandeepBali 没有显式缓冲,所以唯一的缓冲是 Nagle 算法,它不会引入长延迟。
-
您说客户端将整个文件放入缓冲区。它需要做的下一件事是与以某种方式完成该操作的服务器进行通信。看起来您打算通过关闭套接字来执行此操作。但是,您无法知道服务器是否已读取整个文件。客户端可能会在服务器可能仍在读取数据时尝试关闭连接的想法? (你真的设计了一个协议吗?因为你应该在编写任何代码之前解决这些问题。)
-
(i * readData)总共是错误的。您需要保持字节总数,因为每次读取可能会返回不同的数量。 -
您的协议没有解释服务器如何知道它何时收到了整个文件。您的代码无法让服务器知道它何时收到了整个文件。不知何故,客户端必须通过发送文件大小或关闭连接来通知服务器何时收到整个文件。 (或者通过协商单向关闭 TCP 连接然后关闭它,这是 HTTP 的第一个版本所做的。)