#include #include #include "tcp_server.h" #include "stm32f2xx.h" #include "main.h" #include "log.h" #include "lwip/tcp.h" #include "lwip/memp.h" #include "lwip/api.h" #include "lwip/sockets.h" #include "lwip/opt.h" #include "lwip/sys.h" void tcp_server_task(void *Parameters) { int ret,sockfd; struct sockaddr_in tcpServerSock; struct sockaddr_in client_sock; tcpServerSock.sin_family = AF_INET; inet_aton("192.168.2.212",&(tcpServerSock.sin_addr)); // tcpServerSock.sin_addr.s_addr = htonl(IPADDR_ANY); tcpServerSock.sin_port = htons(8080); tcp_server_begin: sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { goto tcp_server_begin; } ret = bind(sockfd, (struct sockaddr *)&tcpServerSock, sizeof(tcpServerSock)); if (ret < 0) { lwip_close(sockfd); sockfd = -1; goto tcp_server_begin; } ret = listen(sockfd, 10); if (ret < 0) { lwip_close(sockfd); sockfd = -1; goto tcp_server_begin; } while (1) { OSTimeDly(1000); socklen_t len = sizeof(client_sock); int client_socket = accept(sockfd, (struct sockaddr*)&client_sock,&len); LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"上位机成功连接%s"); if (client_socket<0) { printf("error"); } } } /*! \brief initialize the tcp_client application \param[in] none \param[out] none \retval none */ #define TCP_TASK_PRIO 9 #define TCP_STK_SIZE 1024 OS_STK TCP_TASK_STK[TCP_STK_SIZE]; void tcp_server_init(void) { OSTaskCreate(tcp_server_task,(void*)0,(OS_STK*)&TCP_TASK_STK[TCP_STK_SIZE - 1],TCP_TASK_PRIO); }