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

📄 udp_srv.c

📁 用Dynamic C写的udp程序
💻 C
字号:
/*******************************************************************************
        Samples\tcpip\UDP\udp_srv.c
        Z-World, 2000

        A UDP example, that receives the 'heartbeat' packets sent from
        udp_cli.c, and will receive either the direct-send packets or
        broadcast packets, depending on the configuration.

*******************************************************************************/
#class auto

/***********************************
 * Configuration                   *
 * -------------                   *
 * All fields in this section must *
 * be altered to match your local  *
 * network settings.               *
 ***********************************/

/*
 * Pick the predefined TCP/IP configuration for this sample.  See
 * LIB\TCPIP\TCP_CONFIG.LIB for instructions on how to set the
 * configuration.
 */
#define TCPCONFIG 1

/*
 * Define the number of socket buffers that will be allocated for
 * UDP sockets.  We only need one UDP socket, so one socket buffer
 * will be enough.
 */
#define MAX_UDP_SOCKET_BUFFERS 1

/*
 * UDP demo configuration
 */
 
/* what local UDP port to use - we receive packets only sent to this port */
#define LOCAL_PORT	1234

/*
 * If this is set to "0", we will accept a connection from anybody.
 * The first person to connect to us will complete the socket with
 * their IP address and port number, and the local socket will be
 * limited to that host only.
 *
 * If it is set to all "255"s, we will receive all broadcast
 * packets instead.
 */
#define REMOTE_IP			"0"
//#define REMOTE_IP			"255.255.255.255" /*broadcast*/

/********************************
 * End of configuration section *
 ********************************/

#memmap xmem
#use "dcrtcp.lib"

udp_Socket sock;

/* receive one packet (heartbeat) */
int receive_packet(void)
{
	static char buf[128];

	#GLOBAL_INIT
	{
		memset(buf, 0, sizeof(buf));
	}

	/* receive the packet */
	if (-1 == udp_recv(&sock, buf, sizeof(buf))) {
		/* no packet read. return */
		return 0;
	}

	printf("Received-> %s\n",buf);
	return 1;
}

void main()
{
	sock_init();
	/*printf("Opening UDP socket\n");*/
	
	if(!udp_open(&sock, LOCAL_PORT, resolve(REMOTE_IP), 0, NULL)) {
		printf("udp_open failed!\n");
		exit(0);
	}

	/* receive heartbeats */
	for(;;) {
		tcp_tick(NULL);
		receive_packet();
	}
}

⌨️ 快捷键说明

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