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

📄 udp_receive.c

📁 embedded_ethernet_complete_code_rabbit
💻 C
字号:
// The Rabbit receives UDP datagrams from a remote host.

// This file and other embedded Ethernet and Internet code and resources are
// available from www.Lvr.com.

// Select a network configuration from \lib\tcpip\tcp_config.lib
// in the Dynamic C distribution.
#define TCPCONFIG 1

//Allocate a socket buffer for the UDP socket.
#define MAX_UDP_SOCKET_BUFFERS 1

// The local port to receive datagrams on.
#define LOCAL_PORT	5550

// The remote IP address or host name to receive datagrams from.
// Set to 0 to connect to the first host that sends a datagram.
// Set to 255.255.255.255 to receive broadcast datagrams.
// If REMOTE_IP is a host name, you must define MY_NAMESERVER
// in tcp_config.lib or this application.

#define REMOTE_IP			"192.168.111.5"

// All C functions not declared as root go to extended memory.
#memmap xmem

// The dcrtcp library supports UDP and IP communications.
#use "dcrtcp.lib"

//The socket to use for UDP communications.
udp_Socket my_socket;


int receive_packet(void) {

	// Receive a datagram.

   // The array that will hold a received datagram.
	static char received_data[128];

   // The GLOBAL_INIT section executes only once.
	#GLOBAL_INIT {

      // Initialize the block of memory that will hold a received datagram.
		memset(received_data, 0, sizeof(received_data));
	}

	// Receive a datagram from the specified host.
   if (-1 == udp_recv(&my_socket, received_data, sizeof(received_data))) {

		// If the return value is -1, there was no datagram, so return.
		return 0;
	}

   // Display the message
	printf("Received datagram: %d %d\n",received_data[0], received_data[1]);
	return 1;
} // end receive_packet

main() {

	// Initialize network support and wait for datagrams.

   int return_value;

   // sock_init must be called before using other functions in dcrtcp.lib.
   // If return_value isn't zero, the network isn't available.
	return_value = sock_init();
	if (return_value == 0) {
	  printf("Nework support is initialized.\n");
	  }
   else {
      printf("The network is not available.\n");
      exit(0);
      }

	printf("Opening UDP socket\n");

    // Attempt to open the UDP socket using the specified IP address and port
    // numbers.
    // If udp_open fails, stop the program.
    // The resolve() function converts an IP address in dotted-quad format
    // or a host name to an IP address expresses as a longword.
	if(!udp_open(&my_socket, LOCAL_PORT, resolve(REMOTE_IP), 0, NULL)) {
		printf("udp_open failed!\n");
		exit(0);
	}

	while(1) {
		// Endless loop.

      // Process network packets.
		tcp_tick(NULL);

      // Check for a received datagram.
		receive_packet();
	}

} // end main()

⌨️ 快捷键说明

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