-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat_cli.c
220 lines (217 loc) · 5.86 KB
/
chat_cli.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*************************************************************************
> File Name: chat_cli.c
> Author:
> Mail:
> Created Time: 2017年11月29日 星期三 09时48分36秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<errno.h>
#include<sys/epoll.h>
#include<fcntl.h>
#define MAX 1024
#define PORT 12345
typedef struct info
{
int fd;
char name[20];
int flag;
int ffd;//朋友的文件描述符
char fname[20];
char message[MAX];
}Info;
Info my;
char a[4] = "exit";
char b[2] = "ok";
char buff[MAX];
char Menu()
{
char choice;
printf("\t***********欢迎进入聊天系统************\n");
printf("\t***********1.单人聊天******************\n");
printf("\t***********2.群组聊天******************\n");
printf("\t***********3.退出**********************\n");
printf("\t***************************************\n");
while(1)
{
printf("\n\t请输入您的选择:\n");
scanf("%c",&choice);
char c_tmp;
while((c_tmp = getchar()) != '\n' && c_tmp !=EOF);
if(choice == '1' || choice == '2' ||choice == '3')
{
return choice;
}
else
{
printf("\t您输入的选择有误,请重新输入!\n");
continue;
}
}
}
void Select(int sockfd)
{
char fname[20] = "";
char choice = Menu();
switch(choice)
{
case '1':
my.flag = 1;
printf("\t欢迎进入一对一聊天\n");
printf("\t请输入好友昵称:\n");
scanf("%s",my.fname);
char c_tmp;
while((c_tmp = getchar()) != '\n' && c_tmp !=EOF);
break;
case '2':
my.flag = 2;
printf("\t欢迎进入多人群组聊天\n");
break;
case '3':
my.flag = 3;
exit(1);
default:
exit(0);
}
}
int init()
{
int sock = 0;
if((sock = socket(PF_INET,SOCK_STREAM,0)) < 0)
perror("socket");
struct sockaddr_in serveraddr;
bzero(&serveraddr,0);
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
serveraddr.sin_port = htons(PORT);
if(connect(sock,(struct sockaddr*)&serveraddr,sizeof(serveraddr)) < 0)
perror("connect");
return sock;
}
int setnonblocking(int fd)
{
int old_option;
if((old_option = fcntl(fd,F_GETFL)) < 0)
perror("fcntl(fd,F_GETFL)");
int new_option = old_option | O_NONBLOCK;
if(fcntl(fd,F_SETFL,new_option) < 0)
perror("fcntl(fd,F_SET,new_option)");
return old_option;
}
void handleSession(int epollfd,int ret,struct epoll_event *triggerEvents,int sock,int std)
{
char recvbuff[MAX+20];
char sendbuff[MAX];
for(int i = 0; i < ret; i++)
{
int sockfd = triggerEvents[i].data.fd;
if(sockfd == sock && (triggerEvents[i].events & EPOLLIN))
{
//printf("read\n");
memset(recvbuff,0,sizeof(recvbuff));
int ret = recv(sock,recvbuff,sizeof(recvbuff),0);
if(ret < 0)
perror("recv");
else if(ret == 0)
{
printf("server close\n");
exit(EXIT_SUCCESS);
}
fputs(recvbuff,stdout);
memset(recvbuff,0,sizeof(recvbuff));
}
else if((sockfd == std)&&(triggerEvents[i].events & EPOLLIN))
{
//printf("write\n");
//memset(my.message,0,sizeof(my.message));
memset(sendbuff,0,sizeof(sendbuff));
if(fgets(sendbuff,sizeof(sendbuff),stdin) == NULL)
break;
int len = strlen(sendbuff);
strncpy(my.message,sendbuff,len);
my.message[len] = '\0';
//printf("%s\n",my.message);
send(sock,&my,sizeof(my),0);
memset(sendbuff,0,sizeof(sendbuff));
}
else
continue;
}
}
void Verify(int sockfd)
{
char name[20];
while(1)//验证身份
{
printf("\t请输入您的用户名:\n");
scanf("%s",name);
getchar();
send(sockfd,name,strlen(name),0);
memset(buff,0,sizeof(buff));
recv(sockfd,buff,sizeof(buff),0);
if(strncmp(buff,a,4) == 0)//与exit匹配
{
printf("\t当前用户已存在!请重新输入\n");
}
if(strncmp(buff,b,2) == 0)//与ok匹配
{
strcpy(my.name,name);
break;
}
}
}
void Clear()
{
my.ffd = -1;
memset(my.message,0,sizeof(my.message));
}
int main()
{
char name[20];
my.fd = -1;
my.flag = -1;
memset(my.fname,0,sizeof(my.fname));
Clear();
int epollfd = epoll_create(MAX);
struct epoll_event registerEvents;
struct epoll_event triggerEvents[2];
int sockfd = init();
my.fd = sockfd;
//printf("请输入您的用户名:\n");
//scanf("%s",name);
//getchar();
strcpy(my.name,name);
Verify(sockfd);//验证并填入用户名
Select(sockfd);//进行功能选择
registerEvents.data.fd = sockfd;
registerEvents.events = EPOLLIN;
epoll_ctl(epollfd,EPOLL_CTL_ADD,registerEvents.data.fd,®isterEvents);//注册读取连接事件
//setnonblocking(sockfd);
int std = fileno(stdin);
registerEvents.data.fd = std;
registerEvents.events = EPOLLIN;
//注册从标准输入读入事件
epoll_ctl(epollfd,EPOLL_CTL_ADD,registerEvents.data.fd,®isterEvents);
//setnonblocking(std);
while(1)
{
int ret = epoll_wait(epollfd,triggerEvents,MAX,-1);
//printf("ret = %d\n",ret);
if(ret < 0)
{
printf("epoll fail\n");
break;
}
handleSession(epollfd,ret,triggerEvents,sockfd,std);
Clear();
//printf("exit\n");
}
close(sockfd);
return 0;
}