tcpcli.c
来自「基于nucleus操作系统的GPRS无线数据传输终端全套源文件。包括支持ARM7」· C语言 代码 · 共 295 行
C
295 行
/*************************************************************************/
/* */
/* 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 */
/* */
/* TCPCLI.C */
/* */
/* */
/* DESCRIPTION */
/* */
/* This is a simple example of a TCP client. It is an echo client, */
/* that is, data is transmitted and the client waits for the echo */
/* to come back from the server. This program demonstrates the use of */
/* Nucleus NET over a PPP 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"
/* Size of data to TX and RX. */
#define BUFFER_SIZE 100
/* Number of times to TX and RX the data. */
#define NUM_LOOPS 50
/* These must be filled in. They are specific to your ISP and account. */
#define NUMBER_TO_DIAL "ISP phone number here" /* enter a phone number */
#define LOGIN_ID "login ID here" /* enter a login id */
#define PASSWORD "password here" /* enter a password */
/* Define Application data structures. */
NU_MEMORY_POOL System_Memory;
NU_TASK tcp_client_task_ptr;
/* Define prototypes for function references. */
VOID TCP_client_task(UNSIGNED argc, VOID *argv);
VOID Error_Loop (INT error_num);
/*************************************************************************/
/* */
/* 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 ret_status;
/* Create a system memory pool that will be used to allocate task stacks,
queue areas, etc. */
ret_status = NU_Create_Memory_Pool(&System_Memory, "SYSMEM",
first_available_memory, 300000, 50, NU_FIFO);
if (ret_status != NU_SUCCESS)
{
Error_Loop (1);
}
/* Create the TCP client task. */
ret_status = NU_Allocate_Memory (&System_Memory, &pointer, 3000, NU_NO_SUSPEND);
if (ret_status != NU_SUCCESS)
{
Error_Loop (2);
}
ret_status = NU_Create_Task (&tcp_client_task_ptr, "TCPCLI", TCP_client_task, 0,
NU_NULL, pointer, 3000, 3, 20, NU_PREEMPT,
NU_START);
if (ret_status != NU_SUCCESS)
{
Error_Loop (3);
}
} /* 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 ret_status;
INT bytes_received;
INT bytes_sent;
CHAR *buffer;
CHAR ip_addr[] = {0, 0, 0, 0};
CHAR serv_ip_addr[4];
CHAR subnet_mask[] = {255, 255, 255, 0};
NU_DEVICE devices[1];
NU_HOSTENT hentry;
NU_IOCTL_OPTION ppp_ioctl;
CHAR server_name[] = "your_server_name";
UINT16 iterations = 0;
/* Replace these assignments with a valid ip address */
serv_ip_addr[0] = 100;
serv_ip_addr[1] = 100;
serv_ip_addr[2] = 100;
serv_ip_addr[3] = 100;
if(NU_Init_Net(&System_Memory)) /* call network initialization */
{
Error_Loop (4);
}
memcpy (devices[0].dv_ip_addr, ip_addr, 4);
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;
DEV_Init_Devices(devices, 1);
/* Allocate space for the buffer. */
if (NU_Allocate_Memory (&System_Memory,
(VOID **) &buffer, BUFFER_SIZE,
NU_SUSPEND) != NU_SUCCESS)
{
Error_Loop (5);
}
memset(buffer, 'A', BUFFER_SIZE);
/* 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);
/* Dial the number */
if (NU_Dial_PPP_Server(NUMBER_TO_DIAL, devices[0].dv_name, (UINT8*)serv_ip_addr) == NU_SUCCESS)
{
/* open a connection via the socket interface */
if ((socketd = NU_Socket(NU_FAMILY_IP, NU_TYPE_STREAM, 0))>=0)
{
/* Get the address for the PPP device. This was assigned to us
during PPP negotiation. */
ppp_ioctl.s_optval = (UINT8 *) "PPP_Link";
NU_Ioctl (IOCTL_GETIFADDR, &ppp_ioctl, sizeof (NU_IOCTL_OPTION));
/* Add a route to the server we want to connect to. In this case
we are adding a zero route which will make this device the
default route. */
NU_Add_Route ((UINT8 *)ip_addr, (UINT8 *) subnet_mask, ppp_ioctl.s_ret.s_ipaddr);
if ((ret_status = NU_Get_Host_By_Name (server_name, &hentry)) != NU_SUCCESS)
{
NU_PPP_Hangup(NU_NO_FORCE, devices[0].dv_name);
Error_Loop (6);
}
/* 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 )
{
while ((bytes_sent > 0) && (iterations++ <= NUM_LOOPS))
{
/* Send the datagram. */
bytes_sent = NU_Send(socketd, buffer, BUFFER_SIZE, 0);
/* If the data was not sent, we have a problem. */
if (bytes_sent < 0)
{
bytes_sent = 0;
}
/* 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, BUFFER_SIZE, 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)
{
bytes_sent = 0;
}
} /* if bytes_sent > 0 */
} /* while not quitting */
} /* connected */
else
{
NU_PPP_Hangup(NU_NO_FORCE, devices[0].dv_name);
Error_Loop (7);
}
/* close the connection */
if ((NU_Close_Socket(socketd)) != NU_SUCCESS)
{
NU_PPP_Hangup(NU_NO_FORCE, devices[0].dv_name);
Error_Loop (8);
}
} /* end successful NU_Socket */
else
{
NU_PPP_Hangup(NU_NO_FORCE, devices[0].dv_name);
Error_Loop (9);
}
} /* end successful PPP logon. */
NU_PPP_Hangup(NU_NO_FORCE, devices[0].dv_name);
NU_Sleep(10);
}
VOID Error_Loop (INT error_num)
{
INT x, y;
while (NU_TRUE)
{
x++;
y++;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?