客户端通信

客户端1  .  c   文件

  1 #include<stdio.h>
  2 #include<arpa/inet.h>
  3 #include<bits/types.h>
  4 #include<stdint.h>
  5 #include<sys/types.h>
  6 #include<sys/socket.h>
  7 #include<errno.h>
  8 #include<string.h>
  9 #include<stdlib.h>
 10 #include<unistd.h>
 11 //传输的信息结构体
 12 struct trr
 13 {
 14     int used;
 15     char data[4096];
 16 };
 17 int main(int argc, const char *argv[])
 18 {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
 19     //写入目标服务器的ip
 20     char* p="192.168.186.130";
 21 
 22 
 23 
 24     //创建客户端的socket套接字符
 25     int client =socket(AF_INET,SOCK_STREAM,0);
 26     if(client==-1)
 27     {
 28         perror("socket");
 29         return -1;
 30     }
 31 
 32 
 33 
 34     //定义了一个装有ip和pot的结构体
 35     struct sockaddr_in add;
 36 
 37     //设定为ipv4;
 38     add.sin_family=AF_INET;
 39     //端口号in_port_t
 40     while(argc!=2){printf("输入端口号\n");close(client);return -1;}
 41     short  port= atoi(argv[1]);
 42     add.sin_port=htons(port);
 43 
 44 
 45 
 46     //ip
 47     if(0>inet_pton(AF_INET,p,&add.sin_addr))
 48     {
 49         perror("inet_pton");
 50         close(client);
 51         return -1;
 52     }
 53 
 54     //将本客户端和服务器链接
 55     if(-1==connect(client,(struct sockaddr*)&add,sizeof(add)))
 56     {
 57         perror("connect");
 58         return -1;
 59     }
 60 
 61     //接收到了客户端2的信息
 62 
 63     char client2_ip [16] = "" ;
 64     int client2_port  =  0 ;
 65     recv(client,client2_ip,15,0);
 66     if(strlen(client2_ip)!=15)
 67     {
 68         perror("recv");
 69         return -1;
 70     }
 71     printf("<<<成功接收到客户端2的ip>>>\n");
 72     printf("%s\n",client2_ip);
 73     if(-1==recv(client,&client2_port,2,0))
 74 
 75 
 76     {
 77         perror("recv");
 78         return -1;
 79     }
 80     printf("<<<成功接收到客户端2的port>>>\n");
 81     printf("%d\n",client2_port);
 82     close(client);
 83     //通过  接受到的客户端2的信息
 84     //与客户端2链接
 85 
 86 
 87   client =socket(AF_INET,SOCK_STREAM,0);
 88      if(client==-1)
 89       {
 90           perror("socket");
 91           return -1;
 92       }
 93 
 94 
 95 
 96 
 97 
 98  struct sockaddr_in ccc2;
 99 
100 
101     //设定为ipv4;
102     ccc2.sin_family=AF_INET;
103 
104 
105     //端口号in_port_t
106     ccc2.sin_port=htons(9999);
107 
108 
109     //ip
110     if(inet_pton(AF_INET,p,&ccc2.sin_addr)<=0)
111     {
112         perror("inet_pton");
113         close(client);
114         return -1;
115     }
116 
117 
118 
119     //将装有ip和port的结构体写入创建的套接字文件
120     //服务器筛选接受信息的ip和端口号
121     if(-1==bind(client,(struct sockaddr *)&ccc2,sizeof(ccc2)))
122     {
123         perror("bind");
124     }
125 
126 
127     if(-1==listen(client,10))
128     {
129         perror("listen");
130         close(client);
131         return -1;
132     }
133 
134 
135   //链接客户端2
136       //client1 接受客户端2的ip,port
137       struct sockaddr_in  client_addr1={0};
138       int len_client_addr1 =  sizeof(client_addr1);
139       int client1 = 0;
140       short  client1_port1 = 0;
141       printf("等待客户端2链接》》》\n");
142       client1 =  accept ( client , (struct sockaddr *)&client_addr1 , &len_client_addr1 );
143 
144      client1_port1 = client_addr1.sin_port;
145 
146      if(-1==client1)
147      {
148          perror("accept1");
149      }
150      char client_ip1  [16]="";
151      inet_ntop(AF_INET,&client_addr1.sin_addr,client_ip1,16);
152      printf("\n成功连接客户端2ip :%s \n\n",client_ip1);
153 
154 //创建select监听列表
155 fd_set  list;
156 FD_ZERO(&list);
157 FD_SET(0,&list);
158 FD_SET(client1,&list);
159 //下面要实现直接与客户端2通信
160     while(1)
161     {
162         fd_set temp=list;
163         select(client1+1,&temp,NULL,NULL,NULL);
164         if(FD_ISSET(0,&temp))
165         {
166             printf("本客户端正在输入信息\n");
167             char buf1[64]="";
168             scanf("%s",buf1);
169             if(-1==send(client1,buf1,64,0))
170             {
171                 perror("send");
172                 return -1;
173             }
174 
175         }
176         if(FD_ISSET(client1,&temp))
177         {
178             printf("有信息传入\n");
179             char buf2[64]="";
180             if(recv(client1,buf2,64,0)==-1)
181             {
182                 perror("recv");
183                 return -1;
184             }
185             printf("%s",buf2);
186         }
187     }
188     return 0;
189 }
~                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
~                                                                                                                                                                                                                                                                                                                 

