C语言socket编程广播

该文章展示了使用C语言实现UDP广播的发送和接收过程。代码包括创建socket,设置允许广播,绑定到特定端口,接收和发送数据包的步骤。程序中包含了一个广播接收器和一个客户端,用于交互式发送广播消息。当接收到特定的退出字符串时,客户端会停止发送。
摘要由CSDN通过智能技术生成

1、广播发送与接收

 2、代码实现

int main (void)
{

	int fd = -1;
	struct sockaddr_in sin;

	/* 1. 创建socket fd */
	if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) {	//udp程序
		perror ("socket");
		exit (1);
	}

	/* 2. 允许绑定地址快速重用 */
	int b_reuse = 1;
	setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &b_reuse, sizeof (int));

	/*2. 绑定 */
	/*2.1 填充struct sockaddr_in结构体变量 */
	bzero (&sin, sizeof (sin));
	sin.sin_family = AF_INET;
	sin.sin_port = htons (SERV_PORT);	//网络字节序的端口号

	/* 让服务器程序能绑定在任意的IP上 */
#if 1
	sin.sin_addr.s_addr = htonl (INADDR_ANY);
#else
	if (inet_pton (AF_INET, SERV_IP_ADDR, (void *) &sin.sin_addr) != 1) {
		perror ("inet_pton");
		exit (1);
	}
#endif
	/*2.2 绑定 */
	if (bind (fd, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
		perror ("bind");
		exit (1);
	}

	char buf[BUFSIZ];
	struct sockaddr_in cin;
	socklen_t addrlen = sizeof (cin);
	printf ("\nBoardcast receiver started!\n");
	while (1) {
		bzero (buf, BUFSIZ);
		if (recvfrom (fd, buf, BUFSIZ - 1, 0, (struct sockaddr *) &cin, &addrlen) < 0) {
			perror ("recvfrom");
			continue;
		}

		char ipv4_addr[16];
		if (!inet_ntop (AF_INET, (void *) &cin.sin_addr, ipv4_addr, sizeof (cin))) {
			perror ("inet_ntop");
			exit (1);
		}

		printf ("Recived boardcast data:%s\n",  buf);

		if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) {	//用户输入了quit字符
			printf ("Sender(%s:%d) is exiting!\n", ipv4_addr, ntohs (cin.sin_port));
		}

	}

	close (fd);

	return 0;
}

/*udp demo */

/* usage:
 * ./client serv_ip serv_port 
*/
#include "net.h"
void usage (char *s)
{
	printf ("\nThis is udp demo!\n");
	printf ("\nUsage:\n\t %s serv_ip serv_port", s);
	printf ("\n\t serv_ip: udp server ip address");
	printf ("\n\t serv_port: udp server port(serv_port > 5000)\n\n");
}

int main (int argc, char *argv[])
{
	int fd = -1;
	int port = SERV_PORT;

	port = atoi (argv[2]);
	if (port < 0 || (port > 0 && port <= 5000)) {
		usage (argv[0]);
		exit (1);
	}
	struct sockaddr_in sin;
	if (argc != 3) {
		usage (argv[0]);
		exit (1);
	}

	/* 1. 创建socket fd */
	if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) {	//UDP编程
		perror ("socket");
		exit (1);
	}

	/* 允许广播设置 */
	int b_br = 1;
	setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &b_br, sizeof(int));

	/*2.1 填充struct sockaddr_in结构体变量 */
	bzero (&sin, sizeof (sin));

	sin.sin_family = AF_INET;
	sin.sin_port = htons (SERV_PORT);	//网络字节序的端口号
#if 0
	sin.sin_addr.s_addr = inet_addr (argv[1]);
#else
	if (inet_pton (AF_INET, argv[1], (void *) &sin.sin_addr) != 1) {
		perror ("inet_pton");
		exit (1);
	}
#endif
	printf ("broadcast demo started!\n");
	char buf[BUFSIZ];
	while (1) {
		fprintf (stderr, "pls input string:");
		bzero (buf, BUFSIZ);
		if (fgets (buf, BUFSIZ - 1, stdin) == NULL) {
			perror ("fgets");
			continue;
		}

		sendto (fd, buf, strlen (buf), 0, (struct sockaddr *) &sin, sizeof (sin));

		if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) {	//用户输入了quit字符
			printf ("Client is exited!\n");
			break;
		}

	}
	close (fd);
	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值