TCPClient
#define SERVERADDR "192.168.0.106"
#define SERVERPORT 6008
void ATaskTcpClient(void *pvParameters)
{
int iVariableExample = 0;
int fd = -1;
int NetTimerOut = 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, &NetTimerOut, 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
{
/* code */
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");
/* code */
}
vTaskDelay(100);
} while (ret == -1);
}
vTaskDelete(NULL);
}
void tcdclient_int(void)
{
xTaskCreate(ATaskTcpClient, "tcpclient", 256, NULL, 4, NULL);
}
测试结果