udpserv.c

来自「基于nucleus操作系统的GPRS无线数据传输终端全套源文件。包括支持ARM7」· C语言 代码 · 共 297 行

C
297
字号
/*************************************************************************/
/*                                                                       */
/*        Copyright (c) 1999 Accelerated Technology, Inc.		 */
/*                                                                       */
/* PROPRIETARY RIGHTS of Accelerated Technology are involved in the      */
/* subject matter of this material.  All manufacturing, reproduction,    */
/* use, and sales rights pertaining to this subject matter are governed  */
/* by the license agreement.  The recipient of this software implicitly  */
/* accepts the terms of the license.                                     */
/*                                                                       */
/*************************************************************************/

/*****************************************************************************/
/*                                                                           */
/* FILE NAME                                                                 */
/*                                                                           */
/*      UDPSERV.C                                                            */
/*                                                                           */
/*                                                                           */
/* DESCRIPTION                                                               */
/*                                                                           */
/*      This is a simple example of a UDP server.  It is an echo server,     */
/*      that is, any data received is echoed back to the client.  This       */
/*      program demonstrates the use of Nucleus NET over a PPP connection    */
/*                                                                           */
/*****************************************************************************/
#include <string.h>

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

/* This is the IP address that will be assigned to a client during PPP
   negotiaton. */
UINT8   client_ip_address[] =  {192, 106, 34, 25};

/* This is the IP address the server will use. */
UINT8   server_ip_address[] =  {192, 106, 34, 50};

/* Define Application data structures.  */
NU_MEMORY_POOL    System_Memory;
NU_TASK           udp_server_task_ptr;
NU_TASK           Control_Task;
NU_DEVICE         devices[1];

