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

📄 tcpserv.c

📁 nucleus source 源码 全部源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/***************************************************************************/
/*                                                                         */
/*    TCPSERV.C                                                            */
/*                                                                         */
/* DESCRIPTION                                                             */
/*                                                                         */
/*      Example TCP server program using PPP over a dialup connection.     */
/*                                                                         */
/***************************************************************************/

/* Include */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "net\inc\externs.h"
#include "net\inc\socketd.h"              /* socket interface structures */
#include "net\inc\tcpdefs.h"
#include "plus\nucleus.h"
#include "ppp\inc\ppp.h"                                /* PPP interface */

#define printf               PRINTF
#define ECHO_LENGTH          1500
#define DEMO_MAX_CONNECTIONS 10

/* Define Application data structures.  */
NU_MEMORY_POOL System_Memory;
NU_MEMORY_POOL Noncached_Memory;
NU_TASK        tcp_server_task_ptr;
NU_TASK        StrEcho0;
NU_TASK        StrEcho1;
NU_TASK        control_task_ptr;
NU_QUEUE       socketQueue;
NU_DEVICE      devices[1];
UINT8          serv_ip_addr[4];
UINT8          cli_ip_addr[4];

/* Define prototypes for function references.  */
void TCP_Server_Task(UNSIGNED argc, VOID *argv);
void str_echo(UNSIGNED argc, VOID *argv);
void Controller(UNSIGNED argc, VOID *argv);
void DEMO_Exit(INT exit_code);
void PRINTF(char*, ...);

extern STATUS  PPP_Initialize(DV_DEVICE_ENTRY *device);

char   line[ECHO_LENGTH + 1];
extern char __NOCACHE_END[];
extern char __NOCACHE_START[];

/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      Application_Initialize                                           */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      Define the Application_Initialize routine that determines the    */
/*      initial Nucleus PLUS application environment.                    */
/*                                                                       */
/*************************************************************************/
void    Application_Initialize(void *first_available_memory)
{

    VOID           *pointer;
    STATUS         status;
    unsigned long  nocacheSize = (((unsigned long)(__NOCACHE_END)) -
                                 ((unsigned long)(__NOCACHE_START)));

    /* Create a system memory pool that will be used to allocate task stacks,
       queue areas, etc.  */
    status = NU_Create_Memory_Pool(&System_Memory, "SYSMEM",
                                   first_available_memory, 400000, 50, NU_FIFO);

    if (status != NU_SUCCESS)
    {
        printf ("Can not create the System Memory Pool.\n");
        DEMO_Exit (1);
    }

    /* Create a memory pool from the non-cached memory.  This will be used
       by the TCP/IP stack and the PPP driver. */
    status = NU_Create_Memory_Pool(&Noncached_Memory, "NOCACHE",
                                 (void*)(__NOCACHE_START), nocacheSize,
                                 8, NU_FIFO);

    if (status != NU_SUCCESS)
    {
        printf ("Can not create the Non-Cached Memory Pool.\n");
        DEMO_Exit (2);
    }

    /* Allocate stack space for and create each task in the system.  */
    
    /* Create TCP_Server_Task.  */
    status = NU_Allocate_Memory(&System_Memory, &pointer, 2000, NU_NO_SUSPEND);
    if (status != NU_SUCCESS)
    {
        printf ("Can not create memory for TCP_Server_Task.\n");
        DEMO_Exit (3);
    }

    status = NU_Create_Task(&tcp_server_task_ptr, "TCPSERV", TCP_Server_Task,
                            0, NU_NULL, pointer, 2000, 3, 0, NU_PREEMPT, NU_START);
    if (status != NU_SUCCESS)
    {
        printf ("Cannot create TCP_Server_Task\n");
        DEMO_Exit (4);
    }

    status = NU_Allocate_Memory(&System_Memory, &pointer, 2000, NU_NO_SUSPEND);
    if (status != NU_SUCCESS)
    {
        printf ("Can not create memory for strecho task 0.\n");
        DEMO_Exit (5);
    }

    status = NU_Create_Task(&StrEcho0, "strecho", str_echo, 0, NU_NULL,
                            pointer, 2000, 3, 0, NU_PREEMPT, NU_START);
    if (status != NU_SUCCESS)
    {
        printf ("Cannot create strecho task 0\n");
        DEMO_Exit (6);
    }

    status = NU_Allocate_Memory(&System_Memory, &pointer, 2000, NU_NO_SUSPEND);
    if (status != NU_SUCCESS)
    {
        printf ("Can not create memory for strecho task 1.\n");
        DEMO_Exit (7);
    }

    status = NU_Create_Task(&StrEcho1, "strecho", str_echo, 0, NU_NULL,
                            pointer, 2000, 3, 0, NU_PREEMPT, NU_START);
    if (status != NU_SUCCESS)
    {
        printf ("Cannot create strecho task 1\n");
        DEMO_Exit (8);
    }

    /* Create Socket queue.  */
    status = NU_Allocate_Memory(&System_Memory, &pointer, 10*sizeof(UNSIGNED),
                                NU_NO_SUSPEND);
    if (status != NU_SUCCESS)
    {
        printf ("Can not create memory for SocQueue.\n");
        DEMO_Exit (9);
    }

    status = NU_Create_Queue(&socketQueue, "SocQueue", pointer, 10, NU_FIXED_SIZE,
                             1, NU_FIFO);
    if (status != NU_SUCCESS)
    {
        printf ("Can not create Socket Queue.\n");
        DEMO_Exit (10);
    }

    /* Create the modem controller task */
    status = NU_Allocate_Memory(&System_Memory, &pointer, 2000, NU_NO_SUSPEND);
    if (status != NU_SUCCESS)
    {
        printf("Could not allocte memory for Controller task.\n");
        DEMO_Exit(11);
    }
    
    status = NU_Create_Task(&control_task_ptr, "control", Controller, 0,
                            NU_NULL, pointer, 2000, 10, 0, NU_PREEMPT,
                            NU_START);
    if (status != NU_SUCCESS)
    {
        printf("Could not create Controller task.\n");
        DEMO_Exit(12);
    }


}   /* end Application_Initialize */

/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      TCP_Server_Task                                                  */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function creates a task that will accept connections from   */
/*      clients.  The socket descriptor for the connection is then placed*/
/*      on the queue.  The StrEcho task will then remove the Socket      */
/*      Descriptor from the queue.                                       */
/*                                                                       */
/*************************************************************************/
void TCP_Server_Task(UNSIGNED argc, VOID *argv)
{    
    INT                 socketd, newsock;       /* the socket descriptor */
    struct addr_struct  *servaddr; /* holds the server address structure */
    unsigned int        i;
    VOID                *pointer;
    STATUS              status;
    struct addr_struct  client_addr;
    CHAR                null_ip_addr[] = {0,0,0,0};
    CHAR                subnet[] = {255,255,255,0};

    /* Replace these assignments with valid IP addresses */
    serv_ip_addr[0] = 100;
    serv_ip_addr[1] = 200;
    serv_ip_addr[2] = 200;
    serv_ip_addr[3] = 1;

    cli_ip_addr[0] = 100;
    cli_ip_addr[1] = 200;
    cli_ip_addr[2] = 200;
    cli_ip_addr[3] = 2;

    /*  Remove compilation warnings for unused parameters.  */
    status = (STATUS) argc + (STATUS) argv;

    /* call network initialization */
    if (NU_Init_Net(&Noncached_Memory) != NU_SUCCESS)
    {
        printf("NU_Init_Net failed.\n");
        DEMO_Exit(13);

⌨️ 快捷键说明

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