gpt4 book ai didi

java - 从服务器端的列表中删除不正确的用户

转载 作者:太空宇宙 更新时间:2023-11-04 09:35:14 25 4
gpt4 key购买 nike

我有一个用于学习的小型原始服务器和客户端。这里有我的服务器代码:

 public class Connector implements Runnable, SocketListener {

private Socket socket;
private ServerSocket serverSocket;
private List<ServerSideClient> clients = new LinkedList<>();
private boolean triger;

public Connector(ServerSocket serverSocket) {
this.serverSocket = serverSocket;

}

@Override
public void run() {
while (true) {
try {
System.out.println("Waiting for clients..");
triger = true;
socket = serverSocket.accept();
System.out.println("Client connected");
ServerSideClient client = createClient();
client.setConnection(true);
client.startListeningClient();

clients.add(client);

new Thread(() -> {
socketIsClosed(client);
}).start();

} catch (IOException e) {
e.printStackTrace();
}
}
}

private ServerSideClient createClient() {
return new ServerSideClient(socket);
}


@Override
public synchronized void socketIsClosed(ServerSideClient client) {
while (triger == true) {
if (client.isConnected() == false) {
triger = false;
clients.remove(client);
System.out.println("Client was removed " + clients.size());
}
}
}
}

这里我们等待新的客户端,然后创建客户端实例并将其添加到 LinkedList 中。在服务器端的实例中,我们等待来自客户端的信息并在单独的线程上发送答案。但是当客户端关闭与服务器的连接时,socketIsClosed()方法应该从集合中删除当前的客户端引用。但是当客户端断开连接时,我什至没有从 socketIsClosed(ServerSideClient client) 方法注销 System.out.println("Client was returned "+clients.size());

客户端代码:

public class Client {

private final String HOST = "localhost";
private final int PORT = 1022;
private InputStream inputStream;
private OutputStream outputStream;
private BufferedReader bufferedReader;
private Socket socket;

private boolean connection;

public Client() throws IOException {
socket = new Socket();
socket.connect(new InetSocketAddress(HOST, PORT));
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
}


public static void main(String[] args) {
Client client = null;
try {
client = new Client();
client.work();
} catch (IOException e) {
e.printStackTrace();
}
}

private void work() {
connection = true;
listenForConsoleInput();
receiveAnswerFromServer();
}

private void listenForConsoleInput() {
new Thread(() -> {

while (connection == true) {
String requset = null;
try {

requset = bufferedReader.readLine();
if (requset.equals(".")) {
closeConnection();
return;
} else {
sendRequest(requset);
}
} catch (IOException e) {
e.printStackTrace();
}
}

}).start();
}

private void sendRequest(String request) {
try {
outputStream.write(request.getBytes());
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}

private void receiveAnswerFromServer() {
new Thread(() -> {
while (connection == true) {
byte[] data = new byte[32 * 1024];
try {
int numberOfBytes = inputStream.read(data);
System.out.println("Server>> " + new String(data, 0, numberOfBytes));
} catch (IOException e) {
closeConnection();
}
}
}).start();
}

private void closeConnection() {
try {
connection = false;
socket.close();
inputStream.close();
outputStream.close();
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

socketIsClosed(ServerSideClient client) 方法在单独的线程中工作。

public class ServerSideClient {

private Socket socket;
private InputStream in;
private OutputStream out;

private boolean connection;
private int numOfBytes;

public boolean isConnected() {
return connection;
}

public void setConnection(boolean connection) {
this.connection = connection;
}

public ServerSideClient(Socket socket) {
this.socket = socket;
try {
in = socket.getInputStream();
out = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
}

public void startListeningClient() {
new Thread(() -> {
listenUsers();
}).start();
}

private void listenUsers() {
while (connection == true) {
byte[] data = new byte[32 * 1024];

readInputFromClient(data);
if (numOfBytes == -1) {
try {
connection = false;
socket.close();
in.close();
out.close();
isConnected();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Client disconected..");
return;
}
String requestFromClient = new String(data, 0, numOfBytes);
System.out.println("Client sended>> " + requestFromClient);


sendResponce(requestFromClient);

}

}

private void readInputFromClient(byte[] data) {
try {
numOfBytes = in.read(data);
} catch (IOException e) {
e.printStackTrace();
}
}

private void sendResponce(String resp) {
try {
out.write(resp.getBytes());
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}

我自两周以来一直在尝试解决这个问题,Helllllllp......

最佳答案

我能够复制您的问题,并且一个简单的解决方案是创建一个 SocketClosedListener 类:

class SocketClosedListener implements Runnable {

private final ServerSideClient client;
private List<ServerSideClient> clients;

public SocketClosedListener(ServerSideClient client, List<ServerSideClient> clients) {
this.client = client;
this.clients = clients;
}

@Override
public void run() {
while (true) {
if (!client.isConnected()) {
clients.remove(client);
System.out.println("Client was removed " + clients.size());
return;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

在 Connector 类的 run() 方法中,我们有这样的调用:

@Override
public void run() {
while (true) {
try {
System.out.println("Waiting for clients..");
triger = true;
socket = serverSocket.accept();
System.out.println("Client connected");
ServerSideClient client = createClient();
client.setConnection(true);
client.startListeningClient();

clients.add(client);

new Thread(new SocketClosedListener(client, clients)).start();//added

} catch (IOException e) {
e.printStackTrace();
}
}
}

添加的行:

new Thread(new SocketClosedListener(client, clients)).start();

它负责在单独的线程中断开连接时寻找客户端。另外还有 100 毫秒的延迟,以避免在多个线程运行时检查每个毫秒,这可能会导致问题。

通过这段代码,我可以在控制台中看到它:

Waiting for clients..
Client sended>> hi
Client disconected..
Client was removed 1
Client disconected..
Client was removed 0

关于java - 从服务器端的列表中删除不正确的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56598829/

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