⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oobsrv.c

📁 闻停开发板视频程序
💻 C
字号:
//--------------------------------------------------------------------------
// IP Stack Server Demonstration Program
//--------------------------------------------------------------------------
// OOBSRV.C
//
// This program implements a TCP OOB data server, which listens on port
// 999, and initiates a TCPURG data test when a connection is made.
//
// Author: Michael A. Denio
// Copyright 1999, 2000 by Texas Instruments Inc.
//-------------------------------------------------------------------------
#include <netmain.h>

//
// TCP/UDP Echo Server
//
void oobsrv()
{
    SOCKET   stcp = INVALID_SOCKET;
    SOCKET   stcpactive = INVALID_SOCKET;
    struct   sockaddr_in sin1;
    char     buf[32];
    int      size,tmp;

    // Allocate the file environment for this task
    fdOpenSession( TaskSelf() );

    // Create the main TCP listen socket
    stcp = socket(AF_INET, SOCK_STREAMNC, IPPROTO_TCP);
    if( stcp == INVALID_SOCKET )
        goto leave;

    // Set Port = 999, leaving IP address = Any
    bzero( &sin1, sizeof(struct sockaddr_in) );
    sin1.sin_family = AF_INET;
    sin1.sin_len    = sizeof( sin1 );
    sin1.sin_port   = htons(999);

    // Bind socket
    if ( bind( stcp, (PSA) &sin1, sizeof(sin1) ) < 0 )
        goto leave;

    // Start listening
    if ( listen( stcp, 1) < 0 )
        goto leave;

    printf("OobSrv Initialized\n");

    // Run until task is destroyed by the system
    for(;;)
    {
        size = sizeof( sin1 );
        stcpactive = accept( stcp, (PSA)&sin1, &size );
        if( stcpactive == INVALID_SOCKET )
        	goto leave;

        //
        // OOB Data Test
        // Will send 10 bytes of data, 1 OOB, and then 10 more data
        // Client shoud read as
        //    10 bytes (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
        //    1  byte  (99)
        //    10 bytes (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
        //
        for(tmp=0; tmp<10; tmp++)
            buf[tmp] = tmp;
        buf[10] = 99;
        send( stcpactive, buf, 11, MSG_OOB );
        send( stcpactive, buf, 10, 0 );

        fdClose( stcpactive );
    }

leave:
    // We only get here on an error - close the sockets
    if( stcp != INVALID_SOCKET )
        fdClose( stcp );

    printf("OobSrv Fatal Error\n");

    // This task is killed by the system - here, we will block
    TaskBlock( TaskSelf() );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -