#defineSERVER_IP"192.168.1.124"
#defineSERVER_PORT1001
3. Implement the TCP communication via socket programming.
Create a socket:
sta_socket=socket(PF_INET,SOCK_STREAM,0);
if(-1==sta_socket){
close(sta_socket);
vTaskDelay(1000/portTICK_RATE_MS);
printf("ESP8266TCPclienttask>socketfail!\n");
continue;
}
printf("ESP8266TCPclienttask>socketok!\n");
Create a TCP connection:
bzero(&remote_ip,sizeof(structsockaddr_in));
remote_ip.sin_family=AF_INET;
remote_ip.sin_addr.s_addr=inet_addr(SERVER_IP);
remote_ip.sin_port=htons(SERVER_PORT);
if(0!=connect(sta_socket,(structsockaddr*)(&remote_ip),sizeof(structsockaddr)))
{
close(sta_socket);
vTaskDelay(1000/portTICK_RATE_MS);
printf("ESP8266TCPclienttask>connectfail!\n");
continue;
}
printf("ESP8266TCPclienttask>connectok!\n");
Send data packets via TCP communication:
if(write(sta_socket,pbuf,strlen(pbuf)+1)<0){
close(sta_socket);
vTaskDelay(1000/portTICK_RATE_MS);
printf("ESP8266TCPclienttask>sendfail\n");
continue;
}
printf("ESP8266TCPclienttask>sendsuccess\n");
free(pbuf);
Receive data packets via TCP communication:
char*recv_buf=(char*)zalloc(128);
while((recbytes=read(sta_socket,recv_buf,128))>0){
recv_buf[recbytes]=0;