源文件
#include "lwip_tcp_driver.h"
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include <cstring> static const char send_data[8] = {0xff,0x44,0x55,0x66,0xdd};namespace lwip_tcp {struct client_info *client_fo;
struct client_task_info *client_task_fo;
struct link_socket_info *socket_link_info;
void send_task(void *param) {struct client_info* client = (struct client_info *)param;const char *data_to_send = "Hello, Client!";int data_length = strlen(data_to_send);while (1) {int sent_bytes = send(client->socket_num, data_to_send, data_length, 0);if (sent_bytes < 0) {debug_print("Failed to send data to client[%d]\r\n", client->socket_num);break; }debug_print("Sent %d bytes to client[%d]\r\n", sent_bytes, client->socket_num);vTaskDelay(1000 / portTICK_PERIOD_MS); }mem_free(client);closesocket(client->socket_num);vTaskDelete(NULL);
}void tcp_server_thread(void *param){struct client_info* client = (struct client_info *)param;debug_print("Client[%d]%s:%d is connect server\r\n", client->socket_num, inet_ntoa(client->ip_addr.sin_addr),ntohs(client->ip_addr.sin_port));send(client->socket_num, (const void* )send_data, strlen(send_data), 0);TaskHandle_t send_task_handle;xTaskCreate(send_task, "SendTask", 1024, (void *)client, osPriorityNormal, &send_task_handle);if (send_task_handle == NULL) {debug_print("Failed to create send task for client[%d]\r\n", client->socket_num);}while (1){char str[1024];memset(str, 0, sizeof(str));int bytes = recv(client->socket_num, str, sizeof(str), 0);if (bytes <= 0){mem_free(client);closesocket(client->socket_num);break;}debug_print("[%d] %s:%d recv size:%d\r\n", client->socket_num, inet_ntoa(client->ip_addr.sin_addr),ntohs(client->ip_addr.sin_port), bytes);send((int )client->socket_num, (const void * )str, bytes, 0);}debug_print("[%d]%s:%d is disconnect...\r\n", client->socket_num, inet_ntoa(client->ip_addr.sin_addr),ntohs(client->ip_addr.sin_port));vTaskDelete(NULL);
}void tcp_server_init(void){int sin_size = sizeof(struct sockaddr_in);char client_name[10] = "server";char client_num[10];socket_link_info = (struct link_socket_info *)mem_malloc(sizeof(struct link_socket_info));client_task_fo = (struct client_task_info *)mem_malloc(sizeof(struct client_task_info));client_task_fo->client_handler = NULL;client_task_fo->client_task_pro = osPriorityNormal;client_task_fo->client_task_stk = 1024; if((socket_link_info->sock_listen = socket(AF_INET, SOCK_STREAM, 0)) == -1){debug_print("Socket error\r\n");return;}socket_link_info->listen_addr.sin_family = AF_INET;socket_link_info->listen_addr.sin_port = htons(5000);socket_link_info->listen_addr.sin_addr.s_addr = htonl(INADDR_ANY);memset(&(socket_link_info->listen_addr.sin_zero), 0, sizeof(socket_link_info->listen_addr.sin_zero));if (bind(socket_link_info->sock_listen, (struct sockaddr * )&socket_link_info->listen_addr, sizeof(struct sockaddr)) < 0){debug_print("Bind fail!\r\n");goto __exit;}listen(socket_link_info->sock_listen, 4);debug_print("begin listing...\r\n");while (1){socket_link_info->sock_connect = accept(socket_link_info->sock_listen, (struct sockaddr* )&socket_link_info->connect_addr, (socklen_t* )&sin_size);if (socket_link_info->sock_connect == -1){debug_print("no socket,waitting others socket disconnect.\r\n");continue;}lwip_itoa((char *)socket_link_info->sock_connect, (size_t)client_num, 10);strcat(client_name, client_num);client_task_fo->client_name = client_name;client_task_fo->client_num = client_num;client_fo = (struct client_info *)mem_malloc(sizeof(struct client_info));client_fo->socket_num = socket_link_info->sock_connect;memcpy(&client_fo->ip_addr, &socket_link_info->connect_addr, sizeof(struct sockaddr_in));client_fo->sockaddr_len = sin_size;xTaskCreate((TaskFunction_t )tcp_server_thread, (const char * )client_task_fo->client_name,(uint16_t )client_task_fo->client_task_stk,(void * )(void*) client_fo,(UBaseType_t )client_task_fo->client_task_pro ++ ,(TaskHandle_t * )&client_task_fo->client_handler);if (client_task_fo->client_handler == NULL){debug_print("no memery for thread %s startup failed!\r\n",client_task_fo->client_name);mem_free(client_fo);continue;}else{debug_print("thread %s success!\r\n", client_task_fo->client_name);}}__exit: debug_print("listener failed\r\n");closesocket(socket_link_info->sock_listen);vTaskDelete(NULL);
}}
头文件
#ifndef LWIP_TCP_DRIVER_H
#define LWIP_TCP_DRIVER_H#include "stm32f4xx_hal.h"
#include "lwip/opt.h"
#include "lwip/sockets.h"#include "lwip/sys.h"
#include "lwip/api.h"namespace lwip_tcp{struct client_info{int socket_num; struct sockaddr_in ip_addr; int sockaddr_len; };struct client_task_info{UBaseType_t client_task_pro; uint16_t client_task_stk; TaskHandle_t * client_handler; char *client_name; char *client_num; };struct link_socket_info{int sock_listen; int sock_connect; struct sockaddr_in listen_addr; struct sockaddr_in connect_addr;};void tcp_server_init(void);void tcp_client_init(void);
}
#endif