C linux socket tcp封装库
# C linux socket tcp封装库
- 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
编辑 (opens new window)
上次更新: 2023/03/31, 22:34:04
- 01
- Linux系统移植(五)--- 制作、烧录镜像并启动Linux02-05
- 03
- Linux系统移植(三)--- Linux kernel移植02-05