tcpcli.c
来自「mcf5307实验源代码」· C语言 代码 · 共 224 行
C
224 行
/*************************************************************************/
/* */
/* Copyright (c) 1993,1996 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. */
/* */
/*****************************************************************************/
/*
* Includes
*/
#include <string.h>
#include "externs.h"
#include "nucleus.h"
#include "socketd.h" /* socket interface structures */
#include "tcpdefs.h"
/* 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);
/*************************************************************************/
/* */
/* FUNCTION */
/* */
/* Application_Initialize */
/* */
/* DESCRIPTION */
/* */
/* Application_Initialize performs the work 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, 400000, 50, NU_FIFO);
if (status != NU_SUCCESS)
{
while(1); /* cannot create memory pool */
}
/* Create each task in the system. */
/* Create TCP client task. */
status = NU_Allocate_Memory (&System_Memory, &pointer, 3000, NU_NO_SUSPEND);
if (status != NU_SUCCESS)
{
while(1); /* Can not create memory for tcp_client_task. */
}
status = NU_Create_Task (&tcp_client_task_ptr, "TCPCLI", TCP_client_task, 0,
NU_NULL, pointer, 3000, 3, 0, NU_PREEMPT,
NU_START);
if (status != NU_SUCCESS)
{
while(1); /* Cannot create TCP_client_task */
}
} /* 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;
char ip_addr[] = {192,200,100,5};
uint32 myaddr;
int temp;
/* Access argc and argv just to avoid compilation warnings. */
status = (STATUS) argc + (STATUS) argv;
if(NU_Init_Net(ip_addr, 3, 0, 0)) /* call network initialization */
{
while(1); /* error at call to NU_Initialize() from TCP_client_task */
}
/* 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. */
while(1); /* Cannot allocate memory for TCP buffer */
}
memset(buffer, 'a', 20);
buffer[20] = (char) 0;
/* open a connection via the socket interface */
if ((socketd = NU_Socket(NU_FAMILY_IP, NU_TYPE_STREAM, 0))>=0)
{
/* fill in a structure with the server address */
servaddr.family = NU_FAMILY_IP;
servaddr.port = 2007;
servaddr.id.is_ip_addrs[0] = (unsigned char) 192;
servaddr.id.is_ip_addrs[1] = (unsigned char) 200;
servaddr.id.is_ip_addrs[2] = (unsigned char) 100;
servaddr.id.is_ip_addrs[3] = (unsigned char) 1;
servaddr.name = "ati";
/* Prime the loop. */
bytes_sent = 1;
if ((NU_Connect (socketd, &servaddr, 0)) >=0 )
{
while(bytes_sent > 0)
{
/* Send the datagram. */
bytes_sent = NU_Send(socketd, buffer, strlen(buffer), 0);
/* If the data was not sent, we have a problem. */
if (bytes_sent < 0)
{
while(1); /* Error in sending to the TCP server */
}
/* 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, 0);
bytes_received = 0;
while (bytes_received < bytes_sent)
{
/* Go get the server's response. */
temp = NU_Recv(socketd, (buffer + bytes_received),
(2000 - bytes_received), 0);
/* Break on error */
if (temp >= 0)
bytes_received += temp;
else
break;
} /* end while */
/* turn off the "block during a read" flag -
other reads may not want to block */
NU_fcntl(socketd, NU_SETFLAG, NU_FALSE, 0);
/* If we got an error, its bad. */
if (bytes_received < 0)
{
while(1); /* Error in receiving from TCP server */
}
/* NULL terminate the string. */
buffer[bytes_received] = (char) 0;
} /* if bytes_sent > 0 */
} /* while not quitting */
} /* connected */
/* close the connection */
if ((NU_Close_Socket(socketd)) != NU_SUCCESS)
{
while(1); /* Error from NU_Close_Socket. */
}
} /* end successful NU_Socket */
/* Indicate that all went well. */
while(1); /* Successful completion of TCP Client program */
NU_Suspend_Task(NU_Current_Task_Pointer());
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?