[英]Java DataInputStream hung with no errors
I'm writing a client and server app for file transfer. 我正在编写用于文件传输的客户端和服务器应用程序。 In the download functionality the client seems to be getting hung at a call to the readUTF function call.
在下载功能中,客户端似乎挂在对readUTF函数调用的调用上。 I've attached the code snippet below.
我在下面附上了代码片段。 I get no errors or any other warnings.
我没有错误或任何其他警告。 If anyone has any insight I'd be greatly appreciative.
如果有人有任何见识,我将不胜感激。
Client: 客户:
os = new PrintStream(socket.getOutputStream());
os.println(fileName);
System.out.println("sending file name to server");
// wait for server to confirm file exists on server
String codeTest = input.readLine();
int code = Integer.parseInt(codeTest);
System.out.println("Code received from server: " + code);
while (code == 0) {
System.out.println("File: " + fileName + " does not exist on server!");
System.out.print("Please enter the file name you would like to download: ");
fileName = sc.next();
os.println(fileName);
codeTest = input.readLine();
code = Integer.parseInt(codeTest);
}
if (code == 1) {
InputStream inputStream = socket.getInputStream();
DataInputStream dataInStream = new DataInputStream(inputStream);
// download
System.out.println("File exists on the server!");
String fileToSave = "";
// get OS downloads directory
String home = System.getProperty("user.home");
String operatingSystem = System.getProperty("os.name");
if ( operatingSystem.startsWith("Windows") ) {
fileToSave = home + "\\" + "Downloads\\";
} else {
System.out.println(home);
fileToSave = home + "/" + "Downloads/";
}
System.out.println(fileToSave + fileName);
fileToSave += fileName;
System.out.println("file to download: " + fileToSave);
int bytesRead;
System.out.println("got streams");
fileName = dataInStream.readUTF();
System.out.println("fileName from server: " + fileName);
fileToSave += fileName;
System.out.println("File to save: " + fileToSave);
OutputStream output = new FileOutputStream((fileToSave));
long size = dataInStream.readLong();
byte[] buffer = new byte[1024];
while (size > 0 && (bytesRead = dataInStream.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
output.write(buffer, 0, bytesRead);
size -= bytesRead;
}
System.out.println("file downloaded successfully");
continue;
}
Server 服务器
System.out.println("user selected upload");
DataInputStream clientData = new DataInputStream(socket.getInputStream());
//String fileName = input.readLine();
String fileName = clientData.readUTF();
if ( fileName.equals("q")) {
System.out.println("continue");
continue;
}
//System.out.println("fileName received: " + fileName);
String fileNameToStore = DIRECTORY + fileName;
System.out.println("Saving file to: " + fileNameToStore);
OutputStream output = new FileOutputStream(fileNameToStore);
int bytesRead;
long size = clientData.readLong();
byte[] buffer = new byte[1024];
while( size>0 && (bytesRead = clientData.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
output.write(buffer, 0, bytesRead);
size -= bytesRead;
}
output.close();
//clientData.close();
System.out.println("done");
break;
case "d":
System.out.println("user selected download");
//handle file download
String requestedFile = input.readLine();
String fileToDownload = DIRECTORY + requestedFile;
System.out.println("User requested: " + fileToDownload);
// check if file exists and tell client
File file = new File(fileToDownload);
int code;
while ( !file.exists() ) {
code = 0;
textOut.println(code);
System.out.println("Code sent to client: " + code);
// wait for file name
requestedFile = input.readLine();
System.out.println("user resent filename: " + fileToDownload);
file = new File(fileToDownload);
}
// file exists - send to client
code = 1;
textOut.println(code);
byte[] mybytearray = new byte[(int)file.length()];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(file.getName());
System.out.println("dos writeUTF: " + file.getName());
dos.writeLong(mybytearray.length);
System.out.println(mybytearray.length + " sent to client.");
dos.write(mybytearray, 0, mybytearray.length);
dos.flush();
System.out.println("File "+fileToDownload+" sent to client.");
break;
I was able to get it working. 我能够使它工作。 I simply added an output from client to server letting the server know that the client was ready for the file to be sent.
我只是将客户端的输出添加到服务器,让服务器知道客户端已准备好发送文件。 I did this right before the fileName = dataInStream.readUTF();
我在fileName = dataInStream.readUTF();之前完成了此操作。
I'm not sure why that fixed it, but it did. 我不确定为什么要修复它,但是确实可以。 Thanks for the help.
谢谢您的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.