📄 udpserv.c
字号:
/****************************************************************************/
/* */
/* UDPSERV.C */
/* */
/* DESCRIPTION */
/* */
/* Example UDP server program using PPP over a null modem connection. */
/* */
/****************************************************************************/
/* Includes */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "net\inc\externs.h"
#include "plus\nucleus.h"
#include "net\inc\socketd.h" /* socket interface structures */
#include "net\target.h"
#include "net\inc\tcpdefs.h"
#include "ppp\inc\ppp.h"
#define printf PRINTF
/* Define Application data structures. */
NU_MEMORY_POOL System_Memory;
NU_MEMORY_POOL Noncached_Memory;
NU_TASK udp_server_task_ptr;
/* Define prototypes for function references. */
void UDP_Server_Task(UNSIGNED argc, VOID *argv);
CHAR *DEMO_Get_Modem_String(CHAR *response, CHAR *dev_name);
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;
INT what;
unsigned long nocacheSize = (((unsigned long)(__NOCACHE_END)) -
((unsigned long)(__NOCACHE_START)));
what = NET_PARENT_BUFFER_SIZE;
/* 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);
}
/* Create each task in the system. */
/* Create UDP_Server_Task. */
status = NU_Allocate_Memory (&System_Memory, &pointer, 1000, NU_NO_SUSPEND);
if (status != NU_SUCCESS)
{
printf ("Can not create memory for UDP_Server_Task.\n");
DEMO_Exit (3);
}
status = NU_Create_Task (&udp_server_task_ptr, "UDPSERV", UDP_Server_Task, 0,
NU_NULL, pointer, 1000, 3, 0, NU_PREEMPT,
NU_START);
if (status != NU_SUCCESS)
{
printf ("Cannot create UDP_Server_Task.\n");
DEMO_Exit (4);
}
} /* end Application_Initialize */
/*************************************************************************/
/* */
/* FUNCTION */
/* */
/* UDP_Server_Task */
/* */
/* DESCRIPTION */
/* */
/* This function creates a task that will will wait for data */
/* from a client. Upon receipt the data will be echoed back to 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 */
unsigned int *return_ptr;
STATUS status;
INT bytes_received;
INT bytes_sent;
CHAR *buffer;
NU_DEVICE devices[1];
DV_DEVICE_ENTRY *dev_ptr;
INT16 clilen;
UINT8 serv_ip_addr[4];
UINT8 cli_ip_addr[4];
CHAR null_ip_addr[] = {0,0,0,0};
CHAR subnet[] = {255,255,255,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;
/* Access argc and argv just to avoid compilation warnings. */
status = (STATUS) argc + (STATUS) argv;
/* call network initialization */
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 buffer. */
status = NU_Allocate_Memory (&System_Memory, (void *) &buffer, 2000,
NU_SUSPEND);
if (status != NU_SUCCESS)
{
printf("Cannot allocate memory for UDP buffer.\n");
DEMO_Exit(7);
}
/* Switch to terminal mode. */
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 95 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. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -