6月20日

1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/socket.h>
4 #include <unistd.h>
5 #include <arpa/inet.h>
6 #include <netinet/in.h>
7 #include <string.h>
8 
9 #define ERR_MDG(msg) do{\
0     printf("LINE:__%d__\n",__LINE__);\
1     perror(msg);\
2 }while(0)
3 
4 #define IP  "224.3.2.1"
5 #define PORT 8878
6 
7 
8 int main(int argc, const char *argv[])
9 {
0     int cfd = socket(AF_INET, SOCK_DGRAM, 0);
1     if(cfd < 0)
2     {
3         ERR_MDG("socket");
4         return -1;
5     }
6     printf("socket is success\n");
7 
8     //绑定本UPD的IP和PORT
9     struct sockaddr_in sin;
0     sin.sin_family        = AF_INET;
1     sin.sin_addr.s_addr   = inet_addr(IP);
2     sin.sin_port          = htons(PORT);
3 
4 
5     char buf[128] = "";
6     int res = 0;
7     //发送信息                                                                                                                                                                                                                            
8     while(1)
9     {
0         strcpy(buf, "hellow");
1         if(sendto(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
2         {
3             ERR_MDG("sendto");
4             return -1;
5         }
6         printf("发送成功\n");
7         sleep(2);
8     }
9     close(cfd);
0     return 0;
1 }
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                            
  1 #include <stdio.h>
  2 #include <sys/types.h>
  3 #include <sys/socket.h>
  4 #include <netinet/in.h>
  5 #include <arpa/inet.h>
  6 #include <string.h>
  7 #include <errno.h>
  8 
  9 #define ERR_MSG(msg) {\
 10     printf("LINE:__%d__\n",__LINE__);\
 11     printf("FUNC:__%s__\n",__func__);\
 12     perror(msg);\
 13 }
 14 
 15 #define IP "192.168.124.14"
 16 #define PORT 6666
 17 
 18 #define IIP "0.0.0.0"
 19 #define PPORT 12345
 20 
 21 #define IIIP "192.168.124.255"
 22 
 23 int main(int argc, const char *argv[])
 24 {
 25     //创建套接字
 26     int fd = socket(AF_INET, SOCK_DGRAM, 0);
 27     if(fd < 0)
 28     {
 29         ERR_MSG("socket");
 30         return -1;
 31     }
 32     printf("套接字创建成功\n");
 33 
 34 
 35 
 36 //设置功能
 37 
 38     int reuse = 1;
 39     if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
 40     {
 41         ERR_MSG("setsockopt");
 42         return -1;
 43     }
 44     printf("允许端口快速复用\n");
 45 
 46     int gtreuse  = 0;
 47     socklen_t gtrelen = sizeof(gtreuse);
 48     getsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &gtreuse, &gtrelen);
 49     if(gtreuse == 0)
 50         printf("不允许端口快速使用\n");
 51     else
 52         printf("允许端口快速重用\n");
 53 
 54 
 55 
 56     //设置为允许发送广播
 57     int gb1 = 1;
 58     if(setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &gb1, sizeof(gb1)) < 0)
 59     {
 60         ERR_MSG("setsockopt_SO_BRADCAST");
 61         return -1;
 62     }
 63     printf("允许发送广播设置成功\n");
 64 
 65 
 66 
 67     int gb = 0;
 68     socklen_t gblen = sizeof(gblen);
 69     getsockopt(fd, SOL_SOCKET, SO_BROADCAST, &gb, &gblen);
 70     if(gb == 0)
 71         printf("不允许发送广播\n");
 72     else
 73         printf("允许发送广播\n");
 74 
 75 
 76 
 77 
 78 //绑定本UPD的IP和PORT
 79     struct sockaddr_in sin;
 80     sin.sin_family        = AF_INET;
 81     sin.sin_addr.s_addr   = inet_addr(IP);
 82     sin.sin_port          = htons(PORT);
 83     if(bind(fd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
 84     {
 85         if(errno != 98)
 86         {
 87             ERR_MSG("bind");
 88             return -1;
 89         }
 90     }
 91     printf("bin is success\n");
 92 
 93 
 94     //cin用于接收对端的地址信息
 95     struct sockaddr_in cin;
 96     cin.sin_family = AF_INET;
 97     cin.sin_addr.s_addr = inet_addr(IIIP);
 98     cin.sin_port = htons(PORT);
 99     socklen_t addlen = sizeof(cin);
100     int res = 0;
101     char buf[128] = "";
102 
103 
104 
105 
106     //循环接收与发送信息
107     printf("*****开始循环*****\n ");
108     while(1)
109     {
110         bzero(buf, sizeof(buf));
111         /*
112         //printf("**************\n");
113         res = recvfrom(fd, buf, sizeof(buf), 0, NULL, NULL);
114         if(res < 0)
115         {
116             ERR_MSG("recvfrom");
117             return -1;
118         }
119         printf("[%s:%d] %s\n",inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);
120         */
121 
122         //strcat(buf, "*+*");
123         printf("buf>>>");
124         scanf("%s",buf);
125         getchar();
126         int len = sendto(fd, buf, sizeof(buf), 0,(struct sockaddr*)&cin , sizeof(cin));
127         if(len < 0)
128         {
129             ERR_MSG("sendto");
130             return -1;
131         }
132     }
133 
134     return 0;
135 }                                                                                                                                                                                                                                                                                                                  
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                      
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/socket.h>
4 #include <unistd.h>
5 #include <arpa/inet.h>
6 #include <netinet/in.h>
7 #include <string.h>
8 
9 #define ERR_MDG(msg) do{\
0     printf("LINE:__%d__\n",__LINE__);\
1     perror(msg);\
2 }while(0)
3 
4 #define LOC_IP  "192.168.124.14"
5 #define PORT     8878
6 #define GRP_IP "224.3.2.1"
7 
8 
9 int main(int argc, const char *argv[])
0 {
1     int cfd = socket(AF_INET, SOCK_DGRAM, 0);
2     if(cfd < 0)
3     {
4         ERR_MDG("socket");
5         return -1;
6     }
7     printf("socket is success\n");
8 
9     //允许端口复用
0     int reuse = 1;
1     if(setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
2 
3         ERR_MDG("setsockopt1");                                                                                                                                                                                                    
4         return -1;
5     }
6 
7     //加入组播组
8     struct ip_mreqn mq;
9     mq.imr_multiaddr.s_addr = inet_addr(GRP_IP);
0     mq.imr_address.s_addr   = inet_addr(LOC_IP);
1     mq.imr_ifindex   = 0;
2     if(setsockopt(cfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mq, sizeof(mq)) < 0)
3     {
4         ERR_MDG("setsockopt_GRP");
5         return -1;
6     }
7     printf("加入成功\n");
8 
9     //绑定本UPD的IP和PORT
0     struct sockaddr_in sin;
1     sin.sin_family        = AF_INET;
2     sin.sin_addr.s_addr   = inet_addr(GRP_IP);
3     sin.sin_port          = htons(PORT);
4     if(bind(cfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
5     {
6         ERR_MDG("bind");
7         return -1;
8     }
9     printf("bin is success\n");
0 
1 
2     //目标端的信息
3     struct sockaddr_in cin;
4     socklen_t addrlen = sizeof(cin);
5     char buf[128] = "";
6     int res = 0;
7     //接收信息
8     while(1)
9     {
0         bzero(buf,sizeof(buf));
1         printf("&*****\n");
2         res = recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, &addrlen);
3         if(res < 0)
4         {
5             ERR_MDG("recvfrom");
6             return -1;
7         }
8         printf("%s\n", buf);
9     }
0     close(cfd);
1     return 0;
2 }
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值