[英]java tcp sockets check client disconnected
I want to check if any of my client has disconnected.From my research, a possible way is that I could keep writing messages to the clients. 我想检查是否有任何客户端断开连接。根据研究,一种可能的方法是我可以继续向客户端写入消息。 If the client fails to receive the message, it means it has disconnected.
如果客户端未能收到该消息,则表明它已断开连接。 I send a message "Checking Connection: Client" + clientNo" to the respective clients.
我向相应的客户端发送消息“正在检查连接:客户端” +“客户端编号”。
I connect clientNo 1 to the server and it receive 我将clientNo 1连接到服务器,它接收到
Client1 客户1
"Checking Connection: Client1" <-- output every 10secs
"Checking Connection: Client1" <-- output every 10secs
"Checking Connection: Client1" <-- output every 10secs
but when I connect another client to the server, my clientNo1 stops receiving the message from the server and my clientNo2 will now receive 但是当我将另一个客户端连接到服务器时,我的clientNo1 停止接收来自服务器的消息 ,而我的clientNo2现在将收到
Client2 客户2
"Checking Connection: Client2" <-- output every 10secs
"Checking Connection: Client1" <-- output every 10secs
"Checking Connection: Client2" <-- output every 10secs
"Checking Connection: Client1" <-- output every 10secs
Desired output 所需的输出
Client1 客户1
"Checking Connection: Client1" <-- output every 10secs
"Checking Connection: Client1" <-- output every 10secs
"Checking Connection: Client1" <-- output every 10secs
Client2 客户2
"Checking Connection: Client2" <-- output every 10secs
"Checking Connection: Client2" <-- output every 10secs
"Checking Connection: Client2" <-- output every 10secs
Please help. 请帮忙。 Thank you.
谢谢。
Server.java 服务器.java
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private int portNo = 0;
private ObjectOutputStream out;
private ObjectInputStream in;
private boolean clientAlive = true;
@SuppressWarnings("resource")
public Server(int portNo) {
Socket socket = null;
this.portNo = portNo;
ServerSocket sSocket = null;
int clientNo = 1;
try {
sSocket = new ServerSocket(this.portNo);
} catch (IOException e1) {
e1.printStackTrace();
}
while(true) {
try {
socket = sSocket.accept();
} catch (IOException e) {
e.printStackTrace();
}
new Thread(new acceptClient(socket,clientNo)).start();
clientNo += 1;
}
}
class acceptClient implements Runnable {
Socket socket;
int clientNo = 1;
String writeToClient = "";
public acceptClient(Socket socket, int clientNo) {
this.socket = socket;
this.clientNo = clientNo;
}
public void run() {
try {
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
} catch(IOException exception) {
System.out.println("Error: " + exception);
}
while(clientAlive) {
writeToClient = "Checking Connection: Client" + clientNo;
sendData(out, writeToClient.getBytes());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(!clientAlive) {
break;
}
}
}
}
public synchronized void sendData(ObjectOutputStream os, byte[] byteData) {
if (byteData == null) {
return;
}
try {
os.write(byteData);
os.flush();
}
catch (Exception e) {
System.out.println("Client Disconnected");
clientAlive = false;
}
}
public static void main(String[] args) {
System.out.println("Server started");
new Server(5550);
}
}
Client.java 客户端.java
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
public class Client {
public static void main(String args[]) {
clientConnection();
}
public static void clientConnection() {
Socket clientSocket = null;
ObjectOutputStream out = null;
ObjectInputStream in = null;
try {
clientSocket = new Socket("localhost", 5550);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
out = new ObjectOutputStream(clientSocket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
try {
in = new ObjectInputStream(clientSocket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
byte[] byteData = null;
while(true) {
try {
byteData = receive(in);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(new String(byteData));
}
}
public static byte[] receive(ObjectInputStream is) throws Exception {
try {
byte[] inputData = new byte[1024];
is.read(inputData);
return inputData;
}
catch (Exception exception) {
throw exception;
}
}
}
Your ObjectInputStream
and ObjectOutputStream
are part of the Server
class. 您的
ObjectInputStream
和ObjectOutputStream
是Server
类的一部分。 Make them members of acceptClient
class and it should be OK. 使它们成为
acceptClient
类的成员,应该可以。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.