廣播技術

廣播技術
廣播通信是一種通訊端程式設計技術,這種通信方式是採用單點發送,全網路接收的模式,能夠最大限度地利用頻寬,在當前的Internet中有很多業務是採用廣播技術.
1. ARP協議
ARP是英文Address Resolution Protocol的縮寫,是區域網路中的位址解析通訊協定,利用這個協議,可以找出IP位址到MAC位址的對應關係
2.NTP協議
NTP是英文Network Time Protocol的縮寫,是網路時間協定.
3.原理解析
要進行廣播通信,首先還是要理解廣播地址,在IP地址中,如果最後一個數字為255,則一定是一個廣播地址.
實例操作
實現一個基於C/S模式的廣播通信,伺服器程式負責建立廣播組,並提供一個鍵盤輸入介面,輸入的資訊向組內成員廣播發送;客戶端加入廣播組,接收伺服器的資訊並顯示,若發現資訊中包含quit則退出程式.
1.伺服器程式

#include "/sys/types.h/"
#include "sys/socket.h"
#include "arpa/inet.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "netdb.h"
#include "errno.h"

#define BUFFSIZE
#define PORT 5050

int main()
{
    int serversocket;
    struct sockaddr_in serveraddress,clientaddress;

    int so_broadcast = 1;
    socklen_t size;

    if((serversocket=(socket(AF_INET,SOCK_DGRAM,0))) < 0)
    {
        perror("socket");
        return 0;
    }

    if(setsockopt(serversocket,SOL_SOCKET,SO_BROADCAST,&so_broadcast,sizeof(so_broadcast)) < 0)
    {
        perror("setsockopt");
        return 0;
    }

    serveraddress.sin_family=AF_INET;
    serveraddress.sin_port=htons(INADDR_ANY);
    serveraddress.sin_addr.s_addr= htonl(INADDR_BROADCAST);

    if((bind(serversocket,(struct sockaddr *)&serveraddress, sizeof(struct sockaddr))) < 0)
    {
        perror("bind");
        return 0;
    }

    clientaddress.sin_family=AF_INET;
    clientaddress.sin_port=htons(PORT);
    clientaddress.sin_addr.s_addr= htonl(INADDR_BROADCAST);


    while (1)
    {
        char buf[200];
        printf("please input your word:>");
        scanf("%s",buf);
        if((sendto(serversocket,buf,strlen(buf),0,(struct sockaddr *)&clientaddress,sizeof(clientaddress))) < 0)
        {
            perror("sendto error");
            return 0;
        }
        else
            printf("send msg %s\n",buf);
    }
    return 0;
}



2.客戶端程式

#include "sys/types.h"
#include "sys/socket.h"
#include "arpa/inet.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "netdb.h"
#include "errno.h"

int main()
{
    int clientsocket;
    struct sockaddr_in serveraddress,clientaddress;

    clientsocket = socket(AF_INET, SOCK_DGRAM, 0);
    serveraddress.sin_family = AF_INET;
    serveraddress.sin_port = htons(5050);
    serveraddress.sin_addr.s_addr = htonl(INADDR_ANY);

    int opt = 1;
    if(setsockopt(clientsocket, SOL_SOCKET, SO_REUSEADDR,&opt, sizeof(opt)) < 0)
    {
        perror("setsockopt:SO_REUSEADDR");
        return 0;
    }

    if(bind(clientsocket,(struct sockaddr*)&serveraddress,sizeof(struct sockaddr)) != 0)
    {
        perror("bind");
        return 0;
    }
    char buf[200];
    while (1)
    {
        memset(buf,0,200);
        int size = 0;
        size = recvfrom(clientsocket,buf,200,0, (struct sockaddr *)&clientaddress,sizeof(clientaddress));
        buf[size] = '\0';
        printf("IP:%s msg:%s\n",inet_ntoa(clientaddress.sin_addr),buf);

        if(strcmp(buf,"quit") == 0)
        {
            printf("system quit!!!!\n");
            close(clientsocket);
            return 0;
        }
    }
    return 0;
}

留言

這個網誌中的熱門文章

Json概述以及python對json的相關操作

Docker容器日誌查看與清理

利用 Keepalived 提供 VIP