This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC3235SF: 关于如何获取socket发送缓冲区的大小问题

Part Number: CC3235SF

请问我想要获取TCP-client socket端口的发送缓冲区应该怎么获取呢?如何修改发送缓冲区的大小?关于我使用MQTT-client例程来进行发送数据到上位机,但是我发现在使用sl_send()函数的时候出现了返回-11的情况,而且经过测试,十几分钟约出现丢包四五次的情况,这种情况应该如何解决呢?

以下是关于TCP socket的相关接口函数,请问这样编写代码是否有问题呢?数据发往上位机是通过调用tcp_client_send()来执行。

//****************************************************************************
int32_t tcp_client_disconnect(void)
{
    _i16 status=0;

    if (!is_connect)
    {
        //UART_PRINT("tcp_connect_close, error, tcp_client_socket < 0");
        return -1;
    }

    UART_PRINT("tcp_connect_close, start close\n\r");

    is_connect = FALSE;
    /* Calling 'close' with the socket descriptor,
    * once operation is finished. */
    status = sl_Close(tcp_client_socket);
    tcp_client_socket = -1;
    ASSERT_ON_ERROR(status);

    UART_PRINT("tcp_connect_close,  close OK\n\r");
    return 0;
}

int32_t tcp_client_connect( void)
{
    SlSockAddr_t *sa;
    sockAddr_t sAddr;
    int32_t addrSize;
    int32_t status;
    int32_t nonBlocking;
    ip_t ip_addr;
    int32_t timeout;

    ip_addr.ipv4 = wifi_config.server_ip;
    uint16_t portNumber = wifi_config.server_port;

    if (is_connect)
    {
        //UART_PRINT("tcp is already connected\n\r");
        return -1;
    }

    UART_PRINT("tcp_connect_start, start connect\n\r");

    /* filling the TCP server socket address */
    sAddr.in4.sin_family = SL_AF_INET;

    /* Since this is the client's side,
    * we must know beforehand the IP address
    * and the port of the server wer'e trying to connect.
    */
    sAddr.in4.sin_port = sl_Htons((unsigned short)portNumber);
    sAddr.in4.sin_addr.s_addr = sl_Htonl((unsigned int)ip_addr.ipv4);

    sa = (SlSockAddr_t*)&sAddr.in4;
    addrSize = sizeof(SlSockAddrIn_t);

    /* Get socket descriptor - this would be the
    * socket descriptor for the TCP session.
    */
    tcp_client_socket = sl_Socket(sa->sa_family, SL_SOCK_STREAM, TCP_PROTOCOL_FLAGS);
    ASSERT_ON_ERROR(tcp_client_socket);


    nonBlocking = TRUE;
    //timeout=30;
    status = sl_SetSockOpt(tcp_client_socket, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &nonBlocking, sizeof(nonBlocking));
               
    if(status < 0)
    {
        UART_PRINT("[line:%d, error:%d], socket = %d\n\r", __LINE__, status, tcp_client_socket);
        usleep(1000000);
        sl_Close(tcp_client_socket);
        tcp_client_socket = -1;

        return(-1);
    }

     status = -1;

    /* Calling 'sl_Connect' followed by server's
    * 'sl_Accept' would start session with
    * the TCP server. */
    while(status < 0)
    {
        status = sl_Connect(tcp_client_socket, sa, addrSize);
        if(status == SL_ERROR_BSD_EALREADY)
        {
            usleep(1000000);
            continue;
        }
        else if(status < 0)
        {
            UART_PRINT("[line:%d, error:%d], socket = %d\n\r", __LINE__, status, tcp_client_socket);
            usleep(100000);
            sl_Close(tcp_client_socket);
            tcp_client_socket = -1;
            return(-1);
        }
    }

    is_connect = TRUE;
    UART_PRINT("tcp_connect_start, connect OK,tcp_client_socket = %d\n\r ", tcp_client_socket);
    return 0;
}

extern mqd_t appQueue ;
uint8_t n=0;
int32_t tcp_client_send(uint8_t* data_buf, uint16_t buf_len)
{

    _u16 temp=0;
    int32_t status=0;
    //UART_PRINT("tcp_client_send, send start\n\r");

    if (!is_connect)
    {
        UART_PRINT("tcp is disconnected\n\r");
        return -1;
    }

    /* Send packets to the server */
    status = sl_Send(tcp_client_socket, data_buf, buf_len, 0);  //返回的是buf_len
//    UART_PRINT("MinTxPayloadSize = %d\r\n",g_pCB->FlowContCB.MinTxPayloadSize);
//    UART_PRINT("TxPoolCnt = %d\r\n",g_pCB->FlowContCB.TxPoolCnt);
    while(status != buf_len)    //死循环
    {
        n++;
        status = sl_Send(tcp_client_socket, data_buf, buf_len, 0);
        if(n==5)
        {
           // usleep(5);//需不需要呢?
            n=0;
            status = sl_Send(tcp_client_socket, data_buf, buf_len, 0);
            if(status != buf_len)
                break;
        }

    }
    if(status == SL_ERROR_BSD_EAGAIN)
    {
        status = sl_Send(tcp_client_socket, data_buf, buf_len, 0);
    }
    else if(status < 0)
    {
        UART_PRINT("[line:%d, error:%d]\n\r", __LINE__, status);
        usleep(100000);
        tcp_client_disconnect();
        return(-1);
    }
    else
    {
       // UART_PRINT("Tcp client Sent %u packets successfully\n\r", status);
    }

    return 0;
}

int32_t tcp_client_receive(uint8_t* data_buf, uint16_t* receive_len)
{
    int32_t status;


    //UART_PRINT("tcp_client_receive, receive start\n\r");

    if (!is_connect)
    {
        //UART_PRINT("tcp is disconnected\n\r");
        return -1;
    }

    status = sl_Recv(tcp_client_socket, data_buf, MAX_BUF_SIZE, 0);
    if(status == SL_ERROR_BSD_EAGAIN)
    {
    }
    else if(status < 0)
    {
        UART_PRINT("[line:%d, error:%d]\n\r", __LINE__, status);
        usleep(100000);
        tcp_client_disconnect();
        return(-1);
    }
    else if(status == 0)
    {
        UART_PRINT("TCP Server closed the connection\n\r");
        usleep(100000);
        tcp_client_disconnect();
        return(-1);
    }
    else
    {
        //UART_PRINT("Tcp client Receive %u packets successfully\n\r", status);
    }

    *receive_len = (uint16_t)status;
    return 0;
}

bool tcp_is_connected(void)
{
    return is_connect;
}

还有就是,为什么我DEBUG的时候g_pCB->FlowContCB.MinTxPayloadSize是有值的,但是打印的时候却全是0呢?

UART_PRINT("MinTxPayloadSize = %d\r\n",g_pCB->FlowContCB.MinTxPayloadSize);

请尽快给与解答,急~谢谢