⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 udpserv.c

📁 mcf5307实验源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*      UDP_server_task                                                  */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function creates a task that will will wait for data        */
/*      from a client.  Upon receipt the data will be echoed back to the */
/*      client.                                                          */
/*                                                                       */
/* 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_Recv_From                Receive data across a network.       */
/*                                  Connectionless transfer.             */
/*      NU_Bind                     Assign a local address to a socket.  */
/*      NU_Suspend_Task             Unconditionally suspend a task.      */
/*      NU_Send_To                  Transmit data across a network.      */
/*                                  Connectionless transfer              */
/*      NU_Close_Socket             Break the given socket.              */
/*                                                                       */
/* INPUTS                                                                */
/*                                                                       */
/*      none                                                             */
/*                                                                       */
/* OUTPUTS                                                               */
/*                                                                       */
/*      none                                                             */
/*                                                                       */
/* HISTORY                                                               */
/*                                                                       */
/*         NAME            DATE                    REMARKS               */
/*                                                                       */
/*      N. Henderson    06-28-1994      Created initial version 1.0      */
/*                                                                       */
/*************************************************************************/
void UDP_server_task(UNSIGNED argc, VOID *argv)
{
    int socketd;                    /* the socket descriptor */
    struct addr_struct  *servaddr;  /* holds the server address structure */
    struct addr_struct  *cliaddr;   /* holds the client address structure */
    unsigned int        *return_ptr;
    STATUS              status;
    int                 bytes_received;
    int                 bytes_sent;
    char                *buffer;
    int16               clilen;
    const char                ip_addr[] = {192,200,100,5};
    int16               irq = 10;
    uint32              addr = 0xcc000L;
    uint32              io_addr = 0x0300L;

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

    /* call network initialization */
    NU_Init_Net(ip_addr, irq, addr, io_addr);

    /*  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.  */
#if (OUTPUT_OK)
	printf("Cannot allocate memory for UDP buffer\n\r");
#endif /* OUTPUT_OK */

    }


    /* 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) 5;
	    servaddr->name = "ati";

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

	    if (status == NU_SUCCESS)
	    {
		cliaddr = (struct addr_struct *)return_ptr;

		/* initialize client address */
		cliaddr->family    = NU_FAMILY_IP;
		cliaddr->port      = 0;
		cliaddr->id.is_ip_addrs[0]  = (unsigned char) 0;
		cliaddr->id.is_ip_addrs[1]  = (unsigned char) 0;
		cliaddr->id.is_ip_addrs[2]  = (unsigned char) 0;
		cliaddr->id.is_ip_addrs[3]  = (unsigned char) 0;
		cliaddr->name = "";

		/*  Bind our address to the socket.  */
		NU_Bind(socketd, servaddr, 0);

		/*  Prime the loop. */
		buffer[0] = (char) 0;

		while(buffer[0] != 'q')
		{
		    /*  Get a string from the client.  */
		    bytes_received = NU_Recv_From(socketd, buffer, 1000, 0,
						cliaddr, &clilen);

		    /*  If we got back less than zero there is a bad error. */
		    if (bytes_received < 0)
		    {
#if (OUTPUT_OK)
			printf("Error: trying to receive from UDP client\n\r");
#endif /* OUTPUT_OK */
		    }
		    else
		    {
#if (OUTPUT_OK)
			printf("Received %d bytes.\n", bytes_received);
			printf("Received message: %s\n", buffer);
#endif /* OUTPUT_OK */
		    }
		    /*  NULL terminate the input string. */
		    buffer[bytes_received] = (char) 0;

		    /*  Send the string back to the client.  */
		    bytes_sent = NU_Send_To(socketd, buffer, strlen(buffer), 0,
					    cliaddr, clilen);

		    /*  If we got sent less than zero there is a bad error. */
		    if (bytes_sent < 0)
		    {
#if (OUTPUT_OK)
			printf("Error: trying to send to UDP client\n\r");
#endif /* OUTPUT_OK */
		    }
		}
		/* close the connection */
		if ((NU_Close_Socket(socketd)) != NU_SUCCESS)
		{
#if (OUTPUT_OK)
		    printf("\nError from NU_Close_Socket.");
#endif /* OUTPUT_OK */
		}
	    } /* status = NU_SUCCESS */

	} /* status = NU_SUCCESS */

    } /* end successful NU_Socket */

    /*  Indicate successful completion of the program.  */
#if (OUTPUT_OK)
    printf("Successful completion of the UDP Server program.\n\r");
#endif /* OUTPUT_OK */

    NU_Suspend_Task(NU_Current_Task_Pointer());
}

⌨️ 快捷键说明

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