当前位置:网站首页>LwIP development | socket | TCP | client
LwIP development | socket | TCP | client
2022-07-28 16:21:00 【Chop Hua】
Reference resources :**https://blog.csdn.net/Chuangke_Andy/article/details/113116289
** significance :** Improve each platform lwip TCP/UDP Programming compatibility , Portability
Environmental Science :
1.freertos
2. Turn on LWIP_SOCKET
3. Bgi, HC32F4A0( Compatible )
Code programming :
#include "tcp_client_test.h"
#include "lwip/opt.h"
#include <lwip/sockets.h>
#include "lwip/sys.h"
#include "lwip/api.h"
#include "string.h"
#if LWIP_SOCKET
#define DEST_PORT 6134// Destination address port number
#define DEST_IP "192.168.2.195"/* Destination address IP, This is set as local */
#define MAX_DATA 1024// The maximum amount of data received
#define LWIP_TCP_DEBUG_ENABLE 1
#if LWIP_TCP_DEBUG_ENABLE
#define LWIP_TCP_DEBUG printf
#else
#define LWIP_TCP_DEBUG(...)
#endif
void tcp_client_thread(void)
{
int sockfd,new_fd;/*cocket Handle and handle after receiving connection */
struct sockaddr_in dest_addr;/* Destination address information */
char buf[MAX_DATA];// Store received data
sockfd=socket(AF_INET,SOCK_STREAM,0);/* establish socket*/
if(sockfd < 0)
{
LWIP_TCP_DEBUG("socket failed:%d",errno);
}
dest_addr.sin_family=AF_INET;
dest_addr.sin_port=htons(DEST_PORT);
dest_addr.sin_addr.s_addr=inet_addr(DEST_IP);
inet_aton(DEST_IP,&dest_addr.sin_addr);/* Convert the dotted decimal system to 32 Bit integer type */
if(connect(sockfd,(struct sockaddr*)&dest_addr,sizeof(struct sockaddr)) == -1){
// Connection method , Incoming handle , Destination address and size
LWIP_TCP_DEBUG("connect failed:%d",errno);// In case of failure, you can print errno
}
else
{
LWIP_TCP_DEBUG("connect success");
recv(sockfd,buf,MAX_DATA,0);// Enter the received data into buf, The parameters are handles , Storage place , Maximum length , Other information ( Set to 0 that will do ).
LWIP_TCP_DEBUG("Received:%s",buf);
}
close(sockfd);// close socket
return;
}
#endif
边栏推荐
猜你喜欢
随机推荐
JS array (summary)
远距离串口服务器( 适配器)UART 转 1-Wire 应用
One channel encoder, two channels Di speed measurement, RS485 serial port connected to one channel do alarm module ibf151
ffmpeg获取首帧
Pyqt5 rapid development and practice 5.1 tables and trees
2021 亚鸿笔试题
仅需三步 轻松实现远程办公
Brief tutorial for soft exam system architecture designer | software debugging
JS linked list 02
Knowledge points qwer
js 优先级队列
兆骑科创创新创业大赛人才引进平台,双创赛事高层次人才引进
Dynamic programming -- digital statistics DP
深入理解Istio流量管理的熔断配置
Numpy ndarray learning < I > Foundation
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
高精度绝对角度传感器应用高速度角度监测
RF module wireless transceiver rf63u chip application data transmission and infrastructure network
分体式测斜探头安装要点及注意事项
Redis系列4:高可用之Sentinel(哨兵模式)









