rt-thread/examples/network/tcpsendpacket.c

90 lines
2.7 KiB
C
Raw Permalink Normal View History

/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
*/
2013-01-08 21:05:02 +08:00
#include <rtthread.h>
2020-04-17 18:39:04 +08:00
#include <netdb.h> /* 为了解析主机名需要包含netdb.h头文件 */
#include <sys/socket.h> /* 使用BSD socket需要包含socket.h头文件 */
void tcp_senddata(const char *url, int port, int length)
2013-01-08 21:05:02 +08:00
{
2017-08-10 14:09:57 +08:00
struct hostent *host;
int sock, err, result, timeout, index;
struct sockaddr_in server_addr;
rt_uint8_t *buffer_ptr;
2013-01-08 21:05:02 +08:00
2020-04-17 18:39:04 +08:00
/* 通过函数入口参数url获得host地址如果是域名会做域名解析 */
2017-08-10 14:09:57 +08:00
host = gethostbyname(url);
2020-04-17 18:39:04 +08:00
/* 创建一个socket类型是SOCKET_STREAMTCP类型 */
2017-08-10 14:09:57 +08:00
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
{
2020-04-17 18:39:04 +08:00
/* 创建socket失败 */
2017-08-10 14:09:57 +08:00
rt_kprintf("Socket error\n");
return;
}
2013-01-08 21:05:02 +08:00
2020-04-17 18:39:04 +08:00
/* 申请内存 */
2017-08-10 14:09:57 +08:00
buffer_ptr = rt_malloc(length);
2020-04-17 18:39:04 +08:00
if(RT_NULL == buffer_ptr)
{
/* 申请内存失败 */
2020-04-17 18:44:15 +08:00
rt_kprintf("No memory\n");
2021-03-07 08:34:04 +08:00
return;
2020-04-17 18:39:04 +08:00
}
2021-03-07 08:34:04 +08:00
2020-04-17 18:39:04 +08:00
/* 构造发送数据 */
2017-08-10 14:09:57 +08:00
for (index = 0; index < length; index ++)
buffer_ptr[index] = index & 0xff;
2013-01-08 21:05:02 +08:00
2017-08-10 14:09:57 +08:00
timeout = 100;
2020-04-17 18:39:04 +08:00
/* 设置发送超时时间100ms */
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
2020-04-17 18:39:04 +08:00
/* 初始化预连接的服务端地址 */
2017-08-10 14:09:57 +08:00
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
2013-01-08 21:05:02 +08:00
2020-04-17 18:39:04 +08:00
/* 连接到服务端 */
2017-08-10 14:09:57 +08:00
err = connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
rt_kprintf("TCP thread connect error code: %d\n", err);
2013-01-08 21:05:02 +08:00
while (1)
2017-08-10 14:09:57 +08:00
{
2020-04-17 18:39:04 +08:00
/* 发送数据到sock连接 */
2017-08-10 14:09:57 +08:00
result = send(sock, buffer_ptr, length, MSG_DONTWAIT);
2020-04-17 18:39:04 +08:00
if (result < 0) //数据发送错误处理
2017-08-10 14:09:57 +08:00
{
rt_kprintf("TCP thread send error: %d\n", result);
closesocket(sock);
2017-08-10 14:09:57 +08:00
2020-04-17 18:39:04 +08:00
/* 关闭连接,重新创建连接 */
2017-08-10 14:09:57 +08:00
rt_thread_delay(10);
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
rt_kprintf("TCP Socket error:%d\n", sock);
2017-08-10 14:09:57 +08:00
err = connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
rt_kprintf("TCP thread connect error code: %d\n", err);
}
else if (result == 0)
{
2020-04-17 18:39:04 +08:00
/* 打印send函数返回值为0的警告信息 */
rt_kprintf("\n Send warning,send function returns 0.\r\n");
2017-08-10 14:09:57 +08:00
}
}
2013-01-08 21:05:02 +08:00
}
#ifdef RT_USING_FINSH
#include <finsh.h>
2020-04-17 18:39:04 +08:00
/* 输出tcpclient函数到finsh shell中 */
2013-01-08 21:05:02 +08:00
FINSH_FUNCTION_EXPORT(tcp_senddata, send a packet through tcp connection);
#endif