4/10日

1、广播 

  1 #include
  2 #include
  3 #include
  4 #include
  5 #include
  6 #include
  7 #include
  8 #include
  9 
 10 
 11 #define ERR_MSG(msg) do{\
 12     fprintf(stderr,"%d",__LINE__);\
 13     perror("msg");\
 14 }while(0)
 15 
 16 #define IP "192.168.2.255"
 17 #define PORT 6868
 18 
 19 int main(int argc, const char *argv[])
 20 {
 21     //创建报式套接字
 22     int sfd = socket(AF_INET,SOCK_DGRAM,0);
 23     if(sfd < 0)
 24     {
 25         ERR_MSG("socket");
 26         return -1;
 27     }
 28 
 29     //创建填充地址信息结构体
 30     struct sockaddr_in sin;
 31     sin.sin_family   ^Z = AF_INET;                                                                                                                                                                                                                                                        
 32     sin.sin_port        = htons(PORT);
 33     sin.sin_addr.s_addr = inet_addr(IP);
 34     //绑定自身服务器端的IP和端口
 35     if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
 36     {
 37         ERR_MSG("bind");
 38         return -1;
 39     }
 40 
 41     //因为要接收数据,所以要定义一个结构体用来存储客户端的信息
 42     struct sockaddr_in cin;
 43     socklen_t addrlen = sizeof(cin);
 44 
 45     char buf[128]="";
 46     ssize_t res = 0;
 47 
 48     while(1)
 49     {
 50         //接收数据
 51         bzero(buf,sizeof(buf));
 52         //继续接受数据包发送方的地址信息,给sendto使用
 53         res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);
 54         if(res < 0)
 55         {
 56             ERR_MSG("recvfrom");
 57             return -1;
 58         }
 59         printf("[%s:%d]: %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
 60 
 61     }
 62 
 63     //关闭
 64     close(sfd);
 65     return 0;
 66 }
