完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
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...rn"); // 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!rn"); char cRxBuf[BUF_SIZE]; while(1) [ UART_PRINT("Start Receiving...rn"); //Receiving TCP package iStatus = sl_Recv(iNewSockID, cRxBuf, iTestBufLen, 0); //正值表示接收字节数 P191 if(iStatus > 0) [ cRxBuf[iStatus]=0; UART_PRINT("rnReceive: "); Message(cRxBuf); UART_PRINT("rn"); ] else break; ] return SUCCESS;] |
|
相关推荐
3个回答
|
|
供参考借鉴,没有调 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 < 0) [ if( (socketID = sl_Accept(serverID, ( struct SlSockAddr_t *)&sAddr, (SlSocklen_t*)&iAddrSize)) >= 0) [ //Got a connection iStatus = sl_SetSockOpt(socketID, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &lNonBlocking, sizeof(lNonBlocking)); if(iStatus<0) [ sl_Close(socketID); socketID=-1; ] sl_SetSockOpt(socketID,SOL_SOCKET,SL_SO_RCVTIMEO, &timeVal, sizeof(timeVal)); if(iStatus<0) [ sl_Close(socketID); socketID=-1; ] ] else if(socketID !=SL_EAGAIN) [ LOOP_FOREVER(); ] ] if(socketID > 0) [ Ret = sl_Recv(socketID, tcpbf, 1024, 0); if(Ret > 0) [ tcpbf[Ret] = ' |