forked from antdimarino/Computer-Networks-BlockChain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapper.h
More file actions
117 lines (108 loc) · 2.62 KB
/
Copy pathwrapper.h
File metadata and controls
117 lines (108 loc) · 2.62 KB
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
#include<sys/types.h> /* predefined types */
#include<unistd.h> /* include unix standard library */
#include<arpa/inet.h> /* IP addresses conversion utililites */
#include<sys/socket.h> /* socket library */
#include<string.h>
#include<time.h>
#include<netdb.h>
#include<errno.h>
#include<sys/stat.h>
#include<pthread.h>
#include<semaphore.h>
#include<fcntl.h>
#include<signal.h>
ssize_t FullWrite(int fd, const void *buf, size_t count);
ssize_t FullRead(int fd, void *buf, size_t count);
int Accept(int socket,struct sockaddr *addr, socklen_t *addr_len);
void Listen(int socket,int backlog);
void Bind(int socket,struct sockaddr *addr,socklen_t addr_len);
int Connect(int socket,struct sockaddr *addr,socklen_t addr_len);
pid_t Fork();
int Socket(int domain,int type,int protocol)
{
int fd;
if((fd=socket(domain,type,protocol))<0){
perror("socket error");
exit(1);
}
return fd;
}
int Connect(int socket,struct sockaddr *addr,socklen_t addr_len)
{
if(connect(socket,addr,addr_len)<0){
perror("Connect error");
return -1;
}
return 0;
}
void Bind(int socket,struct sockaddr *addr,socklen_t addr_len)
{
if(bind(socket,addr,addr_len)<0){
perror("bind error");
exit(1);
}
}
void Listen(int socket,int backlog)
{
if(listen(socket,backlog)<0){
perror("listen error");
exit(1);
}
}
int Accept(int socket,struct sockaddr *addr, socklen_t *addr_len)
{
int connfd;
if((connfd=accept(socket,addr,addr_len))<0){
perror("accept error");
exit(1);
}
}
ssize_t FullRead(int fd, void *buf, size_t count)
{
size_t nleft;
ssize_t nread;
nleft = count;
while (nleft > 0) { /* repeat until no left */
if((nread = read(fd,buf,nleft))<0){
if(errno == EINTR){
continue;
} else{
exit(nread);
}
}else if (nread==0){
nleft = -1;
break;
}
nleft-=nread;
buf+=nread;
}
buf = 0;
return (nleft);
}
ssize_t FullWrite(int fd, const void *buf, size_t count)
{
size_t nleft;
ssize_t nwritten;
nleft = count;
while (nleft > 0) { /* repeat until no left */
if((nwritten = write(fd,buf,nleft))<0){
if(errno==EINTR){
continue;
}else{
exit(nwritten);
}
}
nleft -= nwritten;
buf+=nwritten;
}
return (nleft);
}
pid_t Fork(){
pid_t pid;
if((pid= fork())<0)
{
perror ("fork error: ");
exit ( -1);
}
return pid;
}