CC3200 TCP socket编程

来源:互联网 发布:淘宝店中国制造 编辑:程序博客网 时间:2024/06/05 08:29

TCP,UDP套接字流程图
对于TCP,服务器先创建TCP套接字sl_Socket,并将其绑定在TCP服务器地址sl_Bind,然后指定端口监听sl_Listen,并接受TCP连接sl_Accept,一单建立起TCP连接,就能进行数据接受sl_Recv和数据传送sl_Send,最后关闭TCP套接字sl_Close。
对于UDP,不需要监听和接受连接,创建并绑定好套接字后就可以通信,所以这是一种不可靠的连接。不过udp套接字的应用简单很多。
套接字的应用程序可以使用操作系统运行,也可以不使用操作系统运行。不使用操作系统时,通常调用sl_SetSocketOpt()设置为非阻塞模式,而使用操作系统,由于多线程可使用阻塞模式。
下面在程序中体现

//****************************************************************************////! \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);    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);        }    }    // 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;}

分析:
创建TCP套接字 sl_Socket()
绑定TCP套接字
监听TCP套接字
设置非阻塞模式
接受TCP连接
接受TCP数据包
关闭TCP套接字

iStatus = sl_Close(iSockID),关闭的iSockID包括sl_Accept() AND sl_Socket()的返回值。

BsdTcpClient()分析
//****************************************************************************////! \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);    sAddr.sin_addr.s_addr = sl_Htonl((unsigned int)g_ulDestinationIp);    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);    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;

分析:
创建TCP套接字 sl_Socket()
连接TCP服务器
发送TCP数据包
关闭TCP套接字

应用:无线UART程序设计 基于TCP
无线UART程序流程图

主要分析用TCP套接传送的字符

参考:ARM CORTEX-M4+WIFI MCU应用指南 CC3200 IAR 基础篇 郭书军

0 0
原创粉丝点击