当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
Darknet training yolov4 record
mysql 查看事件状态语句和修改办法
Roson的Qt之旅#101 Qt Quick中的模型和视图
Note: numerical accumulation animation
五舅的思考
小程序中的分页查询
One channel encoder, two channels Di speed measurement, RS485 serial port connected to one channel do alarm module ibf151
JS priority queue
js 链表 01
【微信小程序开发(七)】订阅消息
js 链表 02
JS array (summary)
CoDeSys realizes bubble sorting
2021 Yahong pen test questions
Writing of factorial
A program for judging the result of cyclic input
视频号找到金钥匙,抖音模仿后来人
为什么学编程的人大多数都去了深圳和北京?
IT远程运维是什么意思?远程运维软件哪个好?
2021 亚鸿笔试题2









