我在服务器端有这个代码:
服务器端:
ServerSocket listenTransferSocket = new ServerSocket(6000);
Socket connectionTransferSocket = listenTransferSocket.accept();
DataOutputStream outTransferToClient =
new DataOutputStream(connectionTransferSocket.getOutputStream());
{
....................... (Some code)
.......................
}
outTransferToClient.write(fileInBytes,0,numOfBytes);
System.out.println("File send");
**// outTransferToClient.close();**
BufferedReader inFromClientR =
new BufferedReader(new InputStreamReader(connectionTransferSocket.getInputStream()));
客户端:
Socket fileTransferSocket = new Socket("localhost",6000);
DataInputStream in = new DataInputStream(new BufferedInputStream(
fileTransferSocket.getInputStream()));
OutputStream out = new FileOutputStream(new File("./TransferedFiles/"+fileName));
byte[] by = new byte[numOfBytes];
while ((read = in.read(by, 0, numOfBytes)) != -1) {
out.write(by,0,read);
}
DataOutputStream outToServerR =
new DataOutputStream(fileTransferSocket.getOutputStream());
System.out.println("checkC");
outToServerR.writeBytes("Transfer completed \n");
如果我关闭它,当我尝试打开 BufferedReader 时出现以下异常:outTransferToClient.close();
Exception in thread "main" java.net.SocketException: Socket is closed
at java.net.Socket.getInputStream(Socket.java:788)
at Server.main(Server.java:92)
如果我没有客户端的while循环永远不会停止..有什么帮助吗????