gpt4 book ai didi

java tcp套接字检查客户端是否已断开连接

转载 作者:行者123 更新时间:2023-12-02 04:24:40 25 4
gpt4 key购买 nike

我想检查我的客户端是否已断开连接。根据我的研究,一种可能的方法是我可以继续向客户端写入消息。如果客户端没有收到消息,则说明已经断开连接。我向相应的客户端发送一条消息“检查连接:客户端”+ clientNo。

我将 clientNo 1 连接到服务器,它接收

客户端1

"Checking Connection: Client1" <-- output every 10secs
"Checking Connection: Client1" <-- output every 10secs
"Checking Connection: Client1" <-- output every 10secs

但是当我将另一个客户端连接到服务器时,我的 clientNo1 停止接收来自服务器的消息,而我的 clientNo2 现在将接收

客户端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

期望的输出

客户端1

"Checking Connection: Client1" <-- output every 10secs 
"Checking Connection: Client1" <-- output every 10secs
"Checking Connection: Client1" <-- output every 10secs

客户端2

"Checking Connection: Client2" <-- output every 10secs 
"Checking Connection: Client2" <-- output every 10secs
"Checking Connection: Client2" <-- output every 10secs

请帮忙。谢谢。

服务器.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);
}
}

客户端.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;
}
}
}

最佳答案

您的ObjectInputStreamObjectOutputStreamServer类的一部分。让它们成为 acceptClient 类的成员,应该没问题。

关于java tcp套接字检查客户端是否已断开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32345343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com