-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
184 lines (155 loc) · 4.84 KB
/
client.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
//客户端
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/epoll.h>
#include <assert.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <pthread.h>
int sfd; //socket描述符
int size; //读取和发送文件的长度
int r; //函数返回值
int len; //要发送的文件名的长度
char buf[128]; //数据的缓存
struct sockaddr_in dr; //网络地址
struct epoll_event ev, events[20]; //ev用于注册事件,events用于回传要处理的事件
int epid = 0;
pthread_t pid,pid2;
char command_buff[512];
int sigign() {
struct sigaction sa;
sa.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &sa, 0);
return 0;
}
int
firefly_start(){
printf("*******firefly client start*****\n");
printf("***list:show all online players*\n");
printf("***send:send one player message*\n");
}
void*
main_loop(void* data){
for (;;)
{
firefly_start();
//scanf("%s",command_buff);
fgets(command_buff,512,stdin);
//printf("%s\n",command_buff);
fputs(command_buff,stdout);
printf("len:%d\n",strlen(command_buff) );
int r = 0;
int a = strlen(command_buff)+1;
command_buff[a] ='\0';
r = send(sfd,&a,sizeof(a),MSG_WAITALL);
if (r < 0)
{
printf("send len error\n");
continue;
}
r = send(sfd,command_buff,strlen(command_buff)+1,MSG_WAITALL);
if (r < 0)
{
printf("send buff error\n");
continue;
}
}
}
void*
run(void* data){
for (;;)
{
int nfds = epoll_wait(epid,events,20,500);
for (int i =0; i < nfds; ++i )
{
printf("has event :%d\n",nfds);
if(events[i].events & EPOLLOUT){ //数据向外发送 //关闭的时候,也会走这个分支
printf("EPOLLOUT\n");
int a = 10;
r = send(events[i].data.fd,&a,sizeof(a),MSG_WAITALL); //2次给空的fd发消息,程序就会崩溃
if(r == -1){
//关闭socket
printf("socket close\n");
printf("2:%m\n"),close(sfd),exit(-1);
}
r = send(events[i].data.fd,"abcdefghig",10,MSG_WAITALL); //2次给空的fd发消息,程序就会崩溃
if(r == -1){
//关闭socket
printf("socket close\n");
printf("3:%m\n"),close(sfd),exit(-1);
}
printf("write success!\n");
struct epoll_event ev;
ev.data.fd = sfd;
ev.events = EPOLLIN|EPOLLET;//发送事件
epoll_ctl(epid,EPOLL_CTL_MOD,sfd,&ev);//事件为读
}
if (events[i].events & EPOLLIN){ //数据读
printf("EPOLLIN\n");
int n;
int a;
n = read(sfd,&a,sizeof(a));
if (n < 0)
{
printf("read error\n");
}
char* buff = (char*)malloc(a+1);
n = read(sfd,buff,a);
if (n < 0)
{
printf("read error\n");
}
printf("data:%d:%s\n", a,buff);
}
}
}
}
int
main(int argc, char const *argv[]){
sigign();
// int sfd; //socket描述符
// int size; //读取和发送文件的长度
// int r; //函数返回值
// int len; //要发送的文件名的长度
// char buf[128]; //数据的缓存
// struct sockaddr_in dr; //网络地址
// struct epoll_event ev, events[20]; //ev用于注册事件,events用于回传要处理的事件
epid = epoll_create(256);
//1.建立socket
sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd==-1)
printf("1:%m\n"),exit(-1);
printf("socket成功!\n");
ev.data.fd = sfd;
ev.events = EPOLLIN|EPOLLET;//发送事件
epoll_ctl(epid,EPOLL_CTL_ADD,sfd,&ev);//事件为读
//2.连接到服务器
dr.sin_family=AF_INET;
dr.sin_port=htons(8888);
inet_aton("192.168.1.18",&dr.sin_addr);
r=connect(sfd,(struct sockaddr*)&dr,sizeof(dr));
if(r==-1)
printf("2:%m\n"),close(sfd),exit(-1);
printf("connect成功!\n");
//发送到服务器
// int a = 10;
// r = send(sfd,&a,sizeof(a),0);
// if(r == -1){
// printf("2:%m\n"),close(sfd),exit(-1);
// }
//连接成功后,把sfd放入epoll进行管理
pthread_create(&pid,0,run,0); //接收消息
pthread_create(&pid2,0,main_loop,0); //发送消息
pthread_join(pid,(void**)NULL);
pthread_join(pid2,(void**)NULL);
failed:
//6.读取到文件尾,发送0数据包
close(sfd);
printf("OK!\n");
}