📄 udpserv.c
字号:
/****************************************************************************/
/* */
/* UDPSERV.C */
/* */
/* DESCRIPTION */
/* */
/* Example UDP server program using PPP over a dialup 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" /* PPP interface */
#define printf PRINTF
/* Define Application data structures. */
NU_MEMORY_POOL System_Memory;
NU_MEMORY_POOL Noncached_Memory;
NU_TASK udp_server_task_ptr;
NU_TASK control_task_ptr;
NU_DEVICE devices[1];
CHAR serv_ip_addr[4];
CHAR cli_ip_addr[4];
/* Define prototypes for function references. */
void UDP_Server_Task(UNSIGNED argc, VOID *argv);
void Controller (UNSIGNED argc, VOID *argv);
void DEMO_Exit (INT exit_code);
void PRINTF(char*, ...);
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;
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 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_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);
}
/* Create the modem controller task */
status = NU_Allocate_Memory(&System_Memory, &pointer, 2000, NU_NO_SUSPEND);
if (status != NU_SUCCESS)
{
printf("");
DEMO_Exit(4);
}
status = NU_Create_Task(&control_task_ptr, "control", Controller, 0,
NU_NULL, pointer, 2000, 10, 0, NU_PREEMPT,
NU_START);
if (status != NU_SUCCESS)
{
printf("");
DEMO_Exit(5);
}
} /* 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;
INT16 clilen;
CHAR null_ip_addr[] = {0,0,0,0};
CHAR subnet[] = {255,255,255,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;
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(6);
}
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(7);
}
/* 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 UDP buffer\n");
DEMO_Exit(8);
}
/* Loop forever accepting client connections. */
while (NU_TRUE)
{
/* 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");
DEMO_Exit(9);
}
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");
DEMO_Exit(10);
}
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, null_ip_addr, 4);
servaddr->name = "server_name";
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 cliaddr.\n");
DEMO_Exit(11);
}
cliaddr = (struct addr_struct *)return_ptr;
/* fill in a structure with the client address */
cliaddr->family = NU_FAMILY_IP;
cliaddr->port = 0;
memcpy(&cliaddr->id, null_ip_addr, 4);
cliaddr->name = "client_name";
/* Bind our address to the socket. */
status = NU_Bind(socketd, servaddr, 0);
if (status != NU_SUCCESS)
{
printf("NU_Bing failed.\n");
DEMO_Exit(12);
}
/* 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)
{
printf("Error: trying to receive from UDP client\n");
DEMO_Exit(13);
}
else
{
printf("Received %d bytes.\n", bytes_received);
printf("Received message: %s\n", buffer);
}
/* NULL terminate the input string. */
buffer[bytes_received] = (char) 0;
/* Send the string back to the client. */
bytes_sent = NU_Send_To(socketd, buffer, strlen(buffer), 0,
cliaddr, clilen);
/* If we got sent less than zero there is a bad error. */
if (bytes_sent < 0)
{
printf("Error: trying to send to UDP client\n");
DEMO_Exit(14);
}
} /* end successful NU_Socket */
/* close the connection */
status = NU_Close_Socket(socketd);
if (status != NU_SUCCESS)
{
printf("Error from NU_Close_Socket.\n");
DEMO_Exit(15);
}
} /* while accepting connections */
/* Indicate successful completion of the program. */
printf("Successful completion of the UDP Server program.\n");
DEMO_Exit(0);
}
/*************************************************************************/
/* */
/* 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 (cli_ip_addr, 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(serv_ip_addr, 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();
}
}
/* 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 + -