捕捉有 Wi-Fi 连接的时刻

发布于 2024-10-17 05:39:10 字数 1775 浏览 10 评论 0原文

我需要编写遵循以下步骤的程序:

  1. 启动程序(守护进程)
  2. 等待(睡眠,阻止)直到我有wifi连接
  3. 从服务器发送/获取一些数据
  4. 等待直到wifi连接断开
  5. 转到2

步骤2的问题。我不知道如何捕捉建立网络连接的时刻。有 /proc/net/wireless 条目,其中显示有关可用无线连接的信息,但尝试使用 inotify 监视它没有成功。网络连接是异步建立的。

这是我使用 inotify 进行的测试代码(主要从 R.Loves 书中复制):

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/inotify.h>
#include <sys/select.h>

#define BUF_LEN 1024

int
main() {

    int fd, wd, rc;
    char buf[BUF_LEN];
    ssize_t len, i = 0;
    static fd_set read_fds;

    fd = inotify_init();
    if (fd == -1) {
        perror("inotify_init");
        exit(EXIT_FAILURE);
    }

    wd = inotify_add_watch(fd, "/proc/net/wireless", IN_ALL_EVENTS);
    if (wd == -1) {
        perror("inotify_add_watch");
        exit(EXIT_FAILURE);
    }

    for (;;) {

        FD_ZERO(&read_fds);
        FD_SET(wd, &read_fds);
        rc = select(wd + 1, &read_fds, NULL, NULL, NULL);
        if (rc == -1)
            perror("select");

        len = read(fd, buf, BUF_LEN);
        while (i < len) {
            struct inotify_event *event = (struct inotify_event *) &buf[i];
            printf("wd=%d mask=%d cookie=%d len=%d dir=%s\n",
                event->wd, event->mask, event->cookie, event->len, 
                (event-> mask & IN_ISDIR) ? "yes" : "no");
            if (event->len)
                printf("name=%s\n", event->name);

            i += sizeof(struct inotify_event) + event->len;
        }

        sleep(1);
    }

    return 0;
}

当我执行 cat /proc/net/wireless 时,它只会捕获evernt

问题:当我有网络连接时,如何捕获时刻运行(wifi),仅使用Linux功能?

PS这是我在这里的第一篇文章,希望一切顺利。

I need to write program which follow such steps:

  1. Start program (daemon)
  2. Wait (sleep, block) until I have wifi connection up
  3. Send/get some data from server
  4. Wait until wifi connection goes down
  5. goto 2

Problem with step 2. I dont know how to catch moment when there is established network connection. There is /proc/net/wireless entry, where information about available wireless connections appear, but trying to monitor it with inotify have no success. Network connection is established asynchronously.

Here is my test code with inotify (copied mostly from R.Loves book):

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/inotify.h>
#include <sys/select.h>

#define BUF_LEN 1024

int
main() {

    int fd, wd, rc;
    char buf[BUF_LEN];
    ssize_t len, i = 0;
    static fd_set read_fds;

    fd = inotify_init();
    if (fd == -1) {
        perror("inotify_init");
        exit(EXIT_FAILURE);
    }

    wd = inotify_add_watch(fd, "/proc/net/wireless", IN_ALL_EVENTS);
    if (wd == -1) {
        perror("inotify_add_watch");
        exit(EXIT_FAILURE);
    }

    for (;;) {

        FD_ZERO(&read_fds);
        FD_SET(wd, &read_fds);
        rc = select(wd + 1, &read_fds, NULL, NULL, NULL);
        if (rc == -1)
            perror("select");

        len = read(fd, buf, BUF_LEN);
        while (i < len) {
            struct inotify_event *event = (struct inotify_event *) &buf[i];
            printf("wd=%d mask=%d cookie=%d len=%d dir=%s\n",
                event->wd, event->mask, event->cookie, event->len, 
                (event-> mask & IN_ISDIR) ? "yes" : "no");
            if (event->len)
                printf("name=%s\n", event->name);

            i += sizeof(struct inotify_event) + event->len;
        }

        sleep(1);
    }

    return 0;
}

It only catches evernt when I do cat /proc/net/wireless

Question: How to catch moment, when I have network connection running (wifi), using only Linux features?

P.S. This is my first post here, hope everything is ok.

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

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

发布评论

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

评论(1

北陌 2024-10-24 05:39:10

您可以通过 netlink 接口 rtnetlink 检测网络连接(不仅仅是 wifi)何时变为链路就绪状态。

这不是一个容易编程的接口,因此您可能希望改为调用进程“ip monitor link”。如果您看到接口具有 LOWER_UP 标志,则意味着它已准备好发送/接收(编辑:您可能还想检查 NO_CARRIER 标志是否不存在;请参阅 Simon 的评论)。

但是,还存在一个问题,即您可能会与 NetworkManager 等守护程序发生竞争情况,该守护程序(如果如此配置)会在链接可用后尝试获取 IP 地址。

You can detect when a network connection (not just wifi) beomes link-ready through the netlink interface, rtnetlink.

This is not an easy interface to program against, so you might wish to invoke the process "ip monitor link" instead. If you see the interface have the LOWER_UP flag, that means it's ready to send/ receive (EDIT: You may also want to check the NO_CARRIER flag is absent; see Simon's comment).

However, there is also a problem that you may have a race condition with a daemon like NetworkManager, which will (if so configured) attempt to get an IP address after the link becomes available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文