tcpserv.c
字号:
/* This function creates a task that will accept connections from */
/* clients. The socket descriptor for the connection is then placed*/
/* on the queue. The StrEcho task will then remove the Socket */
/* Descriptor from the queue. */
/* */
/* AUTHOR */
/* */
/* Neil Henderson, Accelerated Technology, Inc. */
/* */
/* CALLED BY */
/* */
/* */
/* CALLS */
/* */
/* NU_Allocate_Memory Allocate memory from memory pool */
/* NU_Socket Establish a new socket descriptor and*/
/* define the type of communication */
/* protocol to be established. */
/* NU_Accept Establish a new socket descriptor, */
/* with information on both client and */
/* server. */
/* NU_Bind Assign a local address to a socket. */
/* NU_Send_To_Queue Places a message at the back of the */
/* specified queue. */
/* NU_Listen Indicates that the server is willing */
/* to accept connection requests. */
/* NU_Suspend_Task Unconditionally suspend a task. */
/* */
/* INPUTS */
/* */
/* none */
/* */
/* OUTPUTS */
/* */
/* none */
/* */
/* HISTORY */
/* */
/* NAME DATE REMARKS */
/* */
/* N. Henderson 06-28-1994 Created initial version 1.0 */
/* MQ Qian 02-05-1996 Modified for QUICC with SDS */
/* */
/*************************************************************************/
void tcp_server_task(UNSIGNED argc, VOID *argv)
{
char c;
int socketd, newsock; /* the socket descriptor */
struct addr_struct *servaddr; /* holds the server address structure */
unsigned int i;
VOID *pointer;
STATUS status;
struct addr_struct client_addr;
#ifdef BOOTP
uchar file[128];
#endif
uint32 ipaddr;
const char ip_addr[] = {192, 200, 100, 5};
/* the following variables are not really used by QUICC, but they are
declared to meet the requirement of the interface of NU_Init_Net() only */
int16 irq = 10;
uint32 addr = 0xcc000L;
uint32 io_addr = 0x300L;
/* call network initialization */
if(NU_Init_Net(ip_addr, irq, addr, io_addr) != NU_SUCCESS)
NU_Suspend_Task(NU_Current_Task_Pointer());
/* Remove warnings for unused parameters. */
status = (STATUS) argc + (STATUS) argv;
#ifdef BOOTP
file[1] = (char) 0;
if ( NU_Bootp(file) != NU_SUCCESS);
#endif
/* open a connection via the socket interface */
if ((socketd = NU_Socket(NU_FAMILY_IP, NU_TYPE_STREAM, 0)) >=0 )
{
status = NU_Allocate_Memory(&System_Memory, &pointer,
sizeof(struct addr_struct), NU_SUSPEND);
if (status == NU_SUCCESS)
{
servaddr = (struct addr_struct *)pointer;
/* fill in a structure with the server address */
servaddr->family = NU_FAMILY_IP;
servaddr->port = 7;
ipaddr = NU_Get_Host_ID();
memcpy(servaddr->id.is_ip_addrs, &ipaddr, 4);
/* make an NU_Bind() call to bind the server's address */
if ((NU_Bind(socketd, servaddr, 0))>=0)
{
/* be ready to accept connection requests */
status = NU_Listen(socketd, 10);
if (status == NU_SUCCESS)
{
for (i=0; i < 2; i++)
{
/* block in NU_Accept until a client attempts connection */
newsock = NU_Accept(socketd, &client_addr, 0);
if (newsock >= 0)
{
/* process the new connection */
status = NU_Send_To_Queue(&socketQueue, (UNSIGNED *)&newsock,
1, NU_SUSPEND);
NU_Sleep(2);
} /* end successful NU_Accept */
} /* end for loop */
} /* end successful NU_Listen */
} /* end successful NU_Bind */
} /* end successful memory allocation */
} /* end successful NU_Socket */
NU_Suspend_Task(NU_Current_Task_Pointer());
}
/*************************************************************************/
/* */
/* FUNCTION */
/* */
/* str_echo */
/* */
/* DESCRIPTION */
/* */
/* Read a stream socket one line at a time */
/* and write each line back to the sender. */
/* Return when the connection is terminated. */
/* */
/* AUTHOR */
/* */
/* Neil Henderson, Accelerated Technology, Inc. */
/* */
/* CALLED BY */
/* */
/* */
/* CALLS */
/* */
/* NU_Recv Receive data across a network during */
/* a connection-oriented transfer. */
/* NU_fcntl Set the block flag associated with a */
/* socket descriptor. */
/* NU_Receive_From_Queue Retrieves a message from the */
/* specified queue. */
/* NU_Send Transmit data across a network */
/* */
/* INPUTS */
/* */
/* none */
/* */
/* OUTPUTS */
/* */
/* none */
/* */
/* HISTORY */
/* */
/* NAME DATE REMARKS */
/* */
/* N. Henderson 06-28-1994 Created initial version 1.0 */
/* */
/*************************************************************************/
void str_echo(UNSIGNED argc, VOID *argv)
{
int connected = 1;
int bytes_recv, bytes_sent;
int sockfd;
STATUS status;
UNSIGNED actSize;
/* Remove compilation warnings for unused parameters. */
status = (STATUS) argc + (STATUS) argv;
status = NU_Receive_From_Queue(&socketQueue, (UNSIGNED *) &sockfd, 1,
&actSize, NU_SUSPEND);
if (status != NU_SUCCESS)
while (-1);
while(connected)
{
/* turn on the "block during a read" flag */
NU_fcntl(sockfd, NU_SETFLAG, NU_BLOCK, 0);
bytes_recv = NU_Recv(sockfd, line, ECHO_LENGTH, 0);
/* turn off the "block during a read" flag -
other reads may not want to block */
NU_fcntl(sockfd, NU_SETFLAG, NU_FALSE, 0);
if (bytes_recv == NU_NOT_CONNECTED)
{
connected = 0;
continue;
}
bytes_sent = NU_Send(sockfd, line, bytes_recv, 0);
}
/* close the connection */
if ((NU_Close_Socket(sockfd)) != NU_SUCCESS)
while(1);
/* Indicate that all went well. */
while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -