rt-thread之sal+lwip的tcp客户端示例记录(接收非阻塞)

示例记录

c 复制代码
#include "lwip_test.h"
#include "lwip/sockets.h"
#include "netdev.h"


#define DBG_ENABLE
#define DBG_TAG "lwip.tst"
#define DBG_LVL DBG_LOG

#include <rtdbg.h>

#define SERVER_PORT 8080
#define SERVER_HOST "192.168.199.194"
#define REC_BUF_LEN 512
#if 1

static rt_thread_t mq_rec;
static struct netdev *netdev = RT_NULL;
const char *msg = "hello sever,i is client";


static void _th_entity(void *args) {
    struct sockaddr_in client_addr;
    struct sockaddr_in server_addr;
    int sockfd = -1;
    int result;
    /* 通过名称获取 netdev 网卡对象 */
    netdev = netdev_get_by_name(ETH_DEV_NAME);
    if (netdev == RT_NULL) {
        LOG_E("get network interface device(%s) failed.", ETH_DEV_NAME);
        return;
    }
    while (netdev->ip_addr.addr == 0 || netdev->gw.addr == 0) {
        LOG_W("current netdev stat is :%d.", netdev->flags);
        rt_thread_mdelay(1000);
    }
    LOG_I("current netdev ip addr :%s.", inet_ntoa(netdev->ip_addr));
    /* 初始化需要绑定的客户端地址 */
    client_addr.sin_family = AF_INET;
    client_addr.sin_port = htons(8080);
    client_addr.sin_addr.s_addr = netdev->ip_addr.addr;
    rt_memset(&(client_addr.sin_zero), 0, sizeof(client_addr.sin_zero));
    /* 初始化预连接的服务端地址 */
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(SERVER_PORT);
    server_addr.sin_addr.s_addr = inet_addr(SERVER_HOST);
    rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
    rt_uint8_t *rec_buf = rt_calloc(1, REC_BUF_LEN);
    while (1) {
        /*初始化连接*/
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0) {
            LOG_W("init socket is failed: %d;waiting 1s", sockfd);
            rt_thread_mdelay(1000);
            continue;
        }
        if (bind(sockfd, (struct sockaddr *) &client_addr, sizeof(struct sockaddr)) < 0) {
            LOG_W("socket bind failed.\n");
            goto __close_socket;
        }
        result = connect(sockfd, (struct sockaddr *) &server_addr, sizeof(struct sockaddr));
        if (result < 0) {
            LOG_W("connect is failed: %d;waiting 1s", result);
            goto __close_socket;
        }
        /*配置成非阻塞模式*/
        int val = 1;
        ioctlsocket(sockfd, FIONBIO, &val);
        client_rec: /*接收标签*/

        result = recv(sockfd, rec_buf, REC_BUF_LEN, 0);

        if (result == 0) { /*表示连接关闭*/
            LOG_I("server(%s) reject connected or closed", SERVER_HOST);
            goto __close_socket;
        } else if (result == -1) { /*表示未接收到数据*/
            rt_thread_mdelay(10);
            goto client_rec;
        } else if (result > 0) {
            /*连接成功*/
            if (send(sockfd, msg, strlen(msg), 0) == strlen(msg)) {
                goto client_rec;
            } else {
                goto __close_socket;
            }
        } else {
            /*todo 逻辑上不会执行到此处*/
            LOG_E("unknown result: %d", result);
            goto __close_socket;
        }

        __close_socket:
        closesocket(sockfd);
        rt_thread_mdelay(1000);

    }
    rt_free(rec_buf);
}

int lwip_test() {

    mq_rec = rt_thread_create("lwip_test",
                              _th_entity,
                              RT_NULL,
                              1024,
                              15,
                              20);
    if (mq_rec) {
        rt_thread_startup(mq_rec);
    }


    return RT_EOK;

}


INIT_APP_EXPORT(lwip_test);


#endif

设置接收非阻塞函数记录分析



相关推荐
ElePower952711 分钟前
STM32学习(一)
stm32·嵌入式硬件
紫阡星影2 小时前
【模块系列】STM32&1.69TFT屏幕
stm32·单片机·嵌入式硬件
彭某。2 小时前
STM32低功耗模式结合看门狗
stm32·单片机·嵌入式硬件
手心里的白日梦3 小时前
网络层协议--ip协议
网络·网络协议·tcp/ip
IT 古月方源3 小时前
关于高级acl的配置和讲解
运维·开发语言·网络·tcp/ip·智能路由器
傻啦嘿哟4 小时前
华为原生鸿蒙5.0与代理IP的奇妙融合
tcp/ip·华为·harmonyos
小菜鸟学代码··11 小时前
STM32文件详解
stm32·单片机·嵌入式硬件
芷栀夏13 小时前
如何在任何地方随时使用本地Jupyter Notebook无需公网IP
服务器·ide·tcp/ip·jupyter·ip
wenchm16 小时前
细说STM32F407单片机IIC总线基础知识
stm32·单片机·嵌入式硬件
xserver216 小时前
ensp 基于EASY IP的公司出口链路配置
网络·tcp/ip·智能路由器