/* Define prototypes for function references.  */
VOID    UDP_server_task(UNSIGNED argc, VOID *argv);
VOID    Controller (UNSIGNED argc, VOID *argv);
VOID    Error_Loop (INT error_num);


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      Application_Initialize                                           */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      Application_Initialize performs any tasks that must be done      */
/*      before the first task executes.                                  */
/*                                                                       */
/*************************************************************************/
VOID Application_Initialize (VOID *first_available_memory)
{
    VOID *pointer;
    STATUS status;

    /* 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, 270000, 50, NU_FIFO);

    if (status != NU_SUCCESS)
    {
        Error_Loop (1);
    }

    /* Create each task in the system.  */

    /* Create UDP_server_task.  */
    status = NU_Allocate_Memory (&System_Memory, &pointer, 2000, NU_NO_SUSPEND);
    if (status != NU_SUCCESS)
    {
        Error_Loop (2);
    }

    status = NU_Create_Task (&udp_server_task_ptr, "UDPSERV", UDP_server_task, 0,
                             NU_NULL, pointer, 2000, 3, 0, NU_PREEMPT,
                             NU_START);
    if (status != NU_SUCCESS)
    {
        Error_Loop (3);
    }

    /* Create the modem controller task */
    status = NU_Allocate_Memory(&System_Memory, &pointer, 2000, NU_NO_SUSPEND);
    if (status != NU_SUCCESS)
    {
        Error_Loop (4);
    }
    
    status = NU_Create_Task(&Control_Task, "control", Controller, 0, NU_NULL,
				pointer, 2000, 10, 0, NU_PREEMPT, NU_START);
    if (status != NU_SUCCESS)
    {
        Error_Loop (5);
    }

}  /* end Application_Initialize */


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      udp_server_task                                                  */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This is the task entry function for the task that will accept    */
/*      connection requests from clients.  Whenever a new connection is  */
/*      accepted the socket descriptor is pushed onto the Socket         */
/*      Descriptor queue.  The echo task will perform the actual         */
/*      communication with the client.                                   */
/*                                                                       */
/*************************************************************************/
VOID UDP_server_task(UNSIGNED argc, VOID *argv)
{
    INT                 socketd;    /* the socket descriptor */
    struct addr_struct  servaddr;   /* holds the server address structure */
    struct addr_struct  cliaddr;    /* holds the client address structure */
    STATUS              status;
    INT                 bytes_received;
    INT                 bytes_sent;
    CHAR                *buffer;
    INT16               clilen;
    UINT8               null_ip []    = {0, 0, 0, 0};   /* not used by PPP */
    CHAR                subnet_mask[] = {255, 255, 255, 0};



    if(NU_Init_Net(&System_Memory))  /* call network initialization */
    {
        Error_Loop (6);
    }

    memcpy (devices[0].dv_ip_addr, null_ip, 4);
    memcpy (devices[0].dv_subnet_mask, subnet_mask, 4);
    devices[0].dv_name = "PPP_Link";
    devices[0].dv_init = PPP_Initialize;
    devices[0].dv_flags = (DV_POINTTOPOINT | DV_NOARP);
    
    devices[0].dv_hw.uart.com_port         = COM2;
    devices[0].dv_hw.uart.baud_rate        = 38400;
    devices[0].dv_hw.uart.parity           = PARITY_NONE;
    devices[0].dv_hw.uart.stop_bits        = STOP_BITS_1;
    devices[0].dv_hw.uart.data_bits        = DATA_BITS_8;

    DEV_Init_Devices(devices, 1);

    /*  Allocate space for the buffer.  */
    status = NU_Allocate_Memory (&System_Memory, (VOID **) &buffer, 2000,
                                    NU_SUSPEND);

    if (status != NU_SUCCESS)
    {
        /*  Can't allocate memory, get out.  */
        Error_Loop (7);
    }


    /* Loop forever accepting client connections. */
    while (1)
    {

        /* open a connection via the socket interface */
        if ((socketd = NU_Socket(NU_FAMILY_IP, NU_TYPE_DGRAM, 0))>=0)
        {

            /* Fill in a structure with the server address. */
            servaddr.family    = NU_FAMILY_IP;
            servaddr.port      = 7;
            servaddr.name = "ati";
            *((UINT32 *)servaddr.id.is_ip_addrs) = IP_ADDR_ANY;

            /* Initialize the client address structure. */
            cliaddr.family    = NU_FAMILY_IP;
            cliaddr.port      = 0;
            cliaddr.id.is_ip_addrs[0]  = (unsigned char) 0;
            cliaddr.id.is_ip_addrs[1]  = (unsigned char) 0;
            cliaddr.id.is_ip_addrs[2]  = (unsigned char) 0;
            cliaddr.id.is_ip_addrs[3]  = (unsigned char) 0;
            cliaddr.name = "";

            /*  Bind our address to the socket.  */
            if (NU_Bind(socketd, &servaddr, 0) < 0)
            {
                Error_Loop (8);
            }

            /*  Prime the loop. */
            buffer[0] = (char) 0;

            while(buffer[0] != 'q')
            {
                /*  Get a string from the client.  */
                bytes_received = NU_Recv_From(socketd, buffer, 1000, 0,
                                          &cliaddr, &clilen);

                /*  If we got back less than zero there is a bad error. */
                if (bytes_received < 0)
                {
                    Error_Loop (9);
                }
                else
                {
                    /*  NULL terminate the string.  */
                    buffer[bytes_received] = (char) 0;

                    /*  Send the string back to the client.  */
                    bytes_sent = NU_Send_To(socketd, buffer, bytes_received, 0,
                                    &cliaddr, clilen);

                    /*  If the return value is less than zero an error ocurred. */
                    if (bytes_sent < 0)
                    {
                        Error_Loop (10);
                    }
                }
            }
            /* close the connection */
            if ((NU_Close_Socket(socketd)) != NU_SUCCESS)
            {
                Error_Loop (11);
            }

        } /* end successful NU_Socket */

        /* Let the link close down */
        NU_Sleep (10);

    } /* end while */

}

/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*		Controller						 */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*	   This function waits for a caller. When a connection is made it*/
/*  waits until the connection is lost and then waits for another        */
/*  caller.                                                              */
/*                                                                       */
/*************************************************************************/
VOID Controller (UNSIGNED argc, VOID *argv)
{

	/* Modem Initialization string. */
    NU_Modem_Control_String("ATm0S0=0V1X4&K0^M", devices[0].dv_name);

    NU_Set_PPP_Client_IP_Address (client_ip_address, devices[0].dv_name);

    while (NU_TRUE)
	{
        /* Wait for slow modem */
        NU_Sleep (2);

        /* Wait for a client to call. */
        NU_Wait_For_PPP_Client(server_ip_address, devices[0].dv_name);

		/* Wait until the session is ended before we go on */
        while (NU_PPP_Still_Connected(devices[0].dv_name))
			NU_Relinquish();

	}
}

VOID Error_Loop (INT error_num)
{
    INT x, y;

    while (NU_TRUE)
    {
        x++;
        y++;
    }
}

⌨️ 快捷键说明

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