微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 无线和射频 > TI WIFI设计交流 > CC3200作为TCP SOCKET server接收多个客户端连接

CC3200作为TCP SOCKET server接收多个客户端连接

时间:10-02 整理:3721RD 点击:

请问TI工程师,有哪些资料或例程可供参考啊,SDK库里的例程就算了吧,就是一对一连接,断开重连功能都没做,没有参考价值

请参考如下:

void TcpServerMultiClient(int usPort)
{
int serverID, socketID[7] = {-1, -1, -1, -1, -1, -1, -1};
long lNonBlocking = 1;

SlSockAddrIn_t sAddr;
SlSockAddrIn_t sLocalAddr;
struct SlTimeval_t timeVal;
int iAddrSize;
int iStatus;
int iNewSockID;
int i, Ret;

char buffer[1024];

sLocalAddr.sin_family = SL_AF_INET;
sLocalAddr.sin_port = sl_Htons((unsigned short)usPort);
sLocalAddr.sin_addr.s_addr = 0;

timeVal.tv_sec = 0;
timeVal.tv_usec = 0;

serverID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);

if(serverID < 0)
{
printf("Create TCP server socket failed\r\n");
return;
}

iAddrSize = sizeof(SlSockAddrIn_t);

// binding the TCP socket to the TCP server address
iStatus = sl_Bind(serverID, (SlSockAddr_t *)&sLocalAddr, iAddrSize);
if( iStatus < 0 )
{
sl_Close(serverID);
// error
printf("Bind TCP server socket failed\r\n");
return;
}

// putting the socket for listening to the incoming TCP connection
iStatus = sl_Listen(serverID, 0);
if( iStatus < 0 )
{
sl_Close(serverID);
printf("Listen TCP server socket failed\r\n");
return;
}
printf("TCP Server Listening...\n\r");

// setting socket option to make the socket as non blocking
iStatus = sl_SetSockOpt(serverID, SL_SOL_SOCKET, SL_SO_NONBLOCKING,
&lNonBlocking, sizeof(lNonBlocking));
if( iStatus < 0 )
{
printf("set socket TCP server socket failed\r\n");
sl_Close(serverID);
return;
}

sl_SetSockOpt(serverID,SOL_SOCKET,SL_SO_RCVTIMEO, &timeVal, sizeof(timeVal));

//serverID = SL_EAGAIN;

while(1)
{
for(i = 0; i < 7; i ++)
{
if(socketID[i] < 0)
{
if( (socketID[i] = sl_Accept(serverID, ( struct SlSockAddr_t *)&sAddr, (SlSocklen_t*)&iAddrSize)) >= 0)
{
printf("Got a connection, socket id:%d\r\n", socketID[i]);
iStatus = sl_SetSockOpt(socketID[i], SL_SOL_SOCKET, SL_SO_NONBLOCKING,
&lNonBlocking, sizeof(lNonBlocking));

sl_SetSockOpt(socketID[i],SOL_SOCKET,SL_SO_RCVTIMEO, &timeVal, sizeof(timeVal));
}
}
//printf("socket id:%d\r\n", socketID[i]);
if(socketID[i] > 0)
{
//printf("socket id:%d--i:%d\r\n", socketID[i], i);

Ret = sl_Recv(socketID[i], buffer, 1024, 0);

if(Ret > 0)
{
buffer[Ret] = '\0';
printf("Received packets:%s\r\n", buffer);
}
//else
// printf("Returned value:%d\r\n", Ret);
}
}
}
}

谢谢

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top