[英]How to send an UDP datagram to an IGMP device?
I have a broadband Radar that I want to turn on. 我有一个想打开的宽带雷达。 To turn it on I have to send two registers like following:
要打开它,我必须发送两个寄存器,如下所示:
int reg0[3] = {0x00, 0xC1, 0x01};
int reg1[3] = {0x01, 0xC1, 0x01};
The main issue is that my Radar device is working on UDP protocol and works in IGMP (3rd layer of the ISO model). 主要问题是我的Radar设备正在使用UDP协议,并且可以在IGMP(ISO模型的第三层)中使用。 I have read a lot about UDP sending in these sites from the IBM support: 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 …and others.
我已经在IBM支持机构的这些站点上阅读了很多有关UDP发送的信息: https : //www.ibm.com/support/knowledgecenter/zh-CN/SSLTBW_2.3.0/com.ibm.zos.v2r3.hala001/cskudp.htm https ://www.ibm.com/support/knowledgecenter/zh-CN/SSLTBW_2.3.0/com.ibm.zos.v2r3.bpxbd00/setopt.htm…等。
I fathom it is neither obliged to create a struct ip_mreq
nor add options with setsockopt()
for I only want to send a datagram. 我不知道它既
struct ip_mreq
创建struct ip_mreq
也struct ip_mreq
添加带有setsockopt()
选项,因为我只想发送数据报。
I have already tried to do this sort of things: 我已经尝试做这种事情:
#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)
This is my code: 这是我的代码:
#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");}
}
I receive this errors: 我收到此错误:
sending datagram message reg0: Network is unreachable
sending datagram message reg1: Network is unreachable
Could you help me? 你可以帮帮我吗?
I have advanced in my issue, that is why I want to partially answer: I managed to do the connection in python. 我的问题已经解决,这就是为什么我要部分回答的原因:我设法在python中建立连接。 It cannot be a computer's settings' problem.
不能是计算机设置的问题。
Here is the code that works: 这是有效的代码:
import socket
import struct
MCAST_GRP = '236.6.7.8'
MCAST_PORT = 6678
HOST2 = '236.6.7.10'
PORT2 = 6680
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', MCAST_PORT)) # use MCAST_GRP instead of '' to listen only
# to MCAST_GRP, not all groups on MCAST_PORT
mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
sock2 = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
reg0=bytes([0x00, 0xC1, 0x01]) #turn_on
reg1=bytes([0x01, 0xC1, 0x01]) #turn_on
sock2.sendto(reg0, (HOST2, PORT2))
sock2.sendto(reg1, (HOST2, PORT2))
sock2.sendto(reg3, (HOST2, PORT2))
pcapfile = open('ragar.txt', 'w')
while True:
pcapfile.write(str(sock.recv(65536)))
pcapfile.close()
print('the end')
The sock2
manages to send the registers to the device whereas the sock
receives the datas correctly. sock2
设法将寄存器发送到设备,而sock
则正确接收数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.