linux 多网卡发送udp广播

一、客户端代码

#include
#include
#include
#include
#include
#include
#include
#include


int Create_destIpSendBrocast(const char *ipaddress,int port,struct sockaddr_in *addrServer){
        if(ipaddress==NULL){
                return -1;
        }
        int sockClient  = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
        if (sockClient<0){
                printf ("socket create failed. ip=[%s] \n ", ipaddress);
                return  -1;
        }
        int bBroadcast = 1;
        if (0 != setsockopt ( sockClient,SOL_SOCKET,SO_BROADCAST, (char *)&bBroadcast, sizeof(int))){
                printf ("setsockopt failed. ip=[%s] ", ipaddress);
                goto exit0;
        }
        struct sockaddr_in addrClient   = {0};
        addrClient.sin_family   = AF_INET;
        addrClient.sin_addr.s_addr      = inet_addr(ipaddress);
        addrClient.sin_port     = 0;    /// 0 表示由系统自动分配端口号
        if (0 != bind (sockClient, (struct sockaddr*)&addrClient, sizeof(addrClient))){
                printf ("bind failed.ip=[%s] \n", ipaddress);
                goto exit0;
        }
        addrServer->sin_family = AF_INET;
        addrServer->sin_addr.s_addr = htonl ( INADDR_BROADCAST );
        addrServer->sin_port = htons (port);
        return sockClient;
exit0:
        close(sockClient);

        return  -1;
}


void  *ThreadProc(void * arg){
        char    szIp[16] = {0};
        strcpy (szIp, (char*)arg);
        int     nConunter = 0;
        struct sockaddr_in addrServer;
        int sockClient = Create_destIpSendBrocast((const char *)szIp,10000,&addrServer);
        while(1) {
                char pcSendMsg[1024] = {0};
                time_t  rawTime;
                time(&rawTime);
                struct tm  *t = localtime(&rawTime);
                sprintf(pcSendMsg, "%04d-%02d-%02d %02d-%02d-%02d | ip=[%s]", t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, szIp);
                sendto ( sockClient, pcSendMsg, strlen(pcSendMsg)+1, 0, (struct sockaddr *) &addrServer, sizeof ( struct sockaddr_in ));
                printf("%s\n",pcSendMsg);
                sleep(1);

        }
        close(sockClient);
        return  NULL;
}
int main(void){
        char hostname[1024] = {0};
        gethostname(hostname, sizeof(hostname));    //获得本地主机名
        printf("hostname = %s\n",hostname);
        struct hostent * hostinfo = gethostbyname(hostname);//信息结构体
        struct in_addr **addr_list;
        addr_list = (struct in_addr **)hostinfo->h_addr_list;

        int i=0;
             for(i = 0; addr_list[i] != NULL; i++) {
                char *ip = inet_ntoa(*addr_list[i]);
                if(ip!=NULL){
                        printf("%s %s \n", __func__,inet_ntoa(*addr_list[i]));
                        pthread_t tid;
                        pthread_create(&tid, 0, ThreadProc, ip);
                }
        }
        while(1){
                pause();
        }
        printf("exit\n");
        return 0;
}

 



   

二、服务端代码

#include
#include
#include


#include
#include
#include


void init_addr(struct sockaddr_in *addr, char *ip,  int port)
{
addr->sin_family = AF_INET;
 
addr->sin_port = htons(port);
 if(ip == NULL)
addr->sin_addr.s_addr = htonl(INADDR_ANY);
else
addr->sin_addr.s_addr = inet_addr(ip);
}
int create_listen_brocast(char *ipaddr,int port)
{
struct sockaddr_in addr;
const int on=1;
int fd = socket(AF_INET,SOCK_DGRAM,0);
if(fd == -1)
{
perror("create udp socket fail!");
return -1;
}
init_addr(&addr, ipaddr,  port);
if(setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on))<0)
  {
    printf("init broadcast setsockopt failed \n");
    return -1;
  }
if (bind(fd, (struct sockaddr*)&addr, sizeof(struct sockaddr))<0)
{
  printf("bind broadcast failed\n");
  return -1;
}
return fd;
}

int main(int argc,char **argv)
{
        //int sock = create_listen_udp(NULL,10000);
        int sock = create_listen_brocast(NULL,10000);
        if(sock<0){
                printf("create sock failed \n");
                return -1;
        }
        ssize_t ret = 0;
        char buf[128]={0};
        struct sockaddr_in src_addr;
        int len = sizeof(struct sockaddr_in);
        while(1){
                ret =recvfrom(sock,(void *)buf , 128, 0,(struct sockaddr *)&src_addr, (socklen_t *)&len);
                if(ret<=0){
                        usleep(100);
                        continue;
                }
                printf("ret = %d\n",ret);
        }
        return 0;
}


你可能感兴趣的:(linux系统)