客户端2   .c文件

  1 #include<stdio.h>
  2 #include<arpa/inet.h>
  3 #include<bits/types.h>
  4 #include<stdint.h>
  5 #include<sys/types.h>
  6 #include<sys/socket.h>
  7 #include<errno.h>
  8 #include<string.h>
  9 #include<stdlib.h>
 10 #include<unistd.h>
 11 //传输的信息结构体
 12 struct trr
 13 {
 14     int used;
 15     char data[4096];
 16 };
 17 int main(int argc, const char *argv[])
 18 {
 19     //写入目标服务器的ip
 20     char* p="192.168.186.130";
 21 
 22 
 23 
 24     //创建客户端的socket套接字符
 25     int client =socket(AF_INET,SOCK_STREAM,0);
 26     if(client==-1)
 27     {
 28         perror("socket");
 29         return -1;
 30     }
 31 
 32 
 33 
 34     //定义了一个装有ip和pot的结构体
 35     struct sockaddr_in add;
 36 
 37     //设定为ipv4;
 38     add.sin_family=AF_INET;
 39     //端口号in_port_t
 40     while(argc!=2){printf("输入端口号\n");close(client);return -1;}
 41     short  port= atoi(argv[1]);
 42     add.sin_port=htons(port);
 43 
 44 
 45 
 46     //ip
 47     if(0>inet_pton(AF_INET,p,&add.sin_addr))
 48     {
 49         perror("inet_pton");
 50         close(client);
 51         return -1;
 52     }
 53 
 54     //将本客户端和服务器链接
 55     if(-1==connect(client,(struct sockaddr*)&add,sizeof(add)))
 56     {
 57         perror("connect");
 58         return -1;
 59     }
 60 
 61     //接收到了客户端1的信息
 62 
 63     char client2_ip [16] = "" ;
 64     int client2_port  =  0 ;
 65     recv(client,client2_ip,15,0);
 66     if(strlen(client2_ip)!=15)
 67     {
 68         perror("recv");
 69         return -1;
 70     }
 71     printf("<<<成功接收到客户端1的ip>>>\n");
 72     printf("%s\n",client2_ip);
 73     if(-1==recv(client,&client2_port,2,0))
 74 
 75 
 76     {
 77         perror("recv");
 78         return -1;
 79     }
 80     printf("<<<成功接收到客户端1的port>>>\n");
 81     printf("%d\n",client2_port);
 82 
 83   close(client);
 84 
 85     //将本客户端2与客户端1链接
 86 
 87 int sockfd=socket(AF_INET,SOCK_STREAM,0);
 88        if(sockfd==-1)
 89         {
 90             perror("socket");
 91             return -1;
 92         }
 93 
 94 add.sin_port=htons(9999);
 95 
 96      if(-1==connect(sockfd,(struct sockaddr*)&add,sizeof(add)))
 97       {
 98          perror("connect");
 99          return -1;
100      }
101     printf("成功将客户端2与客户端1链接\n\n");
102 
103 //创建select监听列表
104 fd_set  list;
105 FD_ZERO(&list);
106 FD_SET(0,&list);
107 FD_SET(sockfd,&list);
108 //下面要实现直接与客户端2通信
109     while(1)
110     {
111         fd_set temp=list;
112         select(sockfd+1,&temp,NULL,NULL,NULL);
113         if(FD_ISSET(0,&temp))
114         {
115             printf("本客户端正在输入信息\n");
116             char buf1[64]="";
117             scanf("%s",buf1);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
118             if(-1==send(sockfd,buf1,64,0))
119             {
120                 perror("send");
121                 return -1;
122             }
123 
124         }
125         if(FD_ISSET(sockfd,&temp))
126         {
127              printf("有信息传入\n");
128             char buf2[64]="";
129             if(recv(sockfd,buf2,64,0)==-1)
130             {
131                 perror("recv");
132                 return -1;
133             }
134             printf("%s\n",buf2);
135 
136         }
137     }
138 
139 
140 
141 
142 
143     return 0;
144 }
~                                                                                                                                                                         

