如何让cc3200 同时接受多个ip终端发送的TCP数据包?
cc3200工作在站点模式和多个终端同时连接到路由器组成局域网,多个终端发送tcp数据包到局域网分给3200的ip地址上,现在只能实现单个终端单独发送,请问如何才能让cc3200同时接收多个终端的数据呢? 谢谢指导
int BsdTcpClient(unsigned short usPort) { SlSockAddrIn_t sAddr; SlSockAddrIn_t sLocalAddr; // int iCounter; int iAddrSize; int iSockID; int iStatus; int iNewSockID; // long lLoopCount = 0; long lNonBlocking = 1; int iTestBufLen; iTestBufLen = BUF_SIZE; //filling the TCP server socket address sLocalAddr.sin_family = SL_AF_INET; sLocalAddr.sin_port = sl_Htons((unsigned short)usPort); sLocalAddr.sin_addr.s_addr = 0; // creating a TCP socket iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0); if( iSockID < 0 ) { // error ASSERT_ON_ERROR(SOCKET_CREATE_ERROR); } iAddrSize = sizeof(SlSockAddrIn_t); // binding the TCP socket to the TCP server address iStatus = sl_Bind(iSockID, (SlSockAddr_t *)&sLocalAddr, iAddrSize); if( iStatus < 0 ) { // error sl_Close(iSockID); ASSERT_ON_ERROR(BIND_ERROR); } // putting the socket for listening to the incoming TCP connection iStatus = sl_Listen(iSockID, 0); if( iStatus < 0 ) { sl_Close(iSockID); ASSERT_ON_ERROR(LISTEN_ERROR); } // setting socket option to make the socket as non blocking iStatus = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &lNonBlocking, sizeof(lNonBlocking)); if( iStatus < 0 ) { sl_Close(iSockID); ASSERT_ON_ERROR(SOCKET_OPT_ERROR); } UART_PRINT("Wait for TCP Connection...\r\n"); // waiting for an incoming TCP connection iNewSockID = SL_EAGAIN; while( iNewSockID < 0 ) { // accepts a connection form a TCP client, if there is any // otherwise returns SL_EAGAIN iNewSockID = sl_Accept(iSockID, ( struct SlSockAddr_t *)&sAddr, (SlSocklen_t*)&iAddrSize); if( iNewSockID == SL_EAGAIN ) { MAP_UtilsDelay(10000); } else if( iNewSockID < 0 ) { // error sl_Close(iNewSockID); sl_Close(iSockID); ASSERT_ON_ERROR(ACCEPT_ERROR); } } UART_PRINT("Device Connected!\r\n"); char cRxBuf[BUF_SIZE]; while(1) { UART_PRINT("Start Receiving...\r\n"); //Receiving TCP package iStatus = sl_Recv(iNewSockID, cRxBuf, iTestBufLen, 0); //正值表示接收字节数 P191 if(iStatus > 0) { cRxBuf[iStatus]=0; UART_PRINT("\r\nReceive: "); Message(cRxBuf); UART_PRINT("\r\n"); } else break; } return SUCCESS; }
供参考借鉴,没有调
int TcpServerMultiClient(int usPort)
{
int serverID, socketID[4] = { -1, -1, -1, -1};
long lNonBlocking = 1;
SlSockAddrIn_t sAddr;
SlSockAddrIn_t sLocalAddr;
struct SlTimeval_t timeVal;
int iAddrSize;
int iStatus;
int i, Ret;
//Start TCP Server;
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)
{
ASSERT_ON_ERROR(TCP_CLIENT_FAILED);
}
iAddrSize = sizeof(SlSockAddrIn_t);
// binding the TCP socket to the TCP server address
iStatus = sl_Bind(serverID, (SlSockAddr_t *)&sLocalAddr, iAddrSize);
if( iStatus < 0 )
{
ASSERT_ON_ERROR(sl_Close(serverID));
ASSERT_ON_ERROR(TCP_CLIENT_FAILED);
}
// putting the socket for listening to the incoming TCP connection
iStatus = sl_Listen(serverID, 0);
if( iStatus < 0 )
{
ASSERT_ON_ERROR(sl_Close(serverID));
ASSERT_ON_ERROR(TCP_CLIENT_FAILED);
}
// 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 )
{
ASSERT_ON_ERROR(sl_Close(serverID));
ASSERT_ON_ERROR(TCP_CLIENT_FAILED);
}
iStatus =sl_SetSockOpt(serverID,SOL_SOCKET,SL_SO_RCVTIMEO, &timeVal, sizeof(timeVal));
if( iStatus < 0 )
{
ASSERT_ON_ERROR(sl_Close(serverID));
ASSERT_ON_ERROR(TCP_CLIENT_FAILED);
}
//serverID = SL_EAGAIN;
for(i = 0; i < 4; i ++)
{
if(socketID[i] < 0)
{
if( (socketID[i] = sl_Accept(serverID, ( struct SlSockAddr_t *)&sAddr, (SlSocklen_t*)&iAddrSize)) >= 0)
{
//Got a connection
iStatus = sl_SetSockOpt(socketID[i], SL_SOL_SOCKET, SL_SO_NONBLOCKING,
&lNonBlocking, sizeof(lNonBlocking));
if(iStatus<0)
{
sl_Close(socketID[i]);
socketID[i]=-1;
}
sl_SetSockOpt(socketID[i],SOL_SOCKET,SL_SO_RCVTIMEO, &timeVal, sizeof(timeVal));
if(iStatus<0)
{
sl_Close(socketID[i]);
socketID[i]=-1;
}
}
else
if(socketID[i] !=SL_EAGAIN)
{
LOOP_FOREVER();
}
}
if(socketID[i] > 0)
{
Ret = sl_Recv(socketID[i], tcpbf, 1024, 0);
if(Ret > 0)
{
tcpbf[Ret] = '\0';
iStatus = sl_Send(socketID[i], tcpbf, Ret, 0 );
if( iStatus <= 0 )
{
// error
;
}
}
else if(Ret==0)
{
sl_Close(socketID[i]);
socketID[i]=-1;
}
//Report("Returned value:%d\r\n", Ret);
}
}
}
}
你好,我按照你的调试了一下,程序跑完一遍socket bind listen 非阻塞 之后,重新折回socket 和 bind,并且一直卡在bind报错是为什么呢? 感谢指导
就是这个样子