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

📄 tcpcli.c

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

/* Includes */
#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 "net\target.h"
#include "plus\nucleus.h"
#include "ppp\inc\ppp.h"                                /* PPP interface */


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

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

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

extern STATUS PPP_Initialize(DV_DEVICE_ENTRY *device);

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 TCP client task.  */
   status = NU_Allocate_Memory (&System_Memory, &pointer, 5000, NU_NO_SUSPEND);
   if (status != NU_SUCCESS)                                 
   {
       printf ("Can not create memory for TCP_Client_Task.\n");
       DEMO_Exit (3);
   }
                          
   status = NU_Create_Task (&tcp_client_task_ptr, "TCPCLI", TCP_Client_Task, 0,
                            NU_NULL, pointer, 5000, 3, 0, NU_PREEMPT,
                            NU_START);
   if (status != NU_SUCCESS)
   {
       printf ("Cannot create TCP_Client_Task\n\r");
       DEMO_Exit (4);
   }


}  /* end Application_Initialize */


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      TCP_Client_Task                                                  */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This is the task entry function for the task that will           */
/*      communicate with the echo  server.  The client first tries to    */
/*      establish a connection.  Upon establishing a connection data     */
/*      will be sent to the server.  The client then waits to receive    */
/*      the echo of the data.                                            */
/*                                                                       */
/*************************************************************************/

void TCP_Client_Task(UNSIGNED argc, VOID *argv)
{
    INT                 socketd;                /* the socket descriptor */
    struct addr_struct  servaddr;  /* holds the server address structure */
    STATUS              status;
    INT                 bytes_received;
    INT                 bytes_sent;
    CHAR                *buffer;
    INT16               servlen;
    NU_HOSTENT          hentry;
    CHAR                server_name[] = "server_name";
    CHAR                serv_ip_addr[4];
    CHAR                null_ip_addr[] = {0,0,0,0};
    CHAR                subnet[] = {255,255,255,0};
    NU_DEVICE           devices[1];
    NU_IOCTL_OPTION     ppp_ioctl;
    unsigned long       total_pkts = 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;

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

    status = NU_Init_Net(&Noncached_Memory);
    if(status != NU_SUCCESS)  /* call network initialization */
    {
        printf("NU_Init_Net failed.");
        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 buffer.  */
    status = NU_Allocate_Memory (&System_Memory, (void *) &buffer, 2000,
                                    NU_SUSPEND);

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

    /* 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\r");
        NU_PPP_Hangup(NU_NO_FORCE, devices[0].dv_name);
        DEMO_Exit(8);
    }

    /* open a connection via the socket interface */
    if ((socketd = NU_Socket(NU_FAMILY_IP, NU_TYPE_STREAM, 0)) <0)
    {
        printf("Could not open socket via NU_Socket.\n");
        NU_PPP_Hangup(NU_NO_FORCE, devices[0].dv_name);
        DEMO_Exit(9);
    }
    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(10);
    }

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

    /* fill in a structure with the server address */
    servaddr.family    = hentry.h_addrtype;
    memcpy(&servaddr.id, hentry.h_addr, hentry.h_length);
    servaddr.port      = 7;
    servaddr.name      = server_name;

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

    if ((NU_Connect (socketd, &servaddr, 0)) < 0 )
    {
        printf("NU_Connect failed.\n");
        NU_PPP_Hangup(NU_NO_FORCE, devices[0].dv_name);
        DEMO_Exit(11);
    }

    /* Set the push bit for faster transmission. */
    NU_Push (socketd);

    while(bytes_sent > 0)
    {
        sprintf(buffer, "This is TCP test packet # %lu", total_pkts);

        /*  Send the datagram.  */
        bytes_sent = NU_Send(socketd, buffer, 50, 0);

        /*  If the data was not sent, we have a problem. */
        if (bytes_sent < 0)
        {
            printf("Error in sending to the TCP server\n");
            DEMO_Exit(12);
        }

        /*  Only wait for a string if we sent something.  */
        if (bytes_sent > 0)
        {
            /* turn on the "block during a read" flag */
            NU_Fcntl(socketd, NU_SETFLAG, NU_BLOCK);

            /*  Go get the server's response.  */
            bytes_received = NU_Recv(socketd, buffer, bytes_sent, 0);

            /*  turn off the "block during a read" flag -
                other reads may not want to block  */
            NU_Fcntl(socketd, NU_SETFLAG, NU_FALSE);

            /*  If we got an error, its bad.  */
            if (bytes_received < 0)
            {
                printf("Error in receiving from TCP server\n");
                DEMO_Exit(13);
            }

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

            /*  Show the user that we got something back.  */
            printf("The string received was: %s\n", buffer);
        } /*  if bytes_sent > 0 */

        total_pkts++;

    } /* while not quitting */

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

    NU_Sleep(2);

    /*  Indicate that all went well. */
    printf("Successful completion of TCP Client program\n");
    NU_PPP_Hangup(NU_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 + -