当前位置:网站首页>LwIP development | realize TCP server through socket
LwIP development | realize TCP server through socket
2022-07-28 16:21:00 【Chop Hua】
LWIP To improve compatibility, you can also use Scoket Realization , It is mainly the code function in the file #include <lwip/sockets.h> in , The following are the main relevant codes
/** @ingroup socket */
#define accept(s,addr,addrlen) lwip_accept(s,addr,addrlen)
/** @ingroup socket */
#define bind(s,name,namelen) lwip_bind(s,name,namelen)
/** @ingroup socket */
#define shutdown(s,how) lwip_shutdown(s,how)
/** @ingroup socket */
#define getpeername(s,name,namelen) lwip_getpeername(s,name,namelen)
/** @ingroup socket */
#define getsockname(s,name,namelen) lwip_getsockname(s,name,namelen)
/** @ingroup socket */
#define setsockopt(s,level,optname,opval,optlen) lwip_setsockopt(s,level,optname,opval,optlen)
/** @ingroup socket */
#define getsockopt(s,level,optname,opval,optlen) lwip_getsockopt(s,level,optname,opval,optlen)
/** @ingroup socket */
#define closesocket(s) lwip_close(s)
/** @ingroup socket */
#define connect(s,name,namelen) lwip_connect(s,name,namelen)
/** @ingroup socket */
#define listen(s,backlog) lwip_listen(s,backlog)
/** @ingroup socket */
#define recv(s,mem,len,flags) lwip_recv(s,mem,len,flags)
/** @ingroup socket */
#define recvfrom(s,mem,len,flags,from,fromlen) lwip_recvfrom(s,mem,len,flags,from,fromlen)
/** @ingroup socket */
#define send(s,dataptr,size,flags) lwip_send(s,dataptr,size,flags)
/** @ingroup socket */
#define sendmsg(s,message,flags) lwip_sendmsg(s,message,flags)
/** @ingroup socket */
#define sendto(s,dataptr,size,flags,to,tolen) lwip_sendto(s,dataptr,size,flags,to,tolen)
/** @ingroup socket */
#define socket(domain,type,protocol) lwip_socket(domain,type,protocol)
/** @ingroup socket */
#define select(maxfdp1,readset,writeset,exceptset,timeout) lwip_select(maxfdp1,readset,writeset,exceptset,timeout)
/** @ingroup socket */
#define ioctlsocket(s,cmd,argp) lwip_ioctl(s,cmd,argp)
TCP Programming
Implement environment
1. transplant Freertos
2. transplant LWIP, Turn on LWIP_SOCKET
#include "tcp_server_test.h"
#include "lwip/opt.h"
#include <lwip/sockets.h>
#include "lwip/sys.h"
#include "lwip/api.h"
#include "string.h"
#if LWIP_SOCKET // Need to open Scoket Can be used
#define RECV_DATA (1024UL)
#define LOCAL_PORT (6133UL)
#define BACKLOG (5UL)/* Maximum number of monitors */
#define LWIP_TCP_DEBUG_ENABLE 1
#if LWIP_TCP_DEBUG_ENABLE
#define LWIP_TCP_DEBUG printf
#else
#define LWIP_TCP_DEBUG(...)
#endif
static void tcp_server_thread(void *arg)
{
int sockfd = -1,connected; /*socket Handle and the handle after the connection is established */
struct sockaddr_in server_addr,client_addr;
socklen_t sin_size;
int recv_data_len;
uint8_t recv_data[RECV_DATA];
LWIP_TCP_DEBUG("tcp server port %d\n\n",LOCAL_PORT);
again:
// establish scoket The descriptor AF_INET Use ipv4 Address
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
LWIP_TCP_DEBUG("Socket error\n");
close(sockfd);
vTaskDelay(100);
goto again;
}
//
server_addr.sin_family = AF_INET; // This property indicates the reception of native or other machine transmissions
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // This machine IP
server_addr.sin_port = htons(LOCAL_PORT); // Port number
memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{
LWIP_TCP_DEBUG("Unable to bind\n");
close(sockfd);
vTaskDelay(100);
goto again;
}
// Set the maximum number of listeners
if (listen(sockfd, BACKLOG) == -1)
{
LWIP_TCP_DEBUG("Listen error\n");
close(sockfd);
vTaskDelay(100);
goto again;
}
while(1)
{
sin_size = sizeof(struct sockaddr_in);
// Block here until a message is received , The parameters are socket Handle , Received address information and size
connected = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size);
LWIP_TCP_DEBUG("new client connected from (%s, %d)\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
int tcp_nodelay = 1;//don't delay send to coalesce packets
setsockopt(connected,IPPROTO_TCP,TCP_NODELAY,(void *) &tcp_nodelay,sizeof(int));
while(1)
{
recv_data_len = recv(connected, recv_data, RECV_DATA, 0);
if (recv_data_len <= 0)
{
break;
}
LWIP_TCP_DEBUG("recv %d len data\n",recv_data_len);
// send content
write(connected,recv_data,recv_data_len);
}
if (connected >= 0)
{
close(connected);
}
connected = -1;
}
if (sockfd >= 0)
{
close(sockfd);
}
}
#endif
void tcp_server_init(void)
{
#if LWIP_SOCKET
sys_thread_new("tcpecho_thread", tcp_server_thread, NULL, 512, 4);
#endif
}
Verified as follows 
Recommend a good blogger :https://blog.csdn.net/Chuangke_Andy/article/details/121420376
边栏推荐
- 便携式钻孔测斜仪数据采集仪测量原理与测斜探头的连接及使用方法
- MLX90640 红外热成像仪测温传感器模块开发笔记(八)
- js 优先级队列
- Zhaoqi scientific innovation and entrepreneurship competition platform, activity roadshow, investment and financing docking
- js 队列
- MicTR01 Tester 开发套件(振弦采集读数仪)使用说明
- Ffmpeg get the first frame
- CoDeSys realizes bubble sorting
- [wechat applet development (VII)] subscription message
- js 链表 01
猜你喜欢

