【发布时间】:2011-10-17 14:53:05
【问题描述】:
我正在尝试编写一个基本的客户端-服务器程序,以允许客户端遍历服务器的文件。我希望服务器在连接客户端时将当前目录中所有文件的列表发送给客户端。我该怎么做呢?我已经创建了一个包含所有文件名的数组,并试图将它们发送给客户端,它只是处于无限循环中(因为它意味着!)并且什么都不做。
尝试让服务器在客户端连接时发送消息:
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
boolean found = false;
//Read the data from the input stream
for (int i=0;i < fileList.length;i++) {
outToClient.writeBytes(fileList[i].getName());
}
让服务器接收到这个:
//Prepare an object for reading from the input stream
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//Read the data from the input stream
sentence = inFromServer.readLine();
但这样做只会让客户端陷入无限循环,什么也不做。
请帮忙?
【问题讨论】:
标签: java tcp client-server