当代码保持接受新套接字时,如何检查某些变量?

发布于 2024-11-27 02:02:14 字数 895 浏览 1 评论 0原文

在我制作的应用程序中,服务器接受来自客户端的套接字连接并执行各种工作。客户端列表存储在具有 LinkedList 类型的 clients 中。

我正在使用 while 语句和基于 clients 队列大小的条件语句。

我想知道是否有办法解决下面描述的问题......这样服务器就不会永远挂起。

while(clients.size()>0){ //few clients the queue is alive at the moment
    //Suddenly, all clients in the queue shut down at this point in the while loop,
    //Another thread, which checks clients knows that clients are dead
    //so clients.size() becomes 0, but there is no way to check that at this point
    //and server is expecting for connection clients forever

    Socket socket= server.accept();
    socket.close();
}
//printout results

编辑

客户列表不仅仅是现有客户。这是一份活着的客户的名单。该列表由另一个线程更新。

我上面有 while 语句,因为我想在没有活着的客户离开后做一些工作。 (要么完成他们的工作,要么就死了)

我想在不使用超时异常的情况下解决这个问题,因为客户端会在随机时间响应。 (同样,客户端的活跃度由另一个线程使用心跳技术检查)

In a application that I made, server accepts socket connection from clients and do various work. List of clients is stored in clients which has a LinkedList type.

I am using while statement with conditional statement based on size of the clients queue.

I want to know if there is a way to solve problems described below... so that server will not hang forever.

while(clients.size()>0){ //few clients the queue is alive at the moment
    //Suddenly, all clients in the queue shut down at this point in the while loop,
    //Another thread, which checks clients knows that clients are dead
    //so clients.size() becomes 0, but there is no way to check that at this point
    //and server is expecting for connection clients forever

    Socket socket= server.accept();
    socket.close();
}
//printout results

edit

The clients list is not just existing clients. It is a list of alive clients. This list is gets updated by another thread.

I have while statement above, because I want to do some job after there is no alive clients left. (either finished their job, or just died)

I want to solve this problem without using timeout exception, because clients will response in random time. (again, liveness of clients is checked by another Thread with heartbeat technique)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

总攻大人 2024-12-04 02:02:15

您可以在 ServerSocket 上设置超时并捕获 SocketTimeoutException

server.setSoTimeout(1000);
while (clients.size() > 0) {
    try {
        server.accept();
    }
    catch (SocketTimeoutException e) {
        // Ignore
    }
}

这样您将每隔一秒左右检查一次客户端列表的状态。

如果有其他线程访问它,请不要忘记同步对客户端的访问。使用Collections.synchronizedList 包装它,或者在访问变量的代码部分中放置适当的同步。

You can set the timeout on the ServerSocket and catch the SocketTimeoutException:

server.setSoTimeout(1000);
while (clients.size() > 0) {
    try {
        server.accept();
    }
    catch (SocketTimeoutException e) {
        // Ignore
    }
}

This way you will check the status of the client list every second or so.

Don't forget to synchronize access to clients if you have other threads accessing it. Either wrap it using Collections.synchronizedList or put appropriate synchronization in the parts of your code that access the variable.

童话 2024-12-04 02:02:15

你的循环条件对我来说没有任何意义。为什么在已有客户的情况下您只对接受新客户感兴趣?这个过程是如何开始的?

但一般来说,在 ServerSocket 上设置超时将使accept() 方法在这些时间间隔内抛出 SocketTimeoutExceptions,这样您就可以测试您需要测试的任何内容。

Your loop condition doesn't make any sense to me. Why would you only be interested in accepting new clients when there are existing clients? How does this process ever get started?

But in general setting a timeout on the ServerSocket will let the accept() method throw SocketTimeoutExceptions at those intervals so you can test whatever you need to test.

紫罗兰の梦幻 2024-12-04 02:02:15

我不确定我是否完全理解你的问题,但假设以下一些事情可能有效。第一个假设是您的 Client 类中有一个标志 alive ,指示它是活着还是死了。

while(clients.size()>0){
    client = clients.remove();
    if (client.isAlive()) {
        Socket socket= server.accept();
        socket.close();
    }
}

I'm not sure I've fully understood your question, but assuming a few things the following might work. First assumption is that your Client class has a flag alive in it indicating if it is alive or dead.

while(clients.size()>0){
    client = clients.remove();
    if (client.isAlive()) {
        Socket socket= server.accept();
        socket.close();
    }
}
攒眉千度 2024-12-04 02:02:15

看起来需要在客户端上进行同步,以便客户端的大小在已知点发生变化。

It looks like a need for synchronisation on clients so that the size of clients is changed at known points.

睡美人的小仙女 2024-12-04 02:02:15

当所有客户端都消失时,一个额外的线程来停止服务器套接字怎么样? :) 类似的东西

new Thread() {
    public void run() {
        if (clients.isEmpty()) {
            try { server.close();} catch (IOException e){...}
        }
    }
}.start();

while(clients.size()>0){ 
    if (!clients.isEmpty()) {
        Socket socket= server.accept();
        socket.close();
    }
}

How about an extra thread to stop the serversocket when all clients are gone? :) Something like

new Thread() {
    public void run() {
        if (clients.isEmpty()) {
            try { server.close();} catch (IOException e){...}
        }
    }
}.start();

while(clients.size()>0){ 
    if (!clients.isEmpty()) {
        Socket socket= server.accept();
        socket.close();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文