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

📄 udpcli.c

📁 nucleus source 源码 全部源码
💻 C
字号:
/*************************************************************************/
/*                                                                       */
/*      UDPCLI.C                                                         */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      Example UDP client program using PPP over a dialup connection    */
/*                                                                       */
/*************************************************************************/

#include <string.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 */

#define printf          PRINTF
#define NUMBER_TO_DIAL  "ISP phone number here"
#define LOGIN_ID        "login ID here"
#define PASSWORD        "password here"
#define BUFFER_SIZE     100
#define NUM_LOOPS       50

/* Define Application data structures.  */
NU_MEMORY_POOL    System_Memory;
NU_MEMORY_POOL    Noncached_Memory;
NU_TASK           udp_client_task_ptr;

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

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

   /* Create each task in the system.  */
   
   /* Create UDP_Client_Task.  */
   status = NU_Allocate_Memory (&System_Memory, &pointer, 1000, NU_NO_SUSPEND);
   if (status != NU_SUCCESS)                                 
   {
       printf ("Can not create memory for UDP_Client_Task.\n");
       DEMO_Exit (3);
   }
                          
   status = NU_Create_Task (&udp_client_task_ptr, "UDPSERV", UDP_Client_Task, 0,
                            NU_NULL, pointer, 1000, 3, 0, NU_PREEMPT,
                            NU_START);
   if (status != NU_SUCCESS)
   {
       printf ("Cannot create UDP_Client_Task\n");
       DEMO_Exit (4);
   }


}  /* end Application_Initialize */


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*       UDP_Client_Task                                                 */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function creates a task that prompts the user for input,    */
/*      which is sent to a server.  The task then waits for a response,  */
/*      which is displayed on the monitor.                               */
/*                                                                       */
/*************************************************************************/
void UDP_Client_Task(UNSIGNED argc, VOID *argv)
{
    INT                 socketd;                /* the socket descriptor */
    struct addr_struct  *servaddr; /* holds the server address structure */
    unsigned int        *return_ptr;
    STATUS              status;
    INT                 bytes_received;
    INT                 bytes_sent;
    NU_DEVICE           devices[1];
    CHAR                *sendbuffer;
    CHAR                *recvbuffer;
    INT16               servlen;
    NU_HOSTENT          hentry;
    CHAR                serv_ip_addr[4];
    CHAR                server_name[] = "server_name";
    CHAR                null_ip_addr[] = {0,0,0,0};
    CHAR                subnet[] = {255,255,255,0};
    INT                 x, pokeval, sendval;
    INT                 iterations = 0;
    NU_IOCTL_OPTION     ppp_ioctl;

    /* 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;

    /* Access argc and argv just to avoid compilation warnings.  */
    status =  (STATUS) argc + (STATUS) argv;

    if(NU_Init_Net(&Noncached_Memory) != NU_SUCCESS)
    {
        printf("NU_Init_Net failed.\n");
        DEMO_Exit(5);
    }

    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(6);
    }

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

    if (status != NU_SUCCESS)
    {
        /*  Can't allocate memory, get out.  */
        printf("Cannot allocate memory for UDP sendbuffer\n"); 

        DEMO_Exit(7);
    }

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

    if (status != NU_SUCCESS)
    {
        /*  Can't allocate memory, get out.  */
        printf("Cannot allocate memory for UDP recvbuffer\n"); 
        DEMO_Exit(8);
    }

    pokeval = 0x30;

    for ( x = 0; x < 2000; x++ )
    {
       sendbuffer[x] = pokeval;
       pokeval++;
       if ( pokeval == 0x3a )
          pokeval = 0x30;
    }

    sendval = 60;
    
    /* Set the ID and PW to be used when logging in to the PPP server. */
    NU_Set_PPP_Login (LOGIN_ID, PASSWORD, devices[0].dv_name);

    /* Reset the modem. */
    NU_Modem_Control_String("ATZ^M", devices[0].dv_name);

    /* Wait for the modem. */
    NU_Sleep (2 * TICKS_PER_SECOND);

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

    /* Wait for the modem. */
    NU_Sleep (2 * TICKS_PER_SECOND);

    status = NU_Dial_PPP_Server(NUMBER_TO_DIAL, devices[0].dv_name, serv_ip_addr);
    if (status != NU_SUCCESS)
    {
        printf("Cannot dial into the PPP Server.\n");
        NU_PPP_Hangup(NU_NO_FORCE, devices[0].dv_name);
        DEMO_Exit(9);
    }

    /* open a connection via the socket interface */
    if ((socketd = NU_Socket(NU_FAMILY_IP, NU_TYPE_DGRAM, 0)) < 0)
    {
        printf("Could not open socket via NU_Socket.\n");
        NU_PPP_Hangup(NU_NO_FORCE, devices[0].dv_name);
        DEMO_Exit(10);
    }
    
    status = NU_Get_Host_By_Name (server_name, &hentry);
    if (status != NU_SUCCESS)
    {
        printf("NU_Get_Host_By_Name failed.\n");
        NU_PPP_Hangup(NU_NO_FORCE, devices[0].dv_name);
        DEMO_Exit (11);
    }

    /* Get the address for the opther end of the PPP link. This was given
       to us during PPP negotiation. */
    ppp_ioctl.s_optval = devices[0].dv_name;

    NU_Ioctl (IOCTL_GETIFDSTADDR, &ppp_ioctl, sizeof (NU_IOCTL_OPTION));

    /* Add a route to the server we want to connect to. This route will 
       use the PPP server as a gateway. */
    NU_Add_Route (hentry.h_addr, (UINT8 *) subnet, ppp_ioctl.s_ret.s_ipaddr);

    status = NU_Allocate_Memory (&System_Memory, (void *) &return_ptr,
                                 sizeof(struct addr_struct),
                                 NU_SUSPEND);
    if (status != NU_SUCCESS)
    {
        printf("Could not allocate memory for servaddr.\n");
        NU_PPP_Hangup(NU_NO_FORCE, devices[0].dv_name);
        DEMO_Exit(12);
    }

    servaddr = (struct addr_struct *)return_ptr;

    /* fill in a structure with the server address */
    servaddr->family    = NU_FAMILY_IP;
    servaddr->port      = 7;
    memcpy(&servaddr->id, serv_ip_addr, 4);
    servaddr->name = server_name;

    /*  Prime the loop. */
    bytes_sent = 1;

    while ((bytes_sent > 0) && (iterations++ < NUM_LOOPS))
    {

        /*  Send the datagram.  */
        bytes_sent = NU_Send_To(socketd, sendbuffer, sendval, 0,
                                servaddr, servlen);

        /*  If the data was not sent, we have a problem. */
        if (bytes_sent < 0)
        {
            printf("Error in sending to the UDP server\n");
            PPP_Hangup(PPP_NO_FORCE, devices[0].dv_name);
            DEMO_Exit(13);
        }


        /*  Go get the server's response.  */
        bytes_received = NU_Recv_From(socketd, recvbuffer, sendval, 0,
                                      servaddr, &servlen);


        /* compare what was received to what was sent */
        if ( memcmp( sendbuffer, recvbuffer, sendval ) != 0 )
        {
            printf("String recieved does not match data sent.\n");
            PPP_Hangup(PPP_NO_FORCE, devices[0].dv_name);
            DEMO_Exit(14);
        }

        sendval++;
        if ( sendval == 1460 )
            sendval = 60;

        /*  If we got an error, its bad.  */
        if (bytes_received < 0)
        {
            printf("Error in receiving from the UDP server\n");
            PPP_Hangup(PPP_NO_FORCE, devices[0].dv_name);
            DEMO_Exit(15);
        }

        /*  NULL terminate the string.  */
        sendbuffer[bytes_received] = (char) 0;

        /*  Show the user that we got something back.  */
        printf("The string received was: %s\n", sendbuffer);

    } /* while not quitting */

    /* close the connection */
    status = NU_Close_Socket(socketd);
    if (status != NU_SUCCESS)
    {
        printf("Error from NU_Close_Socket.\n");
        PPP_Hangup(PPP_NO_FORCE, devices[0].dv_name);
        DEMO_Exit(16);
    }

    /*  Indicate that all went well. */
    printf("Successful completion of UDP Client program\n");
    PPP_Hangup(PPP_NO_FORCE, devices[0].dv_name);
    DEMO_Exit(0);

}

/* This function provides an infinite loop for the program to terminate in.
   The local variable a is an exit code which indicates where the program
   aborted.  */

void DEMO_Exit(INT exit_code)
{
    int b = exit_code;        /* This line avoids "unused parameter" warning */
    while(NU_TRUE)
        NU_Sleep (TICKS_PER_SECOND);
}

/* This function is stubbed out.  It may be filled in for debugging purposes */

void PRINTF(char* string, ...) { }

⌨️ 快捷键说明

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