广播和组播(“一对多”采用UDP编程)
一、广播
前面介绍的数据包发送方式只有一个接受方,称为单播
如果同时发给局域网中的所有主机,称为广播
只有用户数据报(使用UDP协议)套接字才能广播
广播地址
- 以192.168.1.0 (255.255.255.0) 网段为例,最大的主机地址192.168.1.255代表该网段的广播地址
- 发到该地址的数据包被所有的主机接收
- 255.255.255.255在所有网段中都代表广播地址
/*sender.c*/
/* 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;
}
/*receiver.c*/
#include "net.h"
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;
}
/*net.h*/
#ifndef __MAKEU_NET_H__
#define __MAKEU_NET_H__
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#define SERV_PORT 5003
#define SERV_IP_ADDR "192.168.7.246"
#define QUIT_STR "quit"
#endif
要点:
广播编程是基于UDP编程
- 发送方要通过setsockopt设置套接字广播属性
- 发送方的发送地址为广播地址:XXX.XXX.XXX.255
- 接收方和发送方的端口号要相同
二、组播
组播是一个人发送,加入到多播组的人接收数据
组播的IP地址: 244.0.0.1~239.255.255.254(中间除掉广播)
组播必须基于UDP的编程方法
/*sender.c */
/* usage:
* ./client serv_ip serv_port
*/
#include "net.h"
void usage (char *s)
{
printf ("\nThis is multicast demo!\n");
printf ("\nUsage:\n\t %s serv_ip serv_port", s);
printf ("\n\t serv_ip: udp server ip address(between 224~239 segment)");
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);
}
/*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 ("multicast 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;
}
/*receiver.c*/
#include "net.h"
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));
/*加入多播组*/
struct ip_mreq mreq;
bzero(&mreq, sizeof(mreq));
mreq.imr_multiaddr.s_addr = inet_addr(MULTICAST_IP);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,&mreq, sizeof(mreq));
/*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 ("\nmulticast demo 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 from(%s:%d), data:%s", ipv4_addr, ntohs (cin.sin_port), buf);
if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Client(%s:%d) is exiting!\n", ipv4_addr, ntohs (cin.sin_port));
}
}
close (fd);
return 0;
}
#ifndef __MAKEU_NET_H__
#define __MAKEU_NET_H__
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#define SERV_PORT 5004
#define MULTICAST_IP "235.10.10.3"
#define QUIT_STR "quit"
#endif
UNIX域套接字(用于本地进程间通信的方式)
- 创建套接字时使用本地协议PF_UNIX(或PF_LOCAL)。
socket(AF_LOCAL, SOCK_STREAM, 0)
socket(AF_LOCAL, SOCK_DGRAM, 0) - 分为流式套接字和用户数据报套接字
进程间通信
1.进程间数据共享:
管道、消息队列、共享内存、unix域套接字
易用性:消息队列 > unix域套接字 >管道 >共享内存(经常要和信号量一起使用)
效率:共享内存 > unix域套接字 >管道>消息队列
常用:共享内存、unix域套接字
2.异步通信:
信号
3.同步和互斥(做资源保护)
信号量
/*server.c*/
#include <pthread.h>
#include <signal.h>
#include "net.h"
void cli_data_handle (void *arg);
void sig_child_handle(int signo)
{
if(SIGCHLD == signo) {
waitpid(-1, NULL, WNOHANG);
}
}
int main (void)
{
int fd = -1;
signal(SIGCHLD, sig_child_handle);
/* 1. 创建socket fd */
if ((fd = socket (AF_LOCAL, SOCK_STREAM, 0)) < 0) { //基于本地的TCP通信
perror ("socket");
exit (1);
}
/* 允许绑定地址快速重用 */
int b_reuse = 1;
setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &b_reuse, sizeof (int));
/* 2.1 填充sockaddr_un结构体变量 */
struct sockaddr_un sun;
bzero(&sun, sizeof(sun));
sun.sun_family = AF_LOCAL;
/* 如果UNIX_DOMAIN_FILE所指向的文件存在,则删除 */
if(!access(UNIX_DOMAIN_FILE, F_OK)) {
unlink(UNIX_DOMAIN_FILE);
}
strncpy(sun.sun_path, UNIX_DOMAIN_FILE, strlen( UNIX_DOMAIN_FILE));
/*2.2 绑定 */
if (bind (fd, (struct sockaddr *) &sun, sizeof (sun)) < 0) {
perror ("bind");
exit (1);
}
/*3. 调用listen()把主动套接字变成被动套接字 */
if (listen (fd, BACKLOG) < 0) {
perror ("listen");
exit (1);
}
printf ("Unix domain server starting....OK!\n");
int newfd = -1;
/*4. 阻塞等待客户端连接请求 */
while(1) {
pid_t pid = -1;
if ((newfd = accept (fd, NULL,NULL)) < 0) {
perror ("accept");
break;
}
/*创建一个子进程用于处理已建立连接的客户的交互数据*/
if((pid = fork()) < 0) {
perror("fork");
break;
}
if(0 == pid) { //子进程中
close(fd);
printf ("Clinet is connected!\n");
cli_data_handle(&newfd);
return 0;
} else { //实际上此处 pid >0, 父进程中
close(newfd);
}
}
close (fd);
return 0;
}
void cli_data_handle (void *arg)
{
int newfd = *(int *) arg;
printf ("Child handling process: newfd =%d\n", newfd);
//..和newfd进行数据读写
int ret = -1;
char buf[BUFSIZ];
char resp_buf[BUFSIZ+10];
while (1) {
bzero (buf, BUFSIZ);
do {
ret = read (newfd, buf, BUFSIZ - 1);
} while (ret < 0 && EINTR == errno);
if (ret < 0) {
perror ("read");
exit (1);
}
if (!ret) { //对方已经关闭
break;
}
printf ("Receive data: %s\n", buf);
if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Client(fd=%d) is exiting!\n", newfd);
break;
}
bzero(resp_buf, BUFSIZ+10);
strncpy(resp_buf, SERV_RESP_STR, strlen(SERV_RESP_STR));
strcat(resp_buf, buf);
do {
ret = write(newfd, resp_buf, strlen(resp_buf));
}while(ret < 0 && EINTR == errno);
}
close (newfd);
}
/*client.c*/
/*./client unix_domain_file */
#include "net.h"
void usage (char *s)
{
printf ("\n%s unix_domain_file\n\n", s);
}
int main (int argc, char **argv)
{
int fd = -1;
int port = -1;
struct sockaddr_un sun;
if (argc != 2) {
usage (argv[0]);
exit (1);
}
/* 1. 创建socket fd */
if ((fd = socket (AF_LOCAL, SOCK_STREAM, 0)) < 0) {
perror ("socket");
exit (1);
}
/*2.连接服务器 */
/*2.1 填充struct sockaddr_un结构体变量 */
bzero (&sun, sizeof (sun));
sun.sun_family = AF_LOCAL;
/*确保UNIX_DOMAIN_FILE要先存在并且可写,不存在则退出 */
if( access(UNIX_DOMAIN_FILE, F_OK| W_OK) < 0){
exit(1);
}
strncpy(sun.sun_path, UNIX_DOMAIN_FILE, strlen( UNIX_DOMAIN_FILE));
if (connect (fd, (struct sockaddr *) &sun, sizeof (sun)) < 0) {
perror ("connect");
exit (1);
}
printf ("Unix domain Client staring...OK!\n");
int ret = -1;
fd_set rset;
int maxfd = -1;
struct timeval tout;
char buf[BUFSIZ];
while (1) {
FD_ZERO (&rset);
FD_SET (0, &rset);
FD_SET (fd, &rset);
maxfd = fd;
tout.tv_sec = 5;
tout.tv_usec = 0;
select (maxfd + 1, &rset, NULL, NULL, &tout);
if (FD_ISSET (0, &rset)) { //标准键盘上有输入
//读取键盘输入,发送到网络套接字fd
bzero (buf, BUFSIZ);
do {
ret = read (0, buf, BUFSIZ - 1);
} while (ret < 0 && EINTR == errno);
if (ret < 0) {
perror ("read");
continue;
}
if (!ret)
continue;
if (write (fd, buf, strlen (buf)) < 0) {
perror ("write() to socket");
continue;
}
if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Client is exiting!\n");
break;
}
}
if (FD_ISSET (fd, &rset)) { //服务器给发送过来了数据
//读取套接字数据,处理
bzero (buf, BUFSIZ);
do {
ret = read (fd, buf, BUFSIZ - 1);
} while (ret < 0 && EINTR == errno);
if (ret < 0) {
perror ("read from socket");
continue;
}
if (!ret)
break; /* 服务器关闭 */
//There is a BUG,FIXME!!
printf ("server said: %s\n", buf);
if ((strlen(buf) > strlen(SERV_RESP_STR))
&& !strncasecmp (buf+strlen(SERV_RESP_STR), QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Sender Client is exiting!\n");
break;
}
}
}
/*4.关闭套接字 */
close (fd);
}
/*net.h*/
#ifndef __MAKEU_NET_H__
#define __MAKEU_NET_H__
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <sys/select.h>
#include <sys/time.h>
//#include <sys/types.h>
#include <unistd.h>
#include <sys/un.h>
#define UNIX_DOMAIN_FILE "/tmp/my_domain_file.1"
#define BACKLOG 5
#define QUIT_STR "quit"
#define SERV_RESP_STR "SERVER:"
#endif