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.

例程tcp-socket的一些问题

Other Parts Discussed in Thread: CC3200

1.改写在客户端模式下主动向服务器发送数据用哪个函数

2.例程上说客户端模式下发1000包数据后退出   是指1000次消息么?

3.#ifdef USER_INPUT_ENABLE  这句代码是什么意思   主函数没有找到 其他的USER_INPUT_ENABLE 关键词

希望TI论坛的技术大大帮我解释下 谢谢了。 

  • // sending packet
    iStatus = sl_Send(iSockID, g_cBsdBuf, sTestBufLen, 0 ); 发送函数

    1000指的是1000包数据

    程序部分了做备注,参考

    //****************************************************************************
    //
    //! \brief Opening a TCP client side socket and sending data
    //!
    //! This function opens a TCP socket and tries to connect to a Server IP_ADDR
    //! waiting on port PORT_NUM.
    //! If the socket connection is successful then the function will send 1000
    //! TCP packets to the server.
    //!
    //! \param[in] port number on which the server will be listening on
    //!
    //! \return 0 on success, -1 on Error.
    //
    //****************************************************************************
    int BsdTcpClient(unsigned short usPort)
    {
    int iCounter;
    short sTestBufLen;
    SlSockAddrIn_t sAddr;
    int iAddrSize;
    int iSockID;
    int iStatus;
    long lLoopCount = 0;

    // filling the buffer
    for (iCounter=0 ; iCounter<BUF_SIZE ; iCounter++)
    {
    g_cBsdBuf[iCounter] = (char)(iCounter % 10);
    }

    sTestBufLen = BUF_SIZE;

    //filling the TCP server socket address
    sAddr.sin_family = SL_AF_INET;
    sAddr.sin_port = sl_Htons((unsigned short)usPort); //目的IP地址端口号
    sAddr.sin_addr.s_addr = sl_Htonl((unsigned int)g_ulDestinationIp); //目的IP地址IP_ADDR-手机上连接USR软件后配置TCP Server设置端口号并查看本机IP地址

    iAddrSize = sizeof(SlSockAddrIn_t);

    // creating a TCP socket
    iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
    if( iSockID < 0 )
    {
    ASSERT_ON_ERROR(SOCKET_CREATE_ERROR);
    }

    // connecting to TCP server
    iStatus = sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize); //<连接>目的IP地址及端口号
    if( iStatus < 0 )
    {
    // error
    sl_Close(iSockID);
    ASSERT_ON_ERROR(CONNECT_ERROR);
    }

    // sending multiple packets to the TCP server
    while (lLoopCount < g_ulPacketCount)
    {
    // sending packet
    iStatus = sl_Send(iSockID, g_cBsdBuf, sTestBufLen, 0 );
    if( iStatus < 0 )
    {
    // error
    sl_Close(iSockID);
    ASSERT_ON_ERROR(SEND_ERROR);
    }
    lLoopCount++;
    }

    Report("Sent %u packets successfully\n\r",g_ulPacketCount);

    iStatus = sl_Close(iSockID);
    //closing the socket after sending 1000 packets
    ASSERT_ON_ERROR(iStatus);

    return SUCCESS;
    }

    //****************************************************************************
    //
    //! \brief Opening a TCP server side socket and receiving data
    //!
    //! This function opens a TCP socket in Listen mode and waits for an incoming
    //! TCP connection.
    //! If a socket connection is established then the function will try to read
    //! 1000 TCP packets from the connected client.
    //!
    //! \param[in] port number on which the server will be listening on
    //!
    //! \return 0 on success, -1 on error.
    //!
    //! \note This function will wait for an incoming connection till
    //! one is established
    //
    //****************************************************************************
    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;

    // filling the buffer
    for (iCounter=0 ; iCounter<BUF_SIZE ; iCounter++)
    {
    g_cBsdBuf[iCounter] = (char)(iCounter % 10);
    }

    iTestBufLen = BUF_SIZE;

    //filling the TCP server socket address
    sLocalAddr.sin_family = SL_AF_INET;
    sLocalAddr.sin_port = sl_Htons((unsigned short)usPort); //仅用到本机IP的端口号即可
    sLocalAddr.sin_addr.s_addr = 0; //建立TCP服务器时忽略本机IP地址

    // 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); //绑定该TCP socket
    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); //开始监听TCP连接
    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);
    }
    }

    // waits for 1000 packets from the connected TCP client
    while (lLoopCount < g_ulPacketCount)
    {
    iStatus = sl_Recv(iNewSockID, g_cBsdBuf, iTestBufLen, 0);
    if( iStatus <= 0 )
    {
    // error
    sl_Close(iNewSockID);
    sl_Close(iSockID);
    ASSERT_ON_ERROR(RECV_ERROR);
    }

    lLoopCount++;
    }

    Report("Recieved %u packets successfully\n\r",g_ulPacketCount);

    // 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;
    }

    //****************************************************************************
    //
    //! \brief Connecting to a WLAN Accesspoint
    //!
    //! This function connects to the required AP (SSID_NAME) with Security
    //! parameters specified in te form of macros at the top of this file
    //!
    //! \param[in] None
    //!
    //! \return Status value
    //!
    //! \warning If the WLAN connection fails or we don't aquire an IP
    //! address, It will be stuck in this function forever.
    //
    //****************************************************************************
    static long WlanConnect()
    {
    SlSecParams_t secParams = {0};
    long lRetVal = 0;

    secParams.Key = (signed char*)SECURITY_KEY;
    secParams.KeyLen = strlen(SECURITY_KEY);
    secParams.Type = SECURITY_TYPE;

    lRetVal = sl_WlanConnect((signed char*)SSID_NAME, strlen(SSID_NAME), 0, &secParams, 0);
    ASSERT_ON_ERROR(lRetVal);

    /* Wait */
    while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus))) //还是要在这等待网络事件产生!
    {
    // Wait for WLAN Event
    #ifndef SL_PLATFORM_MULTI_THREADED
    _SlNonOsMainLoopTask();
    #endif
    }

    return SUCCESS;

    }

    //*****************************************************************************
    //
    //! Application startup display on UART
    //!
    //! \param none
    //!
    //! \return none
    //!
    //*****************************************************************************
    static void
    DisplayBanner(char * AppName)
    {

    Report("\n\n\n\r");
    Report("\t\t *************************************************\n\r");
    Report("\t\t CC3200 %s Application \n\r", AppName);
    Report("\t\t *************************************************\n\r");
    Report("\n\n\n\r");
    }

    //*****************************************************************************
    //
    //! Board Initialization & Configuration
    //!
    //! \param None
    //!
    //! \return None
    //
    //*****************************************************************************
    static void
    BoardInit(void)
    {
    /* In case of TI-RTOS vector table is initialize by OS itself */
    #ifndef USE_TIRTOS
    //
    // Set vector table base
    //
    #if defined(ccs) || defined (gcc)
    MAP_IntVTableBaseSet((unsigned long)&g_pfnVectors[0]);
    #endif
    #if defined(ewarm)
    MAP_IntVTableBaseSet((unsigned long)&__vector_table);
    #endif
    #endif
    //
    // Enable Processor
    //
    MAP_IntMasterEnable();
    MAP_IntEnable(FAULT_SYSTICK);

    PRCMCC3200MCUInit();
    }

    //****************************************************************************
    // MAIN FUNCTION
    //****************************************************************************
    void main()
    {
    long lRetVal = -1;

    //
    // Board Initialization
    //
    BoardInit();

    //
    // Initialize the uDMA
    //
    UDMAInit();

    //
    // Configure the pinmux settings for the peripherals exercised
    //
    PinMuxConfig();

    //
    // Configuring UART
    //
    InitTerm();

    //
    // Display banner
    //
    DisplayBanner(APPLICATION_NAME);
    InitializeAppVariables();

    //
    // Following function configure the device to default state by cleaning
    // the persistent settings stored in NVMEM (viz. connection profiles &
    // policies, power policy etc)
    //
    // Applications may choose to skip this step if the developer is sure
    // that the device is in its default state at start of applicaton
    //
    // Note that all profiles and persistent settings that were done on the
    // device will be lost
    //
    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(0, 0, 0);
    if (lRetVal < 0)
    {
    UART_PRINT("Failed to start the device \n\r");
    LOOP_FOREVER();
    }

    UART_PRINT("Device started as STATION \n\r");


    UART_PRINT("Connecting to AP: %s ...\r\n",SSID_NAME);

    // Connecting to WLAN AP - Set with static parameters defined at common.h
    // After this call we will be connected and have IP address
    lRetVal = WlanConnect();
    if(lRetVal < 0)
    {
    UART_PRINT("Connection to AP failed \n\r");
    LOOP_FOREVER();
    }

    UART_PRINT("Connected to AP: %s \n\r",SSID_NAME);

    UART_PRINT("Device IP: %d.%d.%d.%d\n\r\n\r",
    SL_IPV4_BYTE(g_ulIpAddr,3),
    SL_IPV4_BYTE(g_ulIpAddr,2),
    SL_IPV4_BYTE(g_ulIpAddr,1),
    SL_IPV4_BYTE(g_ulIpAddr,0));

    #ifdef USER_INPUT_ENABLE //定义了USER_INPUT_ENABLE
    lRetVal = UserInput();
    if(lRetVal < 0)
    {
    ERR_PRINT(lRetVal);
    LOOP_FOREVER();
    }
    #else //不执行
    lRetVal = BsdTcpClient(PORT_NUM); //建立CC3200-TCP Client并向目的IP及端口号5001发送TCP数据
    if(lRetVal < 0)
    {
    UART_PRINT("TCP Client failed\n\r");
    LOOP_FOREVER();
    }

    lRetVal = BsdTcpServer(PORT_NUM); //建立TCP Server 等待连接并接收数据
    if(lRetVal < 0)
    {
    UART_PRINT("TCP Server failed\n\r");
    LOOP_FOREVER();
    }
    #endif

    UART_PRINT("Exiting Application ...\n\r");

    //
    // power of the Network processor
    //
    lRetVal = sl_Stop(SL_STOP_TIMEOUT);

    while (1)
    {
    _SlNonOsMainLoopTask();
    }
    }

  • 谢谢你的回答!