服务器文件

            1 #include<stdio.h>
  2 #include<arpa/inet.h>
  3 #include<bits/types.h>
  4 #include<stdint.h>
  5 #include<sys/types.h>
  6 #include<sys/socket.h>
  7 #include<errno.h>
  8 #include<string.h>
  9 #include<stdlib.h>
 10 #include<unistd.h>
 11 //接受信息结构体
 12 struct trr
 13 {
 14     int used;
 15     char data[4096];
 16 };
 17 int main(int argc, const char *argv[])
 18 {
 19     char* p="0.0.0.0";
 20     //创建服务器的socket套接字符
 21     int sockfd=socket(AF_INET,SOCK_STREAM,0);
 22     if(-1==sockfd)
 23     {
 24         perror("socket");
 25         return -1;
 26     }
 27 
 28 
 29     //初始化了一个装有ip和pot的结构体
 30     struct sockaddr_in add;
 31 
 32 
 33     //设定为ipv4;
 34     add.sin_family=AF_INET;
 35 
 36 
 37     //端口号in_port_t
 38     while(argc!=2)
 39     {
 40         printf("输入端口号\n");
 41         close(sockfd);
 42         return -1;
 43 
 44     }
 45     short  port= atoi(argv[1]);
 46     add.sin_port=htons(port);
 47 
 48  
 49     //ip 
 50     if(inet_pton(AF_INET,p,&add.sin_addr)<=0) 
 51     { 
 52         perror("inet_pton"); 
 53         close(sockfd); 
 54         return -1; 
 55     } 
 56  
 57  
 58  
 59     //将装有ip和port的结构体写入创建的套接字文件 
 60     //服务器筛选接受信息的ip和端口号 
 61     if(-1==bind(sockfd,(struct sockaddr *)&add,sizeof(add))) 
 62     { 
 63         perror("bind"); 
 64     } 
 65  
 66  
 67     if(-1==listen(sockfd,10)) 
 68     { 
 69         perror("listen"); 
 70         close(sockfd); 
 71         return -1; 
 72     } 
 73  
 74  
 75  
 76     printf("服务器已经启动,等待连接》》》\n"); 
 77  
 78  
 79 //链接第一个客户端 
 80     //client1 接受连接成功的客户端ip,port 
 81     struct sockaddr_in  client_addr1={0}; 
 82     int len_client_addr1 =  sizeof(client_addr1); 
 83     int client1 = 0; 
 84     short  client1_port1 = 0;  
 85     printf("等待第一个客户端链接》》》\n"); 
 86     client1 =  accept ( sockfd , (struct sockaddr *)&client_addr1 , &len_client_addr1 ); 
 87      
 88     client1_port1 = client_addr1.sin_port; 
 89      
 90     if(-1==client1)  
 91     { 
 92         perror("accept1"); 
 93     } 
 94     char client_ip1  [16]=""; 
 95     inet_ntop(AF_INET,&client_addr1.sin_addr,client_ip1,16); 
 96     printf("成功连接ip :%s  的客户端1\n",client_ip1); 
 97  
 98  
 99  
100 //链接第二个客户端 
101     //client2 接受连接成功的客户端ip,port 
102     struct sockaddr_in  client_addr2={0}; 
103     int len_client_addr2=  sizeof(client_addr2); 
104     int client2 = 0; 
105     short  client2_port2 = 0; 
106     printf("等待第二个客户端链接》》》\n"); 
107     client2 =  accept ( sockfd , (struct sockaddr *)&client_addr2 , &len_client_addr2 ); 
108     client2_port2 = client_addr2.sin_port; 
109      
110      
111     if(-1==client2) 
112     { 
113         perror("accept2"); 
114     } 
115     char client_ip2  [16]=""; 
116     inet_ntop(AF_INET,&client_addr2.sin_addr,client_ip2,16); 
117     printf("成功连接ip :%s  的客户端2\n",client_ip2); 
118  
119  
120 printf("——————传输信息的两个客户端连接成功——————\n"); 
121     //把第一个客户端的信息传递给第二个客户端 
122     if(-1==send(client2,client_ip1,16,0)) 
123     { 
124         perror("send1"); 
125         return -1; 
126     } 
127     if(-1==send(client2,&client1_port1,2,0)) 
128     { 
129         perror("send1"); 
130         return -1; 
131     } 
132     printf("——————成功把第一个客户端的信息传递给第二个客户端——————\n"); 
133  
134  
135     //把第二个客户端的信息传递给第1个客户端 
136     if(-1==send(client1,client_ip2,16,0)) 
137     { 
138         perror("send1"); 
139         return -1; 
140     } 
141     if(-1==send(client1,&client2_port2,2,0)) 
142     { 
143         perror("send1"); 
144         return -1; 
145     } 
146     printf("——————成功把第二个客户端的信息传递给第一个客户端——————\n"); 
147  
148  
149      
150     close(client1); 
151     close(client2); 
152     close(sockfd); 
153  
154     return 0; 
155 }
~                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值