【发布时间】:2019-07-10 07:33:20
【问题描述】:
我有一个想要打开的宽带雷达。要打开它,我必须发送两个寄存器,如下所示:
int reg0[3] = {0x00, 0xC1, 0x01};
int reg1[3] = {0x01, 0xC1, 0x01};
主要问题是我的雷达设备使用 UDP 协议并在 IGMP(ISO 模型的第 3 层)中工作。我从 IBM 支持部门阅读了很多关于在这些站点中发送 UDP 的信息: https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.hala001/cskudp.htm https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.bpxbd00/setopt.htm …和其他人。
我认为既不需要创建struct ip_mreq,也不需要使用setsockopt() 添加选项,因为我只想发送数据报。
我已经尝试过做这种事情:
#define IPM struct ip_mreq
IPM mcast;
int sock_err = bind(skemit, (SA *) &emit, lemit);
mcast.imr_multiaddr.s_addr = MCAST_JOIN_GROUP;//multicastaddress
mcast.imr_interface.s_addr = add1;
setsockopt(semit, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&mcast, sizeof(IPM)); //sizeof(mcast)
这是我的代码:
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <signal.h>
#define SAI struct sockaddr_in
#define SA struct sockaddr
int main(){
SAI emit;
uint32_t add1 = htonl(inet_addr("236.6.7.10")); //host address
unsigned short port = htons(6680); // port
memset(&emit, '\0', sizeof(emit));
emit.sin_family = AF_INET;
emit.sin_port = port;
emit.sin_addr.s_addr = add1;
socklen_t lemit = sizeof(SAI);
int skemit = socket(AF_INET, SOCK_DGRAM, 0);
if (skemit == -1){ perror("creating socket"); }
/*
* Send a message to the multicast address specified by the
* emit sockaddr structure.
*/
int reg0[3] = {0x00, 0xC1, 0x01}; //turn_on
int reg1[3] = {0x01, 0xC1, 0x01}; //turn_on
/* Send the message in reg0 and reg1 to the server */
if (sendto(skemit, reg0, sizeof(reg0), 0, (SA*)&emit, lemit) < 0){perror("sending datagram message reg0");}
if (sendto(skemit, reg1, sizeof(reg1), 0, (SA*)&emit, lemit) < 0){perror("sending datagram message reg1");}
}
我收到此错误:
sending datagram message reg0: Network is unreachable
sending datagram message reg1: Network is unreachable
你能帮我吗?
【问题讨论】:
-
错误“网络无法访问”意味着没有到目标主机的路由。沿途某处的路由器根本不知道如何转发您的消息。
-
或者您自己的主机可能没有到该 mcast 地址的路由。您是正确的,因为您不必加入 mcast 组。注意应该是
emit.sin_port = htons(port);。 -
好吧,你已经说过了... IGMP 与 UDP 处于同一级别。这就像试图向 TCP 发送一个 UDP 数据包。你可以做到,但有什么意义呢? PROTOCOLS 是协议,你必须遵守规则才能使用它们。
-
你不是将你的UDP数据包发送到IGMP,而是发送到UDP,目的端口6680。要将数据包发送到IGMP,你需要在
socket(2)调用中选择IGMP协议,如果内核支持 IGMP 套接字。)协议 0 是默认的AF_INET数据报协议,它是UDP,而不是IGMP。我认为您对协议有一点了解。 -
好的@LuisColorado,我想我还没有清楚地暴露我的问题。我知道 UDP 是传输层(第 4 层),而 IGMP 与路由器处于同一级别。但我不知道如何在不使用 IP 协议的情况下将数据发送到任何路由。设备(雷达)和我的显示单元(pc)都使用 UDP 和 IGMP。
标签: python c network-programming