Feescale K60开发笔记7: RTCS中sendto函数的使用


      在“ Feescale K60开发笔记6 ”中介绍了UDP发送数据的方法,今天在进行测试的时候,当设定的主机不存在时,CPU处于死机状态,只有当设定的主机在网络中存在的情况下,才能正常通讯,经过反复查找原因,目标锁定在sendto函数上,现把该函数的具体使用方法,阐述清晰。

    函数功能:Sends data on the datagram socket
    函数原型:int32_t sendto{
                                uint32_t    socket,
                                char        *buffer,
                                uint32_t    buflen,
                                uint16_t    flags,
                                sockaddr    *destaddr,
                                uint16_t    addrlen)
    参数解读:  socket[in]:Handle for the socket, on which to send data.
                buffer[in]:Pointer to the buffer of data to send.
                buflen[in]:Number of bytes in the buffer(no restriction).
                flags:需特殊解释
                sockaddr:在远程地址变量前加&即可,例如 &remote_sin。
                addrlen:如此写即可,
sizeof(sockaddr_in)。

    描述:
    The function sends the data(at buffer) as a UDP datagram to the remote endpoint(at destaddr).This function can also be used when a remote endpoint has been prespecified through connect(). The datagram is send to destaddr even if it is different than the prespeified.这个函数可以通过UDP给远程端点发送数据,远程端点可以实现指定(用connect函数),也可以不指定。
    If the socket address has been prespecified, you can call sendto() with destaddr set to NULL and addrlen equal to zero, this combination send to the prespecified address.Calling sendto() with destaddr set to NULL and addrlen equal to zero without first having prespecified the destination will result in an error.如果已经指定远程socket地址,可以将sendto函数的
sockaddr参数设置为NULL, addrlen参数设置为0。如果没有指定 远程socket地址,并将 sockaddr参数设置为NULL, addrlen参数设置为0,则会返回一个错误。
    The override is temporary and lasts for the current call to sendto() only.Setting flags to RTCS_MSG_NOLOOP is useful when broadcating or multicasting a datagram to several destinations. When flags is set to 
RTCS_MSG_NOLOOP, the datagram is not duplicated for the local host interface.参数的重载是临时的仅用于当前对 sendto函数的调用,标志 RTCS_MSG_NOLOOP可用于广播或多播,数据留不会复制给当前本地主机接口,就是不发给自己。
    This function blocks,but the command is immediately serviced and replied to.这个函数执行后会被锁定,但是命令立刻会被执行,当收到回复命令后,解除锁定

    我遇到的问题就在于flag标志上,在没有查看这个文档之前,flag标志一直用0来描述。实际上
flag标志可以有以下几种  :
    RTCS_MSG_BLOCK:执行后锁定
    RTCS_MSG_NONBLOCK:执行后不锁定

    RTCS_MSG_CHKSUM:计算效验和
    RTCS_MSG_NOCHKSUM:不计算效验和

    RTCS_MSG_NOLOOP:广播或多播时,数据不返还给主机(就是发数据的)
    0:忽略

    通过以上分析就可以看出,造成本文所描述的现象的原因在于,没有指定
RTCS_MSG_NONBLOCK参数,指定该参数后,程序回复正常。样例程序如下:
    
sendto(listensock, peripheral_01_ptr->command, peripheral_01_ptr->command_length, RTCS_MSG_NONBLOCK, &remote_sin, sizeof(sockaddr_in));

原创性文章,转载请注明出处  http://user.qzone.qq.com/2756567163  
CSDN:http://blog.csdn.net/qingwufeiyang12346
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

snmplink

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值