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

📄 tcpserv.c

📁 nucleus source 源码 全部源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/***************************************************************************/
/*                                                                         */
/*    TCPSERV.C                                                            */
/*                                                                         */
/* DESCRIPTION                                                             */
/*                                                                         */
/*      Example TCP server program using PPP over a null modem 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"

#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_QUEUE          socketQueue;

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

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 Ethernet 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);
    }

}   /* 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;
    NU_DEVICE           devices[1];
    DV_DEVICE_ENTRY     *dev_ptr;
    UINT8               serv_ip_addr[4];
    UINT8               cli_ip_addr[4];
    CHAR                subnet[] = {255,255,255,0};
    CHAR                null_ip_addr[] = {0,0,0,0};
    CHAR                mstring[80];

    /* 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(11);
    }

    devices[0].dv_name = "PPP_0";
    devices[0].dv_init = PPP_Initialize;
    devices[0].dv_flags = (DV_POINTTOPOINT | DV_NOARP);
    devices[0].dv_hw.uart.com_port = SMC1;
    devices[0].dv_hw.uart.baud_rate  = 115200;
    memcpy (devices[0].dv_ip_addr, null_ip_addr, 4); /* Not use by PPP. */
    memcpy (devices[0].dv_subnet_mask, subnet, 4);   /* Not use by PPP. */

    if (NU_Init_Devices(devices, 1) != NU_SUCCESS)
    {
        printf("NU_Init_Devices failed.\n");
        DEMO_Exit(12);
    }

    NU_Change_Communication_Mode(MDM_TERMINAL_COMMUNICATION, devices[0].dv_name);

    /* This while loop simply receives strings from the serial port and echos
       back OK in response.  This code is used to trick Windows into thinking
       that it is communicating with modem, when in fact it is connected via
       null modem to an embedded device. */
    do
    {
        /* Receive a MODEM command. */
        DEMO_Get_Modem_String(mstring, devices[0].dv_name);

        /* Respond with OK. */
        NU_Modem_Control_String("OK\n\r", devices[0].dv_name);

        /* If the command received was a command to dial then Windows 95 now
           thinks that it has a modem connection to a remote HOST.  Get out of
           this loop because data exchanged beyond this point will be in the
           form of IP packets. */
    } while (strncmp(mstring, "ATDT", 4) != 0);

    /* Act like a modem. */
    sprintf (mstring, "CONNECT %d\n\r", devices[0].dv_hw.uart.baud_rate);
    NU_Modem_Control_String(mstring, devices[0].dv_name);

    /* Get a pointer to the device structure. Only used in null modem

⌨️ 快捷键说明

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