int BsdTcpServer(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;
long lRetVal = -1;
//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);
}
iNewSockID = SL_EAGAIN;//////??
// waiting for an incoming TCP connection
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 接收
UART_PRINT("\r\ninBsdTCP:");
while (iFlag) {
osi_Sleep(100);
}
// close the connected socket after receiving from connected TCP client
//iStatus = sl_Close(iNewSockID);//
//ASSERT_ON_ERROR(iStatus);
// close the listening socket
//iStatus = sl_Close(iSockID);//注释的地方
//ASSERT_ON_ERROR(iStatus);
return SUCCESS;
}
/*****************************************************
wlan ap
*****************************************************/
void WlanAPMode(void *pvParameters) {
//int iTestResult = 0;
unsigned char ucDHCP;
long lRetVal = -1;
InitializeAppVariables();
lRetVal = ConfigureSimpleLinkToDefaultState();
if (lRetVal < 0) {
if (DEVICE_NOT_IN_STATION_MODE == lRetVal)
UART_PRINT(
"Failed to configure the device in its default state \n\r");
LOOP_FOREVER()
;
}
UART_PRINT("Device is configured in default state \n\r");
//
// Asumption is that the device is configured in station mode already
// and it is in its default state
//
lRetVal = sl_Start(NULL, NULL, NULL);
if (lRetVal < 0) {
UART_PRINT("Failed to start the device \n\r");
LOOP_FOREVER()
;
}
UART_PRINT("Device started as STATION \n\r");
//
// Configure the networking mode and ssid name(for AP mode)
//
if (lRetVal != ROLE_AP) {
if (ConfigureMode(lRetVal) != ROLE_AP) {
UART_PRINT("Unable to set AP mode, exiting Application…\n\r");
sl_Stop(SL_STOP_TIMEOUT);
LOOP_FOREVER()
;
}
}
while (!IS_IP_ACQUIRED(g_ulStatus)) {
//looping till ip is acquired
}
unsigned char len = sizeof(SlNetCfgIpV4Args_t);
SlNetCfgIpV4Args_t ipV4 = { 0 };
// get network configuration
lRetVal = sl_NetCfgGet(SL_IPV4_AP_P2P_GO_GET_INFO, &ucDHCP, &len,
(unsigned char *) &ipV4);
/*打印出主机IP*/
UART_PRINT("\r\nAP_ip地址:\r\n");
UART_PRINT(
"IP: %d.%d.%d.%d ;\r\nMASK: %d.%d.%d.%d;\r\nGateway: %d.%d.%d.%d;\r\nDNS: %d.%d.%d.%d;\r\n",
SL_IPV4_BYTE(ipV4.ipV4, 3), SL_IPV4_BYTE(ipV4.ipV4, 2),
SL_IPV4_BYTE(ipV4.ipV4, 1), SL_IPV4_BYTE(ipV4.ipV4, 0),
SL_IPV4_BYTE(ipV4.ipV4Mask, 3), SL_IPV4_BYTE(ipV4.ipV4Mask, 2),
SL_IPV4_BYTE(ipV4.ipV4Mask, 1), SL_IPV4_BYTE(ipV4.ipV4Mask, 0),
SL_IPV4_BYTE(ipV4.ipV4Gateway, 3),
SL_IPV4_BYTE(ipV4.ipV4Gateway, 2),
SL_IPV4_BYTE(ipV4.ipV4Gateway, 1),
SL_IPV4_BYTE(ipV4.ipV4Gateway, 0),
SL_IPV4_BYTE(ipV4.ipV4DnsServer, 3),
SL_IPV4_BYTE(ipV4.ipV4DnsServer, 2),
SL_IPV4_BYTE(ipV4.ipV4DnsServer, 1),
SL_IPV4_BYTE(ipV4.ipV4DnsServer, 0));
if (lRetVal < 0) {
UART_PRINT("Failed to get network configuration \n\r");
LOOP_FOREVER()
;
}
UART_PRINT("Connect a client to Device\n\r");
while (!IS_IP_LEASED(g_ulStatus)) {
//wating for the client to connect
}
UART_PRINT("Client is connected to Device\n\r");
////////////////使用TCP传输,端口
lRetVal = BsdTcpServer(5001);
if (lRetVal < 0) {
UART_PRINT("TCP Service failed \n\r");
}
// revert to STA mode
lRetVal = sl_WlanSetMode(ROLE_STA);
if (lRetVal < 0) {
ERR_PRINT(lRetVal);
LOOP_FOREVER()
;
}
lRetVal = osi_TaskCreate(ReceiveTask, (const signed char*) "RECRIVETASK",
OSI_STACK_SIZE, NULL, 12, NULL);
if (lRetVal < 0) {
ERR_PRINT(lRetVal);
LOOP_FOREVER()
;
}
}
Yonghua Pan:
从你的代码里面我没有看到有去调用sl_Recv接收数据包啊。
liu_zheng:
回复 Yonghua Pan:
void ReceiveTask(void *pvparameters) { char cRxBuf[100]; int iStatus; UART_PRINT("ReceiveTask启动了\r\n"); while (1) { iStatus = sl_Recv(iNewSockID, cRxBuf, 64, 0); if (iStatus > 0) { //if (cRxBuf[iStatus – 2] == 0x0d) { cRxBuf[iStatus] = 0; UART_PRINT("\n\r接收inReceive:"); Message(cRxBuf); sl_Send(iNewSockID, cRxBuf, sizeof(cRxBuf), 0); mem_set(cRxBuf, 0, 64); } //else { UART_PRINT("\n\r接收数据完成\n\r"); iFlag = 0; } } osi_Sleep(1000); }}