我正在尝试编写一个基本的客户端服务器程序,以允许客户端遍历服务器的文件。我希望服务器在连接客户端时将当前目录中所有文件的列表发送给客户端。我会如何去做这件事?我已经创建了一个包含所有文件名的数组,并且正试图将这些文件发送给客户端,它只是在一个无限循环中(因为它意味着!)并且什么都不做。如何让服务器向客户端发送连接消息?
试图在服务器上的客户端的连接发送的消息:
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();
,但这样做只是让客户静坐在无限循环,什么都不做。
请帮忙吗?
我试图让服务器立即发送连接,但它不会工作。我将编辑我的帖子以显示我现在正在尝试的内容。 – 2011-03-10 15:52:36
@John McCarthy - 新增示例代码来回答。 – 2011-03-10 16:48:19