epoll服务器客户端通信
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 //接受信息结构体
14 struct trr
15 {
16 int used;
17 char data[4096];
18 };
19 int main(int argc, const char *argv[])
20 {
21 char* p="0.0.0.0";
22 //创建服务器的socket套接字符
23 int sockfd=socket(AF_INET,SOCK_STREAM,0);
24 if(-1==sockfd)
25 {
26 perror("socket");
27 return -1;
28 }
29
30
31 //初始化了一个装有ip和pot的结构体
32 struct sockaddr_in add;
33
34
35 //设定为ipv4;
36 add.sin_family=AF_INET;
37
38
39 //端口号in_port_t
40 while(argc!=2)
41 {
42 printf("输入端口号
");
43 close(sockfd);
44 return -1;
45
46 }
47 short port= atoi(argv[1]);
48 add.sin_port=htons(port);
49
50
51 //ip
52 if(inet_pton(AF_INET,p,&add.sin_addr)<=0)
53 {
54 perror("inet_pton");
55 close(sockfd);
56 return -1;
57 }
58
59
60
61 //将装有ip和port的结构体写入创建的套接字文件
62 //服务器筛选接受信息的ip和端口号
63 if(-1==bind(sockfd,(struct sockaddr *)&add,sizeof(add)))
64 {
65 perror("bind");
66 }
67
68 //
69 if(-1==listen(sockfd,10))
70 {
71 perror("listen");
72 close(sockfd);
73 return -1;
74 }
75
76
77
78 printf("服务器已经启动,等待连接》》》
");
79
80 // 创建epoll文件实例
81 int epfd;
82 epfd = epoll_create1 ( EPOLL_CLOEXEC );
83 if( epfd == -1 )
84 {
85 perror("epoll_create");
86 return -1;
87 }
88 //
89 //把服务器的sockfd添加进epoll文件
90
91 struct epoll_event qwe;
92 memset(&qwe,0,sizeof(qwe));
93 qwe.events = EPOLLIN;
94 qwe.data.fd=sockfd;
95 if( -1 == epoll_ctl(epfd,EPOLL_CTL_ADD,sockfd,&qwe))
96 {
97 perror("epoll_ctl");
98 return -1;
99 }
100
101 //定义一个激活数组
102 struct epoll_event ev[20]= {0};
103
104
105
106 /*
107 int flags = fcntl(client,F_GETFL);
108 flags = flags | O_NONBLOCK;
109 fcntl(client,F_SETFL,flags);*/
110
111 while(1)
112 {
113 memset(ev,0,sizeof(ev));
114 //把激活的文件描述符结构体发在ev里
115 int len = epoll_wait(epfd,ev,20,-1);
116 if(-1 == len )
117 {
118 perror("wpoll_wait");
119 continue;
120 }
121 for(int i = 0; i <= len-1 ; i++ )
122 {
123 int temp = ev[i].data.fd;
124 if( temp == sockfd )
125 {
126
127 //client 接受连接成功的客户端ip,port
128 struct sockaddr_in client_addr={0};
129 int len_client_addr = sizeof(client_addr);
130
131 int client =0;
132 client = accept ( sockfd , (struct sockaddr *)&client_addr , &len_client_addr );
133 if(-1==client)
134 {
135 perror("accept");
136 }
137 char client_ip [16]="";
138 inet_ntop(AF_INET,&client_addr.sin_addr,client_ip,16);
139 printf("接收到的ip :%s
端口号 : %d
",client_ip,ntohs(client_addr.sin_port));
140
141
142 //把新链接的客户端套接字描述符添加进epfd
143 memset(&qwe,0,sizeof(qwe));
144 qwe.events = EPOLLIN ;
145 qwe.data.fd= client;
146 if( -1 == epoll_ctl(epfd,EPOLL_CTL_ADD,client,&qwe))
147 {
148 perror("epoll_ctl111");
149 continue;
150 }
151
152
153 }
154 else
155 {
156
157 char buf[64]="";
158 ssize_t rcv=read(temp,buf,63);
159 if(-1==rcv)
160 {
161 perror("read");
162 }
163 else if ( 0 == rcv )
164 {
165 printf("客户关闭了链接
");
166 if(-1== epoll_ctl( epfd , EPOLL_CTL_DEL, temp, NULL))
167 {
168 perror("epoll_ctl_DEL");
169 return -1;
170 }
171 continue;
172 }
173 else
174 {
175 if(strcmp(buf,"exit")==0)
176 {
177 printf("客户锻关闭了链接
");
178 if(-1== epoll_ctl( epfd , EPOLL_CTL_DEL, temp, NULL))
179 {
180 perror("epoll_ctl_DEL");
181 return -1;
182 }
183 continue;
184 }
185 printf("接收到 %ld 字节
",strlen(buf));
186 printf("收到的消息是 : %s
", buf);
187 printf("是否向客户端返回收到的消息
");
188 printf("是:1/否:2
");
189 char cmd;
190 while(1)
191 {
192 scanf("%c",&cmd);
193 if(cmd == '1')
194 {
195 //向客户端返回收到的信息
196 strcat(buf,"——...——");
197 ssize_t snd = send ( temp , buf , strlen(buf) , 0);
198 if( -1 == snd )
199 {
200 perror("send");
201 break;
202 }
203 break;
204 }
205 else if(cmd == '2' )
206 {
207 char fnh=' ';
208 ssize_t snd = send ( temp , &fnh , strlen(buf) , 0);
209 if( -1 == snd )
210 {
211 perror("send");
212 break;
213 }
214 break;
215
216 }
217 else
218 {
219 continue;
220 }
221 }
222 memset(buf,0,64);
223 }
224
225 }
226 }
227
228
229 }
230 close(sockfd);
231
232 return 0;
233 }
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 //传输的信息结构体
13 struct trr
14 {
15 int used;
16 char data[4096];
17 };
18 int main(int argc, const char *argv[])
19 {
20 //写入目标服务器的ip
21 char* p="192.168.186.130";
22
23
24
25 //创建客户端的socket套接字符
26 int client =socket(AF_INET,SOCK_STREAM,0);
27 if(client==-1)
28 {
29 perror("socket");
30 return -1;
31 }
32
33
34
35 //定义了一个装有ip和pot的结构体
36 struct sockaddr_in add;
37
38 //设定为ipv4;
39 add.sin_family=AF_INET;
40 //端口号in_port_t
41 while(argc!=2){printf("输入端口号
");close(client);return -1;}
42 short port= atoi(argv[1]);
43 add.sin_port=htons(port);
44
45
46
47 //ip
48 if(0>inet_pton(AF_INET,p,&add.sin_addr))
49 {
50 perror("inet_pton");
51 close(client);
52 return -1;
53 }
54
55 //将本客户端和服务器链接
56 if(-1==connect(client,(struct sockaddr*)&add,sizeof(add)))
57 {
58 perror("connect");
59 return -1;
60 }
61 //创建epoll文件实例
62 int epfd;
63 epfd = epoll_create1 ( EPOLL_CLOEXEC );
64 if( epfd == -1 )
65 {
66 perror("epoll_create1");
67 return -1;
68 }
69 //把本客户端的sockfd(client)和输入行缓冲区stdin加入epoll实例
70 struct epoll_event epoll_str;
71 memset(&epoll_str,0,sizeof(epoll_str));
72 epoll_str.events = EPOLLIN;
73 epoll_str.data.fd = client;
74 if ( -1 == epoll_ctl(epfd,EPOLL_CTL_ADD,client,&epoll_str))
75 {
76 perror("epoll_ctl-add");
77 return -1;
78 }
79 struct epoll_event epoll_stdin;
80 memset(&epoll_stdin,0,sizeof(epoll_stdin));
81 epoll_str.events = EPOLLIN;
82 epoll_str.data.fd = 0;
83 if ( -1 == epoll_ctl(epfd,EPOLL_CTL_ADD,0,&epoll_stdin))
84 {
85 perror("epoll_ctl-add");
86 return -1;
87 }
88 //定义一个激活数组
89 struct epoll_event ev[2];
90
91 while(1)
92 {
93 printf("输入传给服务器的信息
");
94 char buf[64]="";
95 scanf("%s",buf);
96
97 ssize_t aaa = send(client,buf,strlen(buf),0);
98 if(-1==aaa)
99 {
100 perror("read");
101 }
102 else if ( 0 == aaa )
103 {
104 printf("客户关闭了链接
");
105 break;
106 }
107 else
108 {
109 if(strcmp(buf,"exit")==0)
110 {
111 printf("关闭客户端
");
112 break;
113 }
114 }
115
116
117
118 memset(ev,0,sizeof(ev));
119 int len = epoll_wait(epfd, ev, 2, -1);
120 if(-1 == len )
121 {
122 perror("epoll_wait");
123 continue;
124 }
125 for(int i = 0; i
127
128 struct epoll_event temp = ev[i];
129 if (temp.data.fd == client)
130
131 {
132 memset(buf , 0 ,64);
133 ssize_t rcv = recv( client , buf ,63 ,0);
134 if(rcv ==-1)
135 {
136 perror("recv");
137 continue;
138 }
139 if(strlen(buf)==0)
140 {
141 printf("服务器未返回信息
");
142 break;
143 }
144 printf("收到服务器返回的信息
");
145 printf("%s
", buf );
146 }
147 }
148 }
149 return 0;
150 }