CC3220SF中bind() 对于建立多个socket一对一的和不同 port 进行绑定只能成功一个为什么?(代码如下:)
CC3220SF中bind() 对于建立多个socket一对一的和不同 port 进行绑定只能成功一个为什么?(代码如下:)
typedef struct udp_prama_t
{
_i16 Sd;
_i16 Status;
_i8 *SendBuf;
_i8 *RecvBuf;
_u32 ip_addr[4];
SlSockAddrIn_t Addr;
}udp_prama_t;
#define WIFI_UDP_SOCKET_NUM 3
#define WIFI_UDP_SEND_DATA_CR 2048
#define WIFI_UDP_RECV_DATA_CR 2048
udp_prama_t gudp_prama[WIFI_UDP_SOCKET_NUM];
//创造socket
_i16 udp_socket_creat(_i16 Sd)
{
Sd = sl_Socket(SL_AF_INET, SL_SOCK_DGRAM, SL_IPPROTO_UDP);
if( 0 > Sd )
{
return 0;
}else{
return 1;
}
}
void udp_socket_server_init(udp_prama_t *ggudp_prama,int32_t port)
{
SlSockNonblocking_t enableOption;
if(udp_socket_creat(ggudp_prama->Sd))
{
UART_PRINT("sl_Socket port = %d creat ok \n",port);
}else{
UART_PRINT("sl_Socket port = %d creat erro \n",port);
}
ggudp_prama->Addr.sin_family = SL_AF_INET;
ggudp_prama->Addr.sin_port = sl_Htons(port);
ggudp_prama->Addr.sin_addr.s_addr = sl_Htonl(SL_INADDR_ANY);
enableOption.NonBlockingEnabled = 1;
sl_SetSockOpt(ggudp_prama->Sd,SL_SOL_SOCKET,SL_SO_NONBLOCKING, (_u8 *)&enableOption,sizeof(enableOption)); // Enable/disable nonblocking mode
//sl_Bind
if( sl_Bind(ggudp_prama->Sd, ( SlSockAddr_t *)&ggudp_prama->Addr, sizeof(SlSockAddrIn_t)) )
{
// error
UART_PRINT("sl_Socket sl_Bind port = %d erro \n",port);
}else{
UART_PRINT("sl_Socket sl_Bind port = %d OK \n",port);
}
ggudp_prama->SendBuf = (_i8 *)sl_Malloc(WIFI_UDP_SEND_DATA_CR);
if(ggudp_prama->SendBuf == NULL)
{
UART_PRINT("sl_Socket ggudp_prama->SendBuf sl_Malloc creat erro\n");
}else{
sl_Memset(ggudp_prama->SendBuf,1,WIFI_UDP_SEND_DATA_CR);
sl_Memcpy(ggudp_prama->SendBuf,"hello world!!!!",strlen("hello world!!!!"));
}
ggudp_prama->RecvBuf = (_i8 *)sl_Malloc(WIFI_UDP_RECV_DATA_CR);
if(ggudp_prama->RecvBuf == NULL)
{
UART_PRINT("sl_Socket ggudp_prama->RecvBuf sl_Malloc creat erro\n");
}else{
sl_Memset(ggudp_prama->RecvBuf,0,WIFI_UDP_RECV_DATA_CR);
}
}
//udp发送函数
void udp_server_recv_and_send(void)
{
int16_t i;
_i16 AddrSize = sizeof(SlSockAddrIn_t);
for(i=0;i<WIFI_UDP_SOCKET_NUM;i++)
{
udp_socket_server_init(&gudp_prama[i],5001+i);
}
/* Wait to check if connection to desire AP succeeds */
while (!(IS_CONNECTED(app_CB.Status)) && !(IS_IP_ACQUIRED(app_CB.Status)))
{
sleep(1);
}
while(1)
{
for(i=0;i<WIFI_UDP_SOCKET_NUM;i++)
{
if(gudp_prama[i].Sd >= 0)
{
gudp_prama[i].Status = sl_RecvFrom(gudp_prama[i].Sd, gudp_prama[i].RecvBuf, WIFI_UDP_RECV_DATA_CR, 0, ( SlSockAddr_t *)&gudp_prama[i].Addr, &AddrSize);
if( 0 > gudp_prama[i].Status )
{
// error
// UART_PRINT("sl_Socket sin_port = %d send erro \n",5001+i);
}
else
{
if(gudp_prama[i].Status > 0)
{
UART_PRINT("\n sl_Socket sin_port = %d recv datalen = %d data = %s \n",5001+i,gudp_prama[i].Status,gudp_prama[i].RecvBuf);
gudp_prama[i].Status = sl_SendTo(gudp_prama[i].Sd, gudp_prama[i].SendBuf, WIFI_UDP_SEND_DATA_CR, 0, (SlSockAddr_t*)&gudp_prama[i].Addr,sizeof(SlSockAddr_t));
if( strlen(gudp_prama[i].SendBuf) != gudp_prama[i].Status )
{
// error
UART_PRINT("sl_Socket sin_port = %d send erro \n",5001+i);
}else{
UART_PRINT("sl_Socket sin_port = %d send datalen = %d OK \n",5001+i,gudp_prama[i].Status);
}
}
}
}
}
}
}
socket 建立函数,参数传输代码错误