【发布时间】:2013-12-26 08:54:51
【问题描述】:
我在这里通过套接字处理客户端-服务器文件传输应用程序。在这里,我的问题是我的客户端抛出异常,说在服务器端仍然打开时套接字已关闭。它仍然提示要发送下一个文件名。我没有关闭套接字或其他任何东西。
代码如下:
服务器端:
public void soc_server() throws IOException {
servsock = new ServerSocket(55000);
sock = servsock.accept();
pw = new PrintWriter(sock.getOutputStream(), true);
System.out.println("Hello Server");
Scanner sc = new Scanner(System.in);
System.out.println("how many files to be sent: ");
String temp = sc.nextLine();
temp = temp.trim();
int fileNumber = Integer.parseInt(temp);
// sc.close();
// sc = new Scanner(System.in);
for (int x = 0; x < fileNumber; x++) {
System.out.println("Please enter the file name or file path ");
String s = sc.nextLine();
File file = new File(s);
if (file.exists())
System.out.println("File found");
else
System.out.println("File not found");
out = sock.getOutputStream();
fileInputStream = new FileInputStream(s);
byte[] buffer = new byte[100 * 1024];
int bytesRead = 0;
System.out.println("sending file no: " + x);
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
if (bytesRead > 0) {
out.write(buffer, 0, bytesRead);
totalSent += bytesRead;
}
}
System.out.println("sent " + (totalSent / 1024) + " KB "
+ ((System.currentTimeMillis() - time) / 1000) + " sec");
out.flush();
pw.print(s);
pw.flush();
}
fileInputStream.close();
out.close();
pw.close();
System.out.println("Sent " + (totalSent / 1024) + " kilobytes in "
+ ((System.currentTimeMillis() - time) / 1000) + " seconds");
sock.close();
servsock.close();
sc.close();
}
客户端代码:
public void soc_client() throws Exception {
sock = new Socket("172.16.27.106", 55000);
System.out.println("Hello Client");
while (sock.isConnected() == true) {
in = sock.getInputStream();
File outputFile = new File("outputFile");
if (outputFile.exists())
System.out.println("File found");
else {
System.out.println("File not found");
outputFile.createNewFile();
}
fileOutputStream = new FileOutputStream(outputFile);
byte[] buffer = new byte[100 * 1024];
int bytesRead = 0;
while ((bytesRead = in.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
totalRecieved += bytesRead;
}
System.out
.println("Recieved " + (totalRecieved / 1024)
+ " kilobytes in "
+ ((System.currentTimeMillis() - time) / 1000)
+ " seconds");
fileOutputStream.flush();
fileOutputStream.close();
in = sock.getInputStream();
br = new BufferedReader(new InputStreamReader(in));
String fileName = br.readLine();
fileName = fileName + "";
System.out.println(fileName);
File file = new File(fileName);
file.createNewFile();
boolean success = outputFile.renameTo(file);
if (!success) {
System.out.println(" file renaming is unsuccessful ");
} else {
outputFile.delete();
}
in.close();
br.close();
}
if (sock.isConnected() == true) {
System.out.println(" connection stays ");
} else {
System.out.println(" connection terminated ");
}
System.out
.println("Recieved "
+ (totalRecieved
/ (1024 * 1024)
+ " MB in "
+ ((System.currentTimeMillis() - time) / (1000 * 60)) + " minutes"));
}
客户端异常:
Press 1 to be server
Press 2 to be client
Press 3 to be emergency exit
2
Hello Client
File not found
Recieved 42 kilobytes in 12.148 secondsException in thread "main"
null
java.net.SocketException: Socket is closed
at java.net.Socket.getInputStream(Unknown Source)
at Client.soc_client(Client.java:28)
at Index.main(Index.java:25)
服务器端提示:
asus@CM1855:~/Desktop$ java Index
Press 1 to be server
Press 2 to be client
Press 3 to be emergency exit
1
Hello Server
how many files to be sent:
2
Please enter the file name or file path
Bill of cloud draft.docx
File found
sending file no: 0
sent 36 KB 20 sec
Please enter the file name or file path
【问题讨论】:
-
我怀疑你可以像你那样在同一个套接字上多次调用
getInputStream。 -
@Heuster 我建议您尝试一下,而不是在这里发布您的猜测。
-
@EJP 哈哈谢谢你的提示。下次我会三思而后行。有像你这样的人使这个网站保持完美,这很好。我希望我能像你一样多一点。
-
@Heuster “更像我”与此无关。你只需要接受你在科学学科中,证据和实验规则适用。