如何让服务器在连接时向客户端发送消息?
我正在尝试编写一个基本的客户端-服务器程序,以允许客户端遍历服务器的文件。我希望服务器在连接客户端时将当前目录中所有文件的列表发送给客户端。我该怎么做呢?我已经创建了所有文件名的数组,并尝试将它们发送到客户端,它只是处于无限循环中(正如它的意思!)并且什么也不做。
尝试让服务器在客户端连接上发送消息:
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();
但这样做只会使客户端陷入无限循环而不执行任何操作。
请帮忙?
I am trying to write a basic client-server program to allow the client to traverse the files of the server. I wish to get the server to, on connection of the client, send a list of all the files in the current directory to the client. How would I go about doing this ? I have already created an array of all the filenames, and am trying to send these to the client it just sits in an infinite loop (as it is meant to !) and does nothing.
Attempting to have the server send a message on connection of the client :
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());
}
And to have the server receive this :
//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();
but doing this just makes the client sit in an infinite loop and do nothing.
Help please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的主要问题是您尝试从客户端向服务器发送空字符串。空字符串转换为 0 长度字节数组。实际上,您没有在客户端和服务器之间发送任何数据。在这种情况下,您的服务器将继续等待通过套接字的 InputStream 检索数据。由于服务器仍在等待,因此它不会继续将数据发送回客户端。因此,当您的客户端尝试侦听来自服务器的数据时,它会无限期地等待,因为服务器永远不会到达该部分代码。
如果您的目标是让服务器在连接时简单地发送数据,那么您有几个选择。
服务器示例:
客户端示例:
You primary problem is that you are attempting to send an empty string from the client to the server. An empty string translates to a 0 length byte array. In effect, you are not sending any data between the client and the server. In this situation, your server will continue to wait for data to be retrieved over the socket's InputStream. Since the server is still waiting, it does not move on to send the data back to the client. As a result, when you client tries to listen for data from the server, it waits indefinitely as the server never reaches that part of the code.
If your goal is to have the server simply send the data upon connect, you have a couple of options.
Example Server:
Example Client:
首先确保客户端和服务器之间进行以下握手:
现在有两条路径:
备用路径:
...正常通信。
发送 0 大小的消息有时可能会导致问题。
因此,使用查询响应方法似乎是准确的。
First make sure that you have following handshaking between client and server:
now there are two paths:
alternate path:
... normal communication.
Sending 0 size messages might cause problems sometimes.
So using query-response approach seems to be accurate.
在我的基本服务器-客户端 TCP 示例中,我提出了一种方法...当客户端被接受时,它会收到一条欢迎消息。当我从客户端发送角色时,它会返回名称。这是来源的链接>> http://matrixsust.blogspot.com/2011/10/basic -tcp-server-client.html 。
希望有帮助。
In my basic server-client TCP example I make a way ...when a client is got accepted it got a welcome message . In that when from client side I send role , it gives me back name. Here is the link of source >> http://matrixsust.blogspot.com/2011/10/basic-tcp-server-client.html .
Hope it helps.
我不相信服务器可以在没有提示的情况下向客户端发送消息。发送消息“dir”并在服务器上监听,然后在客户端上监听并回复。
I don't believe it is possible to have a server send a message to a client unprompted. Send a message "dir" and listen on the server, then listen on client and reply.