-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.c
88 lines (75 loc) · 1.53 KB
/
helper.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
#include <helper.h>
int esocket(int domain,int type,int protocol)
{
int fd;
fd=socket(domain,type,protocol);
if (fd==-1)
{
fprintf(stderr,"Error: socket Error\n");
fprintf(stderr,"Error: %s\n",strerror(errno));
exit(0);
}
return fd;
}
int ebind(int sockfd,const struct sockaddr_in *addr,int len)
{
int ret;
ret=bind(sockfd,(struct sockaddr*)(addr),(socklen_t)(len));
if (ret==-1)
{
fprintf(stderr,"Error: bind Error\n");
fprintf(stderr,"Error: %s\n",strerror(errno));
exit(0);
}
return ret;
}
int elisten(int sockfd,int backlog)
{
int ret;
ret=listen(sockfd,backlog);
if (ret==-1)
{
fprintf(stderr,"Error: listen Error\n");
fprintf(stderr,"Error: %s\n",strerror(errno));
exit(0);
}
return ret;
}
int eaccept(int sockfd,struct sockaddr_in *addr,int *addrlen)
{
int ret;
socklen_t len;
len=*addrlen;
ret=accept(sockfd,(struct sockaddr*)(addr),&len);
*addrlen=len;
if (ret==-1)
{
fprintf(stderr,"Error: accept Error\n");
fprintf(stderr,"Error: %s\n",strerror(errno));
exit(0);
}
return ret;
}
int econnect(int sockfd,struct sockaddr_in *addr)
{
int ret;
int len=sizeof(struct sockaddr_in);
ret=connect(sockfd,(struct sockaddr*)(addr),len);
if (ret==-1)
{
fprintf(stderr,"Error: connect Error\n");
fprintf(stderr,"Error: %s\n",strerror(errno));
exit(0);
}
return ret;
}
struct sockaddr_in mkaddr(char *ipbydot,int port)
{
struct in_addr in;
struct sockaddr_in addr;
inet_pton(AF_INET,ipbydot,&in);
addr.sin_family=AF_INET;
addr.sin_addr=in;
addr.sin_port=htons(port);
return addr;
}