一 .廣播編程
1.前面介紹的數據包發送方式只有一個接受方,稱爲單播
2.如果同時發給局域網中的所有主機,稱爲廣播
3.只有用戶數據報(使用UDP協議)套接字才能廣播
4.廣播地址
以192.168.1.0 (255.255.255.0) 網段爲例,最大的主機地址192.168.1.255代表該網段的廣播地址
發到該地址的數據包被所有的主機接收
255.255.255.255在所有網段中都代表廣播地址
5.廣播的發送:
6.廣播的接收
實例:udp 客戶端
#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 (inet_pton (AF_INET, argv[1], (void *) &sin.sin_addr) != 1) {
perror ("inet_pton");
exit (1);
}
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;
}
通過廣播的方式進行數據的發送,在我們執行的時候要使用 ./client 192.168.0.255 5003(要用廣播地址,我的ip是192.168.0.129)
注意接收方端口要與發送方相同,此時我們的服務器就能接收到客戶端的信息。
二. 組播編程
1.廣播方式發給所有的主機。過多的廣播會大量佔用網絡帶寬,造成廣播風暴,影響正常的通信。爲了解決這個問題
出現了組播。
組播(又稱爲多播)是一種折中的方式。只有加入某個多播組的主機才能收到數據。既可以發給多個主機,又能避免象廣播那樣帶來過多的負載(每臺主機要到傳輸層才能判斷廣播包是否要處理)
2.組播的發送
3.組播的接收
那麼假如多組播怎麼加入??用了我們之前學過的改變網絡屬性的函數setsockopt
那麼怎麼用這個函數,加入到多播組。
/*加入多播組*/
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); //任意IP,也可指定固定IP
setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,&mreq, sizeof(mreq));
那麼我們要看一下ip_mreq這個結構體中有什麼並且都是什麼含義。
//結構體的定義
struct ip_mreq
{
struct in_addr imr_multiaddr; //多播組的IP地址
struct in_addr imr_interface; //加入的客戶端主機IP地址
}
實例:
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 5004
#define MULTICAST_IP "235.10.10.3"
#define QUIT_STR "quit"
#endif
client.c(和之前的udp編程沒有區別)
#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;
}
server.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;
}
執行的時候 ./client 235.10.10.3 5004
到這,多播和組播的一個編程使用就說完了,主要還是理解編程思路,具體的函數使用方式可以在使用的時候多通過man手冊進行查看。