C linux socket - tcp - epoll
# C linux socket - tcp - select
# 封装原始socket库
- myconnect.h
#ifndef __MYCONNECT_H_
#define __MYCONNECT_H_
#include<sys/socket.h>
int Accept(int __fd, struct sockaddr *__restrict__ __addr, socklen_t *__restrict__ __addr_len);
int Bind(int __fd, const struct sockaddr *__addr, socklen_t __len);
int Connect(int __fd, const struct sockaddr *__addr, socklen_t __len);
int Listen(int __fd, int __n);
int Socket(int __domain, int __type, int __protocol);
ssize_t Read(int __fd, void *__buf, size_t __nbytes);
ssize_t Write(int __fd, const void *__buf, size_t __n);
int Close(int __fd);
ssize_t Readn(int __fd, void *__buf, size_t n);
ssize_t Writen(int __fd, const void *__buf, size_t n);
ssize_t Readline(int __fd, void *__buf, size_t maxlen);
#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- myconnect.c
#include"myconnect.h"
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
#include<sys/socket.h>
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
int Accept(int __fd, struct sockaddr *__restrict__ __addr, socklen_t *__restrict__ __addr_len)
{
int n;
again:
if ((n = accept(__fd, __addr, __addr_len)) < 0)
if((errno==ECONNABORTED)||(errno==EINTR))
goto again;
else
handle_error("accept");
return n;
}
int Bind(int __fd, const struct sockaddr *__addr, socklen_t __len)
{
int n;
if ((n = bind(__fd, __addr, __len)) < 0)
handle_error("bind");
return n;
}
int Connect(int __fd, const struct sockaddr *__addr, socklen_t __len)
{
int n;
;
if ((n = connect(__fd, __addr, __len)) < 0)
handle_error("connect");
return n;
}
int Listen(int __fd, int __n)
{
int n;
if ((n = listen(__fd, __n)) < 0)
handle_error("listen");
return n;
}
int Socket(int __domain, int __type, int __protocol)
{
int n;
if ((n = socket(__domain, __type, __protocol)) < 0)
handle_error("socket");
return n;
}
/**
* read返回值:
* 1.> 0 实际读到的字节数
* 2.= 0 数据读完(读到文件末尾、管道写端关闭、socket对端关闭)
* 3.-1 异常
* 1.errno==EINTR 被信号中断 重启/退出
* 2.errno==EAGAIN(EWOULDBLOCK) 非阻塞方式读,并且没有数据
* 3.其他情况 错误。---perror; exit;
*/
ssize_t Read(int __fd, void *__buf, size_t __nbytes)
{
ssize_t n;
again:
if ((n = read(__fd, __buf, __nbytes)) == -1)
if (errno == EINTR)
goto again;
else
return -1;
return n;
}
ssize_t Write(int __fd, const void *__buf, size_t __n)
{
ssize_t n;
again:
if((n=write(__fd,__buf,__n))==-1)
if(errno==EINTR)
goto again;
else
return -1;
return n;
}
int Close(int __fd)
{
int n;
if((n=close(__fd))==-1)
handle_error("close");
return n;
}
/**
* 读一定数量字符串,成功返回0,失败返回非零。
* size_t n:应该读取的字节数
*/
ssize_t Readn(int __fd, void *__buf, size_t n)
{
size_t nleft; //unsigned int 剩余未读取的字节数
ssize_t nread; //int 实际读到的字节数
char *ptr;
ptr = __buf;
nleft = n; //n 未读取字节数
while (nleft > 0)
{
if ((nread = read(__fd, ptr, nleft)) < 0)
{
if (errno == EINTR)
nread = 0;
else
return -1;
}
else if (nread == 0)
break;
nleft -= nread;
ptr += nread;
}
return n - nleft;
}
/*写一定数量字符串,成功返回0,失败返回-1或大于0的数*/
ssize_t Writen(int __fd, const void *__buf, size_t n)
{
size_t nleft;
ssize_t nwritten;
const char *ptr = __buf;
nleft = n;
while (nleft > 0)
{
if((nwritten = write(__fd, ptr, nleft))<0)
{
if (errno == EINTR)
nwritten = 0;
else
return -1;
}
else if (nwritten == 0)
break;
nleft -= nwritten;
ptr += nwritten;
}
return nleft;
}
/*供Readline()使用*/
static ssize_t my_read(int __fd,char*ptr)
{
static int read_cnt;
static char *read_ptr;
static char read_buff[100];
if (read_cnt <= 0)
{
again:
if ((read_cnt = read(__fd, read_buff, sizeof(read_buff))) < 0)
{
if(errno==EINTR)
goto again;
return -1;
}
else if(read_cnt==0)
return 0;
read_ptr = read_buff;
}
read_cnt--;
*ptr = *read_ptr++;
return 1;
}
/*调用my_read(),实现一次读一行,即读到\n结束,末尾\n\0。*/
ssize_t Readline(int __fd, void *__buf, size_t maxlen)
{
ssize_t n, rc;
char c, *ptr;
ptr = __buf;
for (n = 1; n < maxlen; n++)
{
if ((rc = my_read(__fd, &c)) == 1)
{
*ptr++ = c;
if(c=='\n')
break;
}
else if(rc==0)
{
*ptr = 0;
return n - 1;
}else
return -1;
}
*ptr = 0;
return n;
}
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
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
# 调用-client
- client.c
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<ctype.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<string.h>
#include"myconnect.h"
#define SERV_PORT 6868
#define SERV_IP "192.168211.129"
int main()
{
int cfd;
struct sockaddr_in serv_addr;
char buf[BUFSIZ];
cfd = Socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERV_PORT);
inet_pton(AF_INET, SERV_IP, &serv_addr.sin_addr.s_addr);
Connect(cfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
while(1)
{
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), stdin);
Write(cfd, buf, strlen(buf));
int n=Read(cfd, buf, sizeof(buf));
if (n > 0)
printf("%s", buf);
else
break;
}
close(cfd);
return 0;
}
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
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
# 调用-server
- server.c
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/epoll.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<errno.h>
#include <arpa/inet.h>
#include<unistd.h>
#include<ctype.h>
#include"myconnect.h"
#include<string.h>
#include<time.h>
#define SERV_PORT 6868
#define OPEN_MAX 5000
int main()
{
int lfd, cfd, sockfd, i, n, num = 0; /*listenfd connectfd*/
ssize_t nready, efd, res;
char clie_ip[INET_ADDRSTRLEN], buf[BUFSIZ]; /*客户端IP、传输字符串*/
struct sockaddr_in serv_addr, clie_addr;
socklen_t clie_addr_len;
struct epoll_event tep, ep[OPEN_MAX]; /*tep:epoll_ctl参数 ep[]:epoll_wait参数*/
lfd = Socket(AF_INET, SOCK_STREAM, 0);
int opt = 1;
setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); /*端口复用*/
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERV_PORT);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
Bind(lfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
Listen(lfd, 10);
efd = epoll_create(OPEN_MAX); /*创建epoll模型,efd为红黑树根节点*/
if(efd==-1)
{
perror("epoll_create");
exit(1);
}
tep.events = EPOLLIN;
tep.data.fd = lfd;
res = epoll_ctl(efd, EPOLL_CTL_ADD, lfd, &tep); /*将lfd及对应结构体设置到树上*/
if(res==-1)
{
perror("epoll_ctl");
exit(1);
}
while(1)
{
/*epoll为server阻塞监听事件,ep为struct epoll_event类型数组,OPEN_MAX为数组容量,-1表永久阻塞*/
nready = epoll_wait(efd, ep, OPEN_MAX, -1);
if(nready==-1)
{
perror("epoll_wait");
exit(0);
}
for (i = 0; i < nready; i++)
{
if (!(ep[i].events & EPOLLIN)) /*如果不是读事件继续循环*/
continue;
if (ep[i].data.fd == lfd) /*满足的是lfd*/
{
printf("lfd %d\n", lfd);
clie_addr_len = sizeof(clie_addr);
cfd = Accept(lfd, (struct sockaddr *)&clie_addr, &clie_addr_len);
printf("New Client, IP:%s,PORT:%d\n", inet_ntop(AF_INET, &clie_addr.sin_addr.s_addr,
clie_ip, sizeof(clie_ip)), ntohs(clie_addr.sin_port));
printf("cfd %d---client %d\n", cfd, ++num);
tep.events = EPOLLIN;
tep.data.fd = cfd;
res = epoll_ctl(efd, EPOLL_CTL_ADD, cfd, &tep);
if(res==-1)
{
perror("cfd epoll_ctl");
exit(1);
}
}
else
{
sockfd = ep[i].data.fd;
memset(buf, 0, sizeof(buf));
if ((n = Read(sockfd, buf, sizeof(buf))) == 0)
{
res = epoll_ctl(efd, EPOLL_CTL_DEL, sockfd, NULL);
if(res==-1)
{
perror("epoll_ctl");
exit(1);
}
Close(sockfd);
printf("client[%d] closed connection\n", sockfd);
}
else if(n<0)
{
perror("read n<0 error:");
res = epoll_ctl(efd, EPOLL_CTL_DEL, sockfd, NULL);
if(res==-1)
{
perror("epoll_ctl");
exit(1);
}
Close(sockfd);
}
else if(n>0)
{
printf("%d 个字符已读入:%s", n, buf);
for (i = 0; i < n; i++)
{
buf[i] = toupper(buf[i]);
}
//sleep(10);
Write(sockfd, buf, n);
}
}
}
}
Close(lfd);
Close(efd);
return 0;
}
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
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
# makefile
- makefile
all: server client
server: server.c myconnect.o
gcc server.c myconnect.o -o server
client: client.c myconnect.o
gcc client.c myconnect.o -o client
myconnect.o:myconnect.c
gcc -c myconnect.c
clean:
rm *.o server client
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 编译
#编译
make
#清除编译文件
#make clean
1
2
3
4
2
3
4
先执行server,再执行client,效果是client输入小写,server转大写后发送回client。
编辑 (opens new window)
上次更新: 2023/03/31, 22:34:04
- 01
- Linux系统移植(五)--- 制作、烧录镜像并启动Linux02-05
- 03
- Linux系统移植(三)--- Linux kernel移植02-05