请教各位一个问题:
我在开发一个项目,基于STM32F107平台,加载FREERTOS及LWIP协议栈,目前实现了TCP SERVER功能,代码如下:
void TCPServer( void *arg )
{
int optval = 1;
u32_t len;
struct sockaddr_in Server_Local_addr,Client_Remote_addr;
fd_set fdsr;
struct timeval tv;
uint8_t i;
tv.tv_sec = 3;
tv.tv_usec = 0;
(void)arg;
xServerSocket.s = lwip_socket(AF_INET,SOCK_STREAM,0);
if(xServerSocket.s == -1 )
{
return;
}
optval = 1;
lwip_setsockopt(xServerSocket.s,SOL_SOCKET,SO_REUSEADDR,&optval,sizeof(optval));
Server_Local_addr.sin_family = AF_INET;
Server_Local_addr.sin_port = htons(8800);
Server_Local_addr.sin_len = sizeof(Server_Local_addr);
Server_Local_addr.sin_addr.s_addr = INADDR_ANY;
if (lwip_bind(xServerSocket.s, (struct sockaddr *) &Server_Local_addr, sizeof(Server_Local_addr)) < 0)
{
return ;
}
if (lwip_listen(xServerSocket.s, 10) == -1)
{
return;
}
while(1)
{
len = sizeof(Client_Remote_addr);
xServerSocket.c = lwip_accept(xServerSocket.s, (struct sockaddr *)&Client_Remote_addr, &len);
if(xServerSocket.c == -1)
{
lwip_close(xServerSocket.c);
continue;
}
int ret;
while(1)
{
FD_ZERO(&fdsr);
FD_SET(xServerSocket.c, &fdsr);
ret = lwip_select(xServerSocket.c+1, &fdsr, NULL, 0, &tv);
if(ret > 0)
{
if (FD_ISSET(xServerSocket.c, &fdsr))
{
int datalen;
datalen = lwip_recv(xServerSocket.c,ServerRevBuf,NET_BUF_SIZE,0);
if(datalen > 0)
{
//此处处理接收到的数据,省略
........................
}
else
{
lwip_close(xServerSocket.c);
xServerSocket.c = -1;
break;
}
}
}
else if(ret < 0)
{
lwip_close(xServerSocket.c);
xServerSocket.c = -1;
break;
}
}
lwip_close(xServerSocket.c);
xServerSocket.c = -1;
}
// vTaskDelete( NULL );
}
目前的问题是,TCP SERVER的端口8800,需要和测试电脑的两个CLIENT端口(例如:30000、30001)同时建立连接,
进行数据收发,请问如何实现?折腾了好几天,依旧没有结果。
此帖出自
小平头技术问答
非常感谢,我再仔细研究研究你所说的几点,发现新问题再想你请教。
一周热门 更多>