RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-339317

Zigzag_Makryak's questions

Martin Hope
Zigzag_Makryak
Asked: 2021-12-09 15:42:45 +0000 UTC

递增 0.1 时 Visual Basic 中的 For 循环出现问题

  • 0

同事们,下午好。在 Visual Basic 中编写宏时,我遇到了一个奇怪的 For 循环问题。

例如,我有这个代码:

Sub Test_for()
    Dim cell As Range
    Dim counter As Integer
    Set cell = Range("A1")

'Первый цикл For
    counter = 0
    For i = 0 To 1 Step 0.1
        cell.Offset(counter, 0).Value = i
        counter = counter + 1
    Next i

'Второй цикл For
    counter = 0
    For j = 0 To 2 Step 0.1
        cell.Offset(counter, 1).Value = j
        counter = counter + 1
    Next j
'Третий цикл For
    counter = 0
    For k = 0.1 To 2.1 Step 0.1
        cell.Offset(counter, 1).Value = k
        counter = counter + 1
    Next k
End Sub

所以对于第一个周期,我有一个从 0 到 1 形式的输出(一切都是正确的),但是!对于第二个循环,我的输出从 0 到 1.9(但应该最多为 2)。对于第三个循环,输出从 0.1 到 2(但应该高达 2.1)!

了解VB细微差别的同事,告诉我我做错了什么,抽什么手册。对我来说,这种循环行为是不可理解的。

циклы
  • 1 个回答
  • 10 Views
Martin Hope
Zigzag_Makryak
Asked: 2020-09-02 23:11:29 +0000 UTC

无法通过 TAP 设备接收以太网数据包(使用 TunTap 功能)

  • 2

同事们,下午好。请帮助我了解以下情况。我想在用户空间处理以太网帧。要“接收”一帧,我想使用 Tap-device (这样的任务,Raw-socket 不适合这种情况)。其实是一个问题。我使用以下命令创建 Tap-device:

sudo ip tuntap add dev tap0 mode tap
sudo ip address add 10.10.10.3/30 dev tap0

我在间隔中启动了一个自写的软件部分(关于它在下面)。接下来做

sudo ip link set tap0 up
ip address show tap0

结果,我在控制台中得到以下输出:

7: tap0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
    link/ether ba:ba:18:ce:bb:66 brd ff:ff:ff:ff:ff:ff
    inet 10.10.10.3/30 scope global tap0
       valid_lft forever preferred_lft forever

同时,使用该程序,我将创建的 tap-device 与“/dev/net/tun”文件相关联。程序文本:

#include <arpa/inet.h>

#include <cstring>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>

#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/if_tun.h>

#include <net/if.h>
#include <netinet/ether.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>

#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stropts.h>

#include <syslog.h>

#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>

#include <unistd.h>

int tun_alloc(char *dev, short flags);

int main() {

  char tun_name[IFNAMSIZ];
  char buffer[1500];
  printf("Programm is starting\n");

  /* Подключаюсь к tap-device */
  strcpy(tun_name, "tap0");
  int tun_fd = tun_alloc(tun_name, IFF_TAP); /* tun interface */

  if (tun_fd < 0) {
    perror("Allocating interface");
    exit(1);
  }

  printf("Interface number = %d \n", tun_fd);

  printf("Start reading loop \n");
  while (1) {

    printf("Whating for data... \n");
    int nread = read(tun_fd, buffer, sizeof(buffer));
    if (nread < 0) {
      perror("Reading from interface");
      close(tun_fd);
      exit(1);
    }

    /* Здесь обрабатывать полученный фрейм */
    printf("Read %d bytes from device %s\n", nread, tun_name);
  }

  return 0;
}

int tun_alloc(char *dev, short flags) {
  printf("Entering tun_allocate...\n");
  struct ifreq ifr;
  int fd, err;
  char *clonedev = "/dev/net/tun";

  /* Открываем файловый дескриптор вирт.устройства,
    с которого ожидаю получить Ethernet-frame*/
  if ((fd = open(clonedev, O_RDWR)) < 0) {
    perror("open(clonedev, O_RDWR)");
    return fd;
  }

  memset(&ifr, 0, sizeof(ifr));

  ifr.ifr_flags = flags;
  if (*dev) {
    strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  }

  /* пытаюсь увязать файл и Tap-device */
  if ((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0) {
    close(fd);
    perror("ioctl(fd, TUNSETIFF, (void *)&ifr)");
    return err;
  }
  strcpy(dev, ifr.ifr_name);
  printf("Everything seems OK! \n");
  return fd;
}

在启动时,我开始收到一堆服务以太网帧,也就是说,一切似乎都在工作。但最好分析“the”数据包。

为此,我启动了一个简单的 UDP 发送器,我将它放在地址为 192.168.44.128:9999 的设备上,并开始向 10.10.10.3:9999 发送数据。

Eeeee ...在打开的文件描述符fd上我什么也没得到。同时,为了好玩,我尝试使用 UDP-listener 程序(通常的 open ... bind ... read ... cout 集)收听 10.10.10.3:9999。所有数据都在那里。

我不明白为什么我在fd上没有收到以太网帧,尽管根据 TunTap 手册,我应该收到它们,据我所知。

请告知为什么会这样。我有错误的代码吗?我误解了 TunTap 的概念吗?Tap-device的错误创建?误解了网络堆栈?

我尝试通过创建 Tun-device 来解决类似的问题,结果类似。所有变态都发生在 VM Ware Workstation 15 +5.4.0-42-generic #46~18.04.1-Ubuntu SMP Fri Jul 10 07:21:24 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux 下。

linux
  • 1 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5