~                                       
  1 #include
  2 #include
  3 #include
  4 #include
  5 #include
  6 #include
  7 #include
  8 #include
  9 
 10 
 11 #define ERR_MSG(msg) do{\
 12     fprintf(stderr,"%d",__LINE__);\
 13     perror("msg");\
 14 }while(0)
 15 
 16 #define SER_IP "192.168.0.255"
 17 #define SER_PORT 6666
 18 
 19 #define CLI_IP "192.168.0.111"
 20 #define CLI_PORT 7777
 21 int main(int argc, const char *argv[])
 22 {
 23     //创建报式套接字
 24     int cfd = socket(AF_INET,SOCK_DGRAM,0);
 25     if(cfd < 0)
 26     {
 27         ERR_MSG("socket");
 28         return -1;
 29     }
 30 
 31     //设置允许广播
 32     int broad = 1;
 33     if(setsockopt(cfd,SOL_SOCKET,SO_BROADCAST,&broad,sizeof(broad)) < 0)
 34     {
 35         ERR_MSG("msg");
 36         return -1;
 37     }
 38     printf("set broadcast sucess\n");
 39 
 40 /*
 41     //绑定自身服务器端的IP和端口
 42     //非必须绑定
 43     struct sockaddr_in cin;
 44     cin.sin_family      = AF_INET;
 45     cin.sin_port        = htons(CLI_PORT);
 46     cin.sin_addr.s_addr = inet_addr(CLI_IP);
 47 
 48     if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin)) < 0)
 49     {
 50         ERR_MSG("bind");
 51         return -1;
 52     }
 53     printf("client bin sucess\n");
 54 */
 55     //创建填充服务器地址信息结构体
 56     struct sockaddr_in sin;
 57     sin.sin_family      = AF_INET;
 58     sin.sin_port        = htons(SER_PORT);
 59     sin.sin_addr.s_addr = inet_addr(SER_IP);
 60 
 61     //继续接受数据包发送方的地址信息,给sendto使用
 62     struct sockaddr_in rcvaddr;
 63     socklen_t addrlen = sizeof(rcvaddr);
 64 
 65     char buf[128]="";
 66     ssize_t res = 0;
 67 
 68     while(1)
 69     {
 70         bzero(buf,sizeof(buf));
 71         printf("请输入>>>");
 72         fgets(buf,sizeof(buf),stdin);
 73         buf[strlen(buf)-1] = 0;
 74 
 75 
 76         //发送数据
 77         if(sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
 78         {
 79             ERR_MSG("sendto");
 80             return -1;
 81         }
 82         printf("发送成功\n");
 83                                                                                                                                                                                                                                                                                                                                                                  
 84     }
 85 
 86     //关闭
 87     close(cfd);
 88     return 0;
 89 }
~                          

 2、组播

  1 #include
  2 #include
  3 #include
  4 #include
  5 #include
  6 #include
  7 #include
  8 #include
  9 
 10 
 11 #define ERR_MSG(msg) do{\
 12     fprintf(stderr,"%d",__LINE__);\
 13     perror("msg");\
 14 }while(0)
 15 
 16 #define IP "192.168.2.132"
 17 #define PORT 6767
 18 #define GRP_IP "224.1.2.4"
 19 
 20 int main(int argc, const char *argv[])
 21 {
 22     //创建报式套接字
 23     int sfd = socket(AF_INET,SOCK_DGRAM,0);
 24     if(sfd < 0)
 25     {
 26         ERR_MSG("socket");
 27         return -1;
 28     }
 29 
 30     //加入多播组
 31     struct ip_mreqn mq;
 32     mq.imr_multiaddr.s_addr = inet_addr(GRP_IP);//组播IP
 33     mq.imr_address.s_addr   = inet_addr(IP);//本机IP
 34     mq.imr_ifindex = 2;
 35 
 36 
 37     if(setsockopt(sfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mq,sizeof(mq)) <  0)
 38     {
 39         ERR_MSG("setsockopt");
 40         return 0;
 41     }
 42     printf("加入多播组成功\n");
 43 
 44 
 45     //创建填充地址信息结构体
 46     struct sockaddr_in sin;
 47     sin.sin_family      = AF_INET;
 48     sin.sin_port        = htons(PORT);
 49     sin.sin_addr.s_addr = inet_addr(GRP_IP);
 50     //绑定自身服务器端的IP和端口
 51     if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
 52     {
 53         ERR_MSG("bind");
 54         return -1;
 55     }                                                                                                                                                                                                                                                                                                                                                            
 56 
 57     //因为要接收数据,所以要定义一个结构体用来存储客户端的信息
 58     struct sockaddr_in cin;
 59     socklen_t addrlen = sizeof(cin);
 60 
 61     char buf[128]="";
 62     ssize_t res = 0;
 63 
 64     while(1)
 65     {
 66         //接收数据
 67         bzero(buf,sizeof(buf));
 68         //继续接受数据包发送方的地址信息,给sendto使用
 69         res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);
 70         if(res < 0)
 71         {
 72             ERR_MSG("recvfrom");
 73             return -1;
 74         }
 75         printf("[%s:%d]: %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
 76 
 77 
 78     }
 79 
 80     //关闭
 81     close(sfd);
 82     return 0;
 83 }
  1 #include
  2 #include
  3 #include
  4 #include
  5 #include
  6 #include
  7 #include
  8 #include
  9 
 10 
 11 #define ERR_MSG(msg) do{\
 12     fprintf(stderr,"%d",__LINE__);\
 13     perror("msg");\
 14 }while(0)
 15 
 16 #define SER_IP "224.1.2.4"                                                                                                                                                                                                                                                                                                                                       
 17 #define SER_PORT 6767
 18 
 19 #define CLI_IP "192.168.0.111"
 20 #define CLI_PORT 6767
 21 int main(int argc, const char *argv[])
 22 {
 23     //创建报式套接字
 24     int cfd = socket(AF_INET,SOCK_DGRAM,0);
 25     if(cfd < 0)
 26     {
 27         ERR_MSG("socket");
 28         return -1;
 29     }
 30 
 31 /*
 32     //绑定自身服务器端的IP和端口
 33     //非必须绑定
 34     struct sockaddr_in cin;
 35     cin.sin_family      = AF_INET;
 36     cin.sin_port        = htons(CLI_PORT);
 37     cin.sin_addr.s_addr = inet_addr(CLI_IP);
 38 
 39     if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin)) < 0)
 40     {
 41         ERR_MSG("bind");
 42         return -1;
 43     }
 44     printf("client bin sucess\n");
 45 */
 46     //创建填充服务器地址信息结构体
 47     struct sockaddr_in sin;
 48     sin.sin_family      = AF_INET;
 49     sin.sin_port        = htons(SER_PORT);
 50     sin.sin_addr.s_addr = inet_addr(SER_IP);
 51 
 52     struct sockaddr_in rcvaddr;
 53     socklen_t addrlen = sizeof(rcvaddr);
 54 
 55     char buf[128]="";
 56     ssize_t res = 0;
 57 
 58     while(1)
 59     {
 60         bzero(buf,sizeof(buf));
 61         printf("请输入>>>");
 62         fgets(buf,sizeof(buf),stdin);
 63         buf[strlen(buf)-1] = 0;
 64 
 65 
 66         //发送数据
 67         if(sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
 68         {
 69             ERR_MSG("sendto");
 70             return -1;
 71         }
 72         printf("发送成功\n");
 73 
 74     }
 75 
 76     //关闭
 77     close(cfd);
 78     return 0;
 79 }
~                               

 

你可能感兴趣的:(网络,服务器,udp)