udpcli.c

来自「mcf5307实验源代码」· C语言 代码 · 共 291 行 · 第 1/2 页

C
291
字号
		while(1);

   status = NU_Create_Task (&udp_client_task_ptr, "UDPSERV", UDP_client_task, 0,
							NU_NULL, pointer, 2000, 3, 0, NU_PREEMPT,
							NU_START);
   if (status != NU_SUCCESS)
		while(1);

}  /* end Application_Initialize */


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*       UDP_client_task                                                 */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function creates a task that prompts the user for input,    */
/*      which is sent to a server.  The task then waits for a response,  */
/*      which is displayed on the monitor.                               */
/*                                                                       */
/* 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_Send_To                  Transfer data across a network.      */
/*                                  Connectionless transfer.             */
/*      NU_Recv_From                Receive data across a network.       */
/*                                  Connectionless transfer.             */
/*      NU_Close_Socket             Break a socket allocation.           */
/*      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      */
/*                                                                       */
/*************************************************************************/
void UDP_client_task(UNSIGNED argc, VOID *argv)
{
    int socketd;                    /* the socket descriptor */
    struct addr_struct  *servaddr;  /* holds the server address structure */
    unsigned int        *return_ptr;
    STATUS              status;
    int                 bytes_received;
    int                 bytes_sent;
    int16               servlen;
    char                buffer[10];
    static int          temp=0;
    char                ip_addr[] = {192,200,100,4};
    int16               irq = 3;
    uint32              addr = 0xd0000L;
    uint32              io_addr = 0x0280L;

	/* Access argc and argv just to avoid compilation warnings.  */
	status =  (STATUS) argc + (STATUS) argv;

	
   if(NU_Init_Net(ip_addr, irq, addr, io_addr))   /* call network initialization */
		while(1);
	
	
	/* open a connection via the socket interface */
	if ((socketd = NU_Socket(NU_FAMILY_IP, NU_TYPE_DGRAM, 0))>=0)
	{

	   status = NU_Allocate_Memory (&System_Memory, (void *) &return_ptr,
				    sizeof(struct addr_struct),
				    NU_SUSPEND);
	if (status == NU_SUCCESS)
	{
	    servaddr = (struct addr_struct *)return_ptr;

	    /* fill in a structure with the server address */
	    servaddr->family    = NU_FAMILY_IP;
	    servaddr->port      = 6000;
	    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;

	    while(bytes_sent > 0)
	    {
				if (++temp==50)
				{
		    bytes_sent = NU_Send_To(socketd, "\n\r", 1, 0, servaddr, servlen);
					break;
				}
				else
					bytes_sent = NU_Send_To(socketd, "testing", 7, 0, servaddr, servlen);

				/*  If the data was not sent, we have a problem. */
		if (bytes_sent < 0)
					while(1);

				/*  Go get the server's response.  */
				bytes_received = NU_Recv_From(socketd, buffer, 8, 0,servaddr,&servlen);

				/*  If we got an error, its bad.  */
				if (bytes_received < 0)
					while(1);

				/*  NULL terminate the string.  */
				buffer[bytes_received] = (char) 0;

			} /* while not quitting */

	    /* close the connection */
	    if ((NU_Close_Socket(socketd)) != NU_SUCCESS)
					while(1);
		} /* status = NU_SUCCESS */

	} /* end successful NU_Socket */

	/*  Indicate that all went well. */
	while(1);

    NU_Suspend_Task(NU_Current_Task_Pointer());
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?