java 等待来自客户端的消息读取的套接字服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26470140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Socket server that waits for message from client to read
提问by jgr208
I have the following socket server in java,
我在java中有以下套接字服务器,
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class MiddleSocket {
private ServerSocket middleman;
private int port = 1027;
private Socket client;
protected void createSocketServer()
{
try
{
middleman = new ServerSocket(port);
client = middleman.accept();
middleman.close();
PrintWriter out = new PrintWriter(client.getOutputStream(),true);
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
System.out.println("echo: " + in.readLine());
out.println("test");
}
}
catch(IOException e)
{
System.out.println(e);
}
}
}
It works correctly in how it reads from the client and everything. However, I want the socket server to only try and read when there is a message from the client since in the loop it keeps reading even when no messages from the client are received this outputs a bunch of
它在从客户端和所有内容读取的方式上正常工作。但是,我希望套接字服务器仅在有来自客户端的消息时才尝试读取,因为在循环中,即使没有收到来自客户端的消息,它也会继续读取,这会输出一堆
echo: null
echo: null
Is there some mechanism short of a wait()
to have the while loop to know to just sit and wait and then fire when a message is sent from the client to read that message and once that message is read and a reply is sent, to then just sit idle until the next message from the client is received?
是否有某种机制缺乏wait()
让 while 循环知道只是坐下等待,然后在客户端发送消息以读取该消息时触发,一旦读取该消息并发送回复,然后就坐下空闲直到收到来自客户端的下一条消息?
采纳答案by WillBD
You can simply do something like as follows:
您可以简单地执行如下操作:
String line;
while ((line = in.readLine()) != null) {
\Do stuff
}
This should have the expected behaviour.
这应该具有预期的行为。
Edit:
编辑:
here's a full example of what I was talking about in the comments using your code:
这是我在使用您的代码的评论中谈论的内容的完整示例:
package javaapplication12;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketExample {
private ServerSocket middleman;
private int port = 8080;
private Socket client;
protected void createSocketServer()
{
try
{
while (true){
middleman = new ServerSocket(port);
client = middleman.accept();
middleman.close();
PrintWriter out = new PrintWriter(client.getOutputStream(),true);
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String line;
while((line = in.readLine()) != null)
{
System.out.println("echo: " + line);
out.println("test");
}
}
}
catch(IOException e)
{
System.out.println(e);
}
}
public static void main(String[] args){
SocketExample test = new SocketExample();
test.createSocketServer();
}
}
回答by ControlAltDel
You can use InputStream.read(), or InputStream.read(byte[]). Both will block
您可以使用 InputStream.read() 或 InputStream.read(byte[])。两者都会阻塞
回答by user207421
I want the socket server to only try and read when there is a message from the client since in the loop it keeps reading even when no messages from the client are received this outputs a bunch of
我希望套接字服务器只在有来自客户端的消息时才尝试读取,因为在循环中,即使没有收到来自客户端的消息,它也会继续读取,这会输出一堆
echo: null
That's because you're ignoring end of stream, which is what the null means. It doesn't mean there are no messages from the client at all. It means the peer has closed the connection.
那是因为您忽略了流的结尾,这就是 null 的含义。这并不意味着根本没有来自客户端的消息。这意味着对等方已关闭连接。