tcpserv.c
来自「基于nucleus操作系统的GPRS无线数据传输终端全套源文件。包括支持ARM7」· C语言 代码 · 共 440 行 · 第 1/2 页
C
440 行
/*************************************************************************/
/* */
/* 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 */
/* */
/* TCPSERV.C */
/* */
/* */
/* DESCRIPTION */
/* */
/* This is a simple example of a TCP 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 "net\inc\socketd.h"
#include "net\inc\tcpdefs.h"
#include "plus\nucleus.h"
#include "ppp\inc\ppp.h"
/* 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};
/* Size of the system memory pool */
#define MEMORY_SIZE 560000ul
/* Define Application data structures. */
NU_MEMORY_POOL System_Memory;
NU_MEMORY_POOL Noncached_Memory;
NU_TASK tcp_server_task_ptr;
NU_TASK StrEcho0;
NU_TASK StrEcho1;
NU_QUEUE socketQueue;
/* Define prototypes for function references. */
VOID tcp_server_task(UNSIGNED argc, VOID *argv);
VOID str_echo(UNSIGNED argc, VOID *argv);
CHAR *DEMO_Get_Modem_String(CHAR *response, CHAR *);
VOID Error_Loop (INT error_num);
#define ECHO_LENGTH 1500
char line[ECHO_LENGTH + 1];
/*************************************************************************/
/* */
/* 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, MEMORY_SIZE, 50, NU_FIFO);
if (status != NU_SUCCESS)
{
Error_Loop (1);
}
/* Allocate stack space for and create each task in the system. */
/* Create tcp_server_task. */
if (NU_Allocate_Memory(&System_Memory,
&pointer, 5000, NU_NO_SUSPEND) != NU_SUCCESS)
{
Error_Loop (2);
}
/* Create the Application tasks. */
status = NU_Create_Task(&tcp_server_task_ptr, "TCPSERV", tcp_server_task,
0, NU_NULL, pointer, 5000, 3, 0, NU_PREEMPT, NU_START);
if (status != NU_SUCCESS)
{
Error_Loop (3);
}
status = NU_Allocate_Memory(&System_Memory, &pointer, 5000, NU_NO_SUSPEND);
if (status != NU_SUCCESS)
{
Error_Loop (4);
}
status = NU_Create_Task(&StrEcho0, "strecho", str_echo, 0, NU_NULL,
pointer, 5000, 3, 0, NU_PREEMPT, NU_START);
if (status != NU_SUCCESS)
{
Error_Loop (5);
}
status = NU_Allocate_Memory(&System_Memory, &pointer, 5000, NU_NO_SUSPEND);
if (status != NU_SUCCESS)
{
Error_Loop (6);
}
status = NU_Create_Task(&StrEcho1, "strecho", str_echo, 0, NU_NULL,
pointer, 5000, 3, 0, NU_PREEMPT, NU_START);
if (status != NU_SUCCESS)
{
Error_Loop (7);
}
/* Create Socket queue. */
status = NU_Allocate_Memory(&System_Memory, &pointer, 10*sizeof(UNSIGNED),
NU_NO_SUSPEND);
if (status != NU_SUCCESS)
{
Error_Loop (8);
}
status = NU_Create_Queue(&socketQueue, "SocQueue", pointer, 10, NU_FIXED_SIZE,
1, NU_FIFO);
if (status != NU_SUCCESS)
{
Error_Loop (9);
}
} /* end Application_Initialize */
/*************************************************************************/
/* */
/* FUNCTION */
/* */
/* tcp_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 tcp_server_task(UNSIGNED argc, VOID *argv)
{
int socketd, newsock; /* the socket descriptor */
struct addr_struct servaddr; /* holds the server address structure */
unsigned int i;
STATUS status;
struct addr_struct client_addr;
char null_ip[] = {0, 0, 0, 0}; /* Not used by PPP */
NU_DEVICE devices[1];
DV_DEVICE_ENTRY *dev_ptr;
char mstring[80];
char subnet_mask[] = {255, 255, 255, 0};
/* Remove warnings for unused parameters. */
status = (STATUS) argc + (STATUS) argv;
/* Call the network initialization. This must be done before any other net
work services are called. When PPP is being used only the first and
second parameters are used. The second parameter is used to pass in a
pointer to the PPP initialization structure.
*/
if(NU_Init_Net(&System_Memory) != NU_SUCCESS)
{
Error_Loop(10);
}
/* Setup the device structure for initialization of PPP. */
memcpy (devices[0].dv_ip_addr, null_ip, 4); /* Not use by PPP. */
memcpy (devices[0].dv_subnet_mask, subnet_mask, 4); /* Not use by PPP. */
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;
/* Init the device */
NU_Init_Devices(devices, 1);
/* 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 into thinking
that it is communicating with modem, when in fact it is connected via
null modem to an embedded device.
*/
while(1)
{
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?