在“
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 。
函数功能: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 。