我有一个Java TCP服务器,当客户端连接到它时,每30秒向客户端输出一条消息.严格要求客户端不向服务器发送任何消息,并且服务器不向客户端发送除30秒间隔消息之外的任何数据.
当我断开客户端连接时,服务器将在下次尝试写入客户端时才会意识到这一点.因此,服务器最多可能需要30秒才能识别断开连接.
我想要做的是每隔几秒检查断开连接而不必等待,但我不知道如何做到这一点,因为a)服务器没有从客户端接收和b)服务器无法发送任何其他数据.请问有谁可以对此有所了解吗?谢谢.
即使您的服务器没有从客户端"接收",客户端套接字上的非阻塞读取也会告诉您无法读取任何内容(正如您所期望的那样),或者客户端已断开连接.
如果您使用NIO,你可以简单地使用非阻塞的Selector循环(与非阻塞套接字),并只在您的30-第二标记写入.如果a SelectionKey是可读的并且读取SocketChannel返回-1,则表示客户端已断开连接.
编辑:阻止的另一种方法是简单地选择30秒超时.任何客户端断开连接都会导致select返回,并且您将通过读取集知道哪些是.您需要做的另外一件事是跟踪您在选择中被阻止的时间,以确定何时在30秒标记上进行写入(将下一个选择的超时设置为增量).
大编辑:在与Myn谈话之后,提供完整的例子:
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
// Set a 1 second timeout on the socket
clientSocket.setSoTimeout(1000);
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
long myNextOutputTime = System.currentTimeMillis() + 30000;
String inputLine = null;
boolean connected = true;
while (connected)
{
try {
inputLine = in.readLine();
if (inputLine == null)
{
System.out.println("Client Disconnected!");
connected = false;
}
}
catch(java.net.SocketTimeoutException e)
{
System.out.println("Timed out trying to read from socket");
}
if (connected && (System.currentTimeMillis() - myNextOutputTime > 0))
{
out.println("My Message to the client");
myNextOutputTime += 30000;
}
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
Run Code Online (Sandbox Code Playgroud)
值得注意的是,这PrintWriter真的让你远离实际的套接字,并且你不会在写入时捕获套接字断开连接(它永远不会抛出异常,你必须手动检查它checkError())你可以改为使用BufferedWriter替代(需要使用flush()推送输出)并像处理BufferedReader写入时捕获迪斯科一样处理它.