mysql查询 limit 1000,10 和limit 10 速度一样快吗?如果我要分页,我该怎么办?

JS queue

Food safety | these two kinds of melons and fruits should be improved, especially for pregnant women with constipation
![[Multisim Simulation] LM339 zero crossing circuit simulation](/img/ca/f6dae5fd298c00570407c2bdfa5118.png)
[Multisim Simulation] LM339 zero crossing circuit simulation

How to measure the vibrating wire sensor by vibrating wire acquisition module?

太阳能路灯的根本结构及作业原理

光学雨量计对比翻斗式雨量计的优势

Redis series 4: sentinel (sentinel mode) with high availability

Practical development tutorial of software problem repair tracking system (Part 1)

2021 肯特面试题2
随机推荐
Multifunctional mixed signal AI acquisition / switching value di/do acquisition to rs485/232/modbus module
2021-10-21 notes
软考 系统架构设计师 简明教程 | 软件调试
头条文章_signature
[Multisim Simulation] LM339 zero crossing circuit simulation
Temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)
2021 Kent interview question 1
I'll show you a little chat! Summary of single merchant function modules
The deep displacement monitoring system wk813 is used to measure the deep displacement of slopes, dams, embankments, railways and building foundation pit excavation
Remember the common JS methods of projects
[wechat applet development (VII)] subscription message
2021 肯特面试题2
ffmpeg获取首帧
LabVIEW Linx toolkit controls Arduino equipment (expansion-1)
js 双向链表 01
Darknet training yolov4 record
js中的for循环总结
资本「断供」两年,我只能把公司卖了
Roson的Qt之旅#101 Qt Quick中的模型和视图
High speed counter to rs485modbus RTU module ibf150