ESP8266模组SDK开发之TCP编程
TCP编程分为客户端和服务器端两个部分,其中服务器端首先建立起socket,然后与本地端口进行绑定,接着开始接收客户端的连接请求并建立与它的连接,最后接收客户端发送的消息;客户端则是在建立socket之后调用 connect() 函数来建立连接。服务器端和客户端使用TCP的流程如下图示:
1. TCP Client编程
TCP Client功能开发:PC模拟TCP Server,指定IP和Port,等待Client数据;TCP Client向Server发送“I am Client!”;Server收到数据后,向Client发送“I am Server!”
TCP Client任务流程如下示:
实验步骤如下:
–> 在ESP8266_RTOS_SDK目录下新建tcpclient文件夹,把station实验中的station目录下所有文件拷贝到该文件夹中
–> 在user目录下新建tcpclient.c文件,并添加如下代码
/*******************tcpclient.c***********************************************/
#define SERVERADDR "192.168.191.25" //PC端IP地址,确保PC端与ESP8266在同一无线网中
#define SERVERPORT 8000 //
//ATaskTcpClient任务
void ATaskTcpClient( void *pvParameters ){
int iVariableExample = 0;
int fd = -1;
int NetTimeOnt = 5000;
int ret;
struct sockaddr_in ServerAddr;
char Tcpmsg[48];
STATION_STATUS StaStatus;
do{
StaStatus = wifi_station_get_connect_status();
vTaskDelay(100);
}while(StaStatus != STATION_GOT_IP);
fd = socket(PF_INET,SOCK_STREAM,0);
if(fd == -1){
printf("get socket fail!\n");
vTaskDelete(NULL);
}
setsockopt(fd,SOL_SOCKET,SO_RCVTIMEO,&NetTimeOnt,sizeof(int));
memset(&ServerAddr,0,sizeof(ServerAddr));
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_addr.s_addr = inet_addr(SERVERADDR);
ServerAddr.sin_port = htons(SERVERPORT);
ServerAddr.sin_len = sizeof(ServerAddr);
do{
ret = connect(fd,(struct sockaddr*)&ServerAddr,ServerAddr.sin_len);
if(ret != 0){
printf("connect is fail!\n");
vTaskDelay(100);
}
} while (ret != 0);
for(;;){
send(fd,"I am TcpClient!",sizeof("I am TcpClient!"),0);
do{
ret = recv(fd,Tcpmsg,48,0);
if(ret > 0){
printf("TcpServer:%s\n",Tcpmsg);
}
else{
printf("TcpServer data is no!\n");
}
}while(ret == -1);
}
vTaskDelete( NULL );
}
//TcpClient_init初始化
void TcpClient_init(void){
xTaskCreate(ATaskTcpClient, "TcpClient", 256, NULL, 4, NULL);
}
–> 在include目录下新建tcpclient.h文件,并添加声明
void ATaskTcpClient( void *pvParameters );
void TcpClient_init(void);
–> 在user_main.c文件中的user_init()函数下添加如下代码
#define SSID "Hotspot" //PC端和ESP8266要加入的无线网
#define PASSWORD "88888888"
void ICACHE_FLASH_ATTR
user_init(void){
printf("SDK version:%s\n", system_get_sdk_version());
Led_init();
wifi_set_opmode(STATION_MODE);
struct station_config *config = (struct station_config *)\
zalloc(sizeof(struct station_config));
sprintf(config->ssid,SSID);
sprintf(config->password,PASSWORD);
wifi_station_set_config(config);
free(config);
wifi_station_set_auto_connect(TRUE);
TcpClient_init();
xTaskCreate(ATaskLed, "LED", 256, NULL, 2, NULL);
}
–> 在ESP8266编译器中编译完成,烧写bin文件到ESP8266,打开串口助手,建立TCP服务器,模组复位后,即可与TCP服务器建立连接
2. TCP Server编程
TCP Server功能开发:PC模拟TCP Client,指定Server IP和Port,进行连接,连接成功后发送“I am Client!”;Server收到数据后,向Client发送“I am Server!”
TCP Server任务流程如下示:
实验步骤如下:
–> 在ESP8266_RTOS_SDK目录下新建tcpserver文件夹,把station实验中的station目录下所有文件拷贝到该文件夹中
–> 在user目录下新建tcpserver.c文件,并添加如下代码
/*******************tcpserver.c***********************************************/
#define SERVERADDR "192.168.191.29" //esp8266连上WiFi后的IP地址
#define SERVERPORT 8000
//ATaskTcpServer任务
void ATaskTcpServer( void *pvParameters ){
int iVariableExample = 0;
int fd = -1;
int cfd = -1;
int NetTimeOnt = 20000;
int ret;
struct sockaddr_in ServerAddr;
struct sockaddr ClientAddr;
socklen_t ClientAddrlen = sizeof(struct sockaddr);
char Tcpmsg[48];
STATION_STATUS StaStatus;
do{
StaStatus = wifi_station_get_connect_status();
vTaskDelay(100);
}while(StaStatus != STATION_GOT_IP);
fd = socket(PF_INET,SOCK_STREAM,0);
if(fd == -1){
printf("get socket fail!\n");
vTaskDelete(NULL);
return;
}
setsockopt(fd,SOL_SOCKET,SO_RCVTIMEO,&NetTimeOnt,sizeof(int));
memset(&ServerAddr,0,sizeof(ServerAddr));
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_addr.s_addr = INADDR_ANY;
ServerAddr.sin_port = htons(SERVERPORT);
ServerAddr.sin_len = sizeof(ServerAddr);
if(bind(fd,(struct sockaddr*)&ServerAddr,ServerAddr.sin_len) != 0){
printf("bind socket fail!\n");
vTaskDelete(NULL);
return;
}
if(listen(fd,5) != 0){
printf("listen socket fail!\n");
vTaskDelete(NULL);
return;
}
for(;;){
cfd = accept(fd,&ClientAddr,&ClientAddrlen);
if(cfd != -1){
ret = recv(cfd,Tcpmsg,48,0);
if(ret > 0){
printf("TcpClient:%s\n",Tcpmsg);
send(cfd,"I am TcpServer!",sizeof("I am TcpServer!"),0);
}
else{
printf("TcpClient data is no!\n");
}
}
close(cfd);
}
vTaskDelete( NULL );
}
//TcpServer_init初始化
void TcpServer_init(void){
xTaskCreate(ATaskTcpServer, "TcpServer", 256, NULL, 4, NULL);
}
–> 在include目录下新建tcpserver.h文件,并添加声明
void ATaskTcpserver( void *pvParameters );
void Tcpserver_init(void);
–> 在user_main.c文件中的user_init()函数下添加如下代码
#define SSID "Hotspot" //PC端和ESP8266要加入的无线网
#define PASSWORD "88888888"
void ICACHE_FLASH_ATTR
user_init(void){
printf("SDK version:%s\n", system_get_sdk_version());
Led_init();
wifi_set_opmode(STATION_MODE);
struct station_config *config = (struct station_config *)\
zalloc(sizeof(struct station_config));
sprintf(config->ssid,SSID);
sprintf(config->password,PASSWORD);
wifi_station_set_config(config);
free(config);
wifi_station_set_auto_connect(TRUE);
TcpServer_init();
xTaskCreate(ATaskLed, "LED", 256, NULL, 2, NULL);
}
–> 在ESP8266编译器中编译完成,烧写bin文件到ESP8266,打开串口助手,建立TCP客户端,模组复位后,即可与TCP客户端建立连接