微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 无线和射频 > TI WIFI设计交流 > 我将CC3000做服务端,为什么bind总是返回-1?

我将CC3000做服务端,为什么bind总是返回-1?

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

CC3000作为客户端的时候,是可以连接并传输数据的。

当我将CC3000做服务端,BIND总是返回-1;

调用socket,然后bind,结果返回-1,代码如下:

    INT32 fd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(fd < 0)
    {
        while(1);
    }
    sockaddr_in host_addr;
    host_addr.sin_family = AF_INET;
    host_addr.sin_port = my_htons(port);
    ipaddr_aton(ip,&host_addr.sin_addr);
    error = bind(fd3,(sockaddr*)&host_addr,sizeof(sockaddr));
    if(error < 0)
    {
          while(1);
    }

如果忽略bind,继续执行listen、accept,那么accept的时候返回-2。

请大家帮帮忙,实在没辙了。

ip地址设成0.

CC3000 only supports binding to its' local address, which is configured by using 0.0.0.0.

谢谢 TY 

现在可以bind 了,但是accept 的时候,返回-2;

cc3000 :  ip :192.168.43.32   : 4000

                   bind  0.0.0.0

                   linsten     10

                   accept     return  -2;

客户端: connect  192.168.43.32 :4000      失败

请问这是怎么回事?

返回-2的时候继续再调用accept,请参考下面的代码:

//*****************************************************************************
//
//! Initialize Connection Server
//!
//! \param none
//!
//! \return none
//!
//! \brief Waits for a connection where we will
//
//*****************************************************************************
void initServer(void)
{
char portStr[12];
int port;
memset(portStr,0,sizeof(portStr));

// Open Server Socket
serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (serverSocket == -1)
{
serverError(SERV_ERR_SOCKET);
wlan_stop();
while(1)
{
__no_operation();
}
}

// Open port
port = 30000;
serverSocketAddr.sa_family = AF_INET;

// Set the Port Number
serverSocketAddr.sa_data[0] = (port & 0xFF00)>> 8;
serverSocketAddr.sa_data[1] = (port & 0x00FF);

memset (&serverSocketAddr.sa_data[2], 0, 4);

if (bind(serverSocket, &serverSocketAddr, sizeof(sockaddr)) != 0)
{
serverError(SERV_ERR_BIND);
return;
}


// Start Listening
if (listen (serverSocket, 1) != 0)
{
serverError(SERV_ERR_LISTEN);
return;
}

setCC3000MachineState(CC3000_SERVER_INIT);
terminalPrint("Server Initialized on port ");
itoa(port, portStr, DECIMAL_BASE);
terminalPrint(portStr);
terminalPrint("\r\n");

}

void waitForConnection(void)
{
sockaddr clientaddr;
socklen_t addrlen;
volatile int bytesRecvd = 0;
volatile long bytesSent = 0;
int clientDescriptor = -1;
volatile long curSocket = 0;

int optval, optlen;
char clientIP[4];
char requestBuffer[SERVER_RECV_BUF_SIZE];
int i = 0;
int j = 0;
char return_buf[256] = "*************************************\r\nEnoch Geo(CC3000) has finished BLINKING!!r\n*************************************\r\n";

// Check whether the server functionality is successfully initialized
if(currentCC3000State() & CC3000_SERVER_INIT)
{
// Begin waiting for client and handle the connection
while(1)
{
#if 1
if (get_socket_active_status(serverSocket) == -1)
{
terminalPrint("socket closed\r\n");
closesocket(serverSocket);
break;
}
#endif
//
// Handle any un-solicited event if required - the function shall be triggered
// few times in a second
//
hci_unsolicited_event_handler();

addrlen = sizeof(clientaddr);

terminalPrint("Waiting for Clients\r\n");
// accept blocks until we receive a connection
while ( (clientDescriptor == -1) || (clientDescriptor == -2) )
{
clientDescriptor = accept(serverSocket, (sockaddr *) &clientaddr, &addrlen);
}
terminalPrint("accepted\r\n");

//
// Handle any un-solicited event if required - the function shall be triggered
// few times in a second
//
hci_unsolicited_event_handler();

...................

谢谢 Yonghua Pan

经过修改,现象如下:

cc3000 :  ip :192.168.43.32   : 8000(使用静态IP)

                   bind  0.0.0.0

                   linsten     1

                   accept()     return  -2,client addr ip:192.168.43.219,client port :49988;

客户端:    IP:192.168.43.219(动态ip)

                   connect  192.168.43.32 :8000     

当设置如下时

UINT32 optval = 0xFFFFFFFF;

setsockopt(sd,SOL_SOCKET,SOCKOPT_ACCEPT_NONBLOCK,(void*)&optval,4);

accept会一直阻塞。

如果不调用上面 setsocktopt ,则会返回 -2,while循环调用同样如此。在调试过程中,偶尔会接收到 client ip :192.168.43.219,端口号:49988,虽然IP地址是正确的,但是端口号不对,而且仍然返回 -2;

求解!

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

网站地图

Top