一.实验目的
(1) 在掌握IP多播的基本概念和工作原理的基础上,进行多播编程,实现多播通信;
(2) 在掌握广播的基本概念和工作原理的基础上,进行广播播编程,实现广播通信;
二.实验内容
(1)编写一对用于发送和接收信息的IP多播程序,其中:
发送端可向该多播组广播文件中保存的新闻信息,接收端可接收传递到该多播组的新闻信息。
(2)编写一对用于发送和接收信息的IP多播程序,其中:
发送端可向该多播组广播使用者从键盘输入的即时消息,接收端可接收传递到该多播组的即时消息。
(3)编写一对用于发送和接收信息的广播程序,其中:
发送端可向特定网络广播文件中保存的新闻信息,接收端可接收传递到该网络的新闻信息。
(4)编写一对用于发送和接收信息的广播程序,其中:
发送端可向特定网络广播使用者从键盘输入的即时消息,接收端可接收传递到该网络的即时消息。
三.实验过程
(1)多播接收发送文件:
News_sender.c:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <arpa/inet.h>#include <sys/socket.h>#define TTL 64#define BUF_SIZE 30void error_handling(char *message);int main(int argc, char *argv[]){int send_sock;struct sockaddr_in mul_adr;int time_live=TTL;FILE *fp;char buf[BUF_SIZE];if(argc!=3){printf(\"Usage : %s <GroupIP> <PORT>\\n\", argv[0]);exit(1);}send_sock=socket(PF_INET, SOCK_DGRAM, 0);memset(&mul_adr, 0, sizeof(mul_adr));mul_adr.sin_family=AF_INET;mul_adr.sin_addr.s_addr=inet_addr(argv[1]); // Multicast IPmul_adr.sin_port=htons(atoi(argv[2])); // Multicast Portsetsockopt(send_sock, IPPROTO_IP,IP_MULTICAST_TTL, (void*)&time_live, sizeof(time_live));if((fp=fopen(\"news.txt\", \"r\"))==NULL)error_handling(\"fopen() error\");while(!feof(fp)) /* Broadcasting */{fgets(buf, BUF_SIZE, fp);sendto(send_sock, buf, strlen(buf),0, (struct sockaddr*)&mul_adr, sizeof(mul_adr));sleep(2);}fclose(fp);close(send_sock);return 0;}void error_handling(char *message){fputs(message, stderr);fputc(\'\\n\', stderr);exit(1);}News_receiver.c:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <arpa/inet.h>#include <sys/socket.h>#define BUF_SIZE 30void error_handling(char *message);int main(int argc, char *argv[]){int recv_sock;int str_len;char buf[BUF_SIZE];struct sockaddr_in adr;struct ip_mreq join_adr;if(argc!=3) {printf(\"Usage : %s <GroupIP> <PORT>\\n\", argv[0]);exit(1);}recv_sock=socket(PF_INET, SOCK_DGRAM, 0);memset(&adr, 0, sizeof(adr));adr.sin_family=AF_INET;adr.sin_addr.s_addr=htonl(INADDR_ANY);adr.sin_port=htons(atoi(argv[2]));if(bind(recv_sock, (struct sockaddr*) &adr, sizeof(adr))==-1)error_handling(\"bind() error\");join_adr.imr_multiaddr.s_addr=inet_addr(argv[1]);join_adr.imr_interface.s_addr=htonl(INADDR_ANY);setsockopt(recv_sock, IPPROTO_IP,IP_ADD_MEMBERSHIP, (void*)&join_adr, sizeof(join_adr));while(1){str_len=recvfrom(recv_sock, buf, BUF_SIZE-1, 0, NULL, 0);if(str_len<0)break;buf[str_len]=0;fputs(buf, stdout);}close(recv_sock);return 0;}void error_handling(char *message){fputs(message, stderr);fputc(\'\\n\', stderr);exit(1);}
结果:
(2)多播实现接收发送消息;
Sender.c:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <arpa/inet.h>#include <sys/socket.h>#define TTL 64#define BUF_SIZE 30void error_handling(char *message);int main(int argc, char *argv[]){int send_sock;struct sockaddr_in mul_adr;int time_live=TTL;char buf[BUF_SIZE];if(argc!=3){printf(\"Usage : %s <GroupIP> <PORT>\\n\", argv[0]);exit(1);}send_sock=socket(PF_INET, SOCK_DGRAM, 0);memset(&mul_adr, 0, sizeof(mul_adr));mul_adr.sin_family=AF_INET;mul_adr.sin_addr.s_addr=inet_addr(argv[1]); // Multicast IPmul_adr.sin_port=htons(atoi(argv[2])); // Multicast Portsetsockopt(send_sock, IPPROTO_IP,IP_MULTICAST_TTL, (void*)&time_live, sizeof(time_live));while(1) /* Broadcasting */{fputs(\"请输入:\",stdout);fgets(buf, sizeof(buf), stdin);sendto(send_sock, buf, strlen(buf),0, (struct sockaddr*)&mul_adr, sizeof(mul_adr));}close(send_sock);return 0;}void error_handling(char *message){fputs(message, stderr);fputc(\'\\n\', stderr);exit(1);}Receiver.c:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <arpa/inet.h>#include <sys/socket.h>#define BUF_SIZE 30void error_handling(char *message);int main(int argc, char *argv[]){int recv_sock;int str_len;char buf[BUF_SIZE];struct sockaddr_in adr;struct ip_mreq join_adr;if(argc!=3) {printf(\"Usage : %s <GroupIP> <PORT>\\n\", argv[0]);exit(1);}recv_sock=socket(PF_INET, SOCK_DGRAM, 0);memset(&adr, 0, sizeof(adr));adr.sin_family=AF_INET;adr.sin_addr.s_addr=htonl(INADDR_ANY);adr.sin_port=htons(atoi(argv[2]));if(bind(recv_sock, (struct sockaddr*) &adr, sizeof(adr))==-1)error_handling(\"bind() error\");join_adr.imr_multiaddr.s_addr=inet_addr(argv[1]);join_adr.imr_interface.s_addr=htonl(INADDR_ANY);setsockopt(recv_sock, IPPROTO_IP,IP_ADD_MEMBERSHIP, (void*)&join_adr, sizeof(join_adr));while(1){str_len=recvfrom(recv_sock, buf, BUF_SIZE-1, 0, NULL, 0);if(str_len<0)break;buf[str_len]=0;fputs(buf, stdout);}close(recv_sock);return 0;}void error_handling(char *message){fputs(message, stderr);fputc(\'\\n\', stderr);exit(1);}
结果:
(3)广播接收发送文件:
Sender_brd.c:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <arpa/inet.h>#include <sys/socket.h>#define BUF_SIZE 30void error_handling(char *message);int main(int argc, char *argv[]){int send_sock;struct sockaddr_in broad_adr;FILE *fp;char buf[BUF_SIZE];int so_brd=1;if(argc!=3) {printf(\"Usage : %s <Boradcast IP> <PORT>\\n\", argv[0]);exit(1);}send_sock=socket(PF_INET, SOCK_DGRAM, 0);memset(&broad_adr, 0, sizeof(broad_adr));broad_adr.sin_family=AF_INET;broad_adr.sin_addr.s_addr=inet_addr(argv[1]);broad_adr.sin_port=htons(atoi(argv[2]));setsockopt(send_sock, SOL_SOCKET,SO_BROADCAST, (void*)&so_brd, sizeof(so_brd));if((fp=fopen(\"news.txt\", \"r\"))==NULL)error_handling(\"fopen() error\");while(!feof(fp)){fgets(buf, BUF_SIZE, fp);sendto(send_sock, buf, strlen(buf),0, (struct sockaddr*)&broad_adr, sizeof(broad_adr));sleep(2);}close(send_sock);return 0;}void error_handling(char *message){fputs(message, stderr);fputc(\'\\n\', stderr);exit(1);}Receiver_brd.c:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <arpa/inet.h>#include <sys/socket.h>#define BUF_SIZE 30void error_handling(char *message);int main(int argc, char *argv[]){int recv_sock;struct sockaddr_in adr;int str_len;char buf[BUF_SIZE];if(argc!=2) {printf(\"Usage : %s <PORT>\\n\", argv[0]);exit(1);}recv_sock=socket(PF_INET, SOCK_DGRAM, 0);memset(&adr, 0, sizeof(adr));adr.sin_family=AF_INET;adr.sin_addr.s_addr=htonl(INADDR_ANY);adr.sin_port=htons(atoi(argv[1]));if(bind(recv_sock, (struct sockaddr*)&adr, sizeof(adr))==-1)error_handling(\"bind() error\");while(1){str_len=recvfrom(recv_sock, buf, BUF_SIZE-1, 0, NULL, 0);if(str_len<0)break;buf[str_len]=0;fputs(buf, stdout);}close(recv_sock);return 0;}void error_handling(char *message){fputs(message, stderr);fputc(\'\\n\', stderr);exit(1);}
结果:
(4)广播实现接收发送消息:
Sender_brd.c:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <arpa/inet.h>#include <sys/socket.h>#define BUF_SIZE 30void error_handling(char *message);int main(int argc, char *argv[]){int send_sock;struct sockaddr_in broad_adr;char buf[BUF_SIZE];int so_brd=1;if(argc!=3) {printf(\"Usage : %s <Boradcast IP> <PORT>\\n\", argv[0]);exit(1);}send_sock=socket(PF_INET, SOCK_DGRAM, 0);memset(&broad_adr, 0, sizeof(broad_adr));broad_adr.sin_family=AF_INET;broad_adr.sin_addr.s_addr=inet_addr(argv[1]);broad_adr.sin_port=htons(atoi(argv[2]));setsockopt(send_sock, SOL_SOCKET,SO_BROADCAST, (void*)&so_brd, sizeof(so_brd));while(1){fputs(\"请输入:\",stdout);fgets(buf, sizeof(buf), stdin);sendto(send_sock, buf, strlen(buf),0, (struct sockaddr*)&broad_adr, sizeof(broad_adr));}close(send_sock);return 0;}void error_handling(char *message){fputs(message, stderr);fputc(\'\\n\', stderr);exit(1);}Receiver.c:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <arpa/inet.h>#include <sys/socket.h>#define BUF_SIZE 30void error_handling(char *message);int main(int argc, char *argv[]){int recv_sock;struct sockaddr_in adr;int str_len;char buf[BUF_SIZE];if(argc!=2) {printf(\"Usage : %s <PORT>\\n\", argv[0]);exit(1);}recv_sock=socket(PF_INET, SOCK_DGRAM, 0);memset(&adr, 0, sizeof(adr));adr.sin_family=AF_INET;adr.sin_addr.s_addr=htonl(INADDR_ANY);adr.sin_port=htons(atoi(argv[1]));if(bind(recv_sock, (struct sockaddr*)&adr, sizeof(adr))==-1)error_handling(\"bind() error\");while(1){str_len=recvfrom(recv_sock, buf, BUF_SIZE-1, 0, NULL, 0);if(str_len<0)break;buf[str_len]=0;fputs(buf, stdout);}close(recv_sock);return 0;}void error_handling(char *message){fputs(message, stderr);fputc(\'\\n\', stderr);exit(1);}
结果: