-
Notifications
You must be signed in to change notification settings - Fork 0
/
Network.h
151 lines (140 loc) · 5.23 KB
/
Network.h
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
///////////////////////////////////////////////////////////
// Network.h
// Implementation of the Class Network
// Created on: 14-Ò»ÔÂ-2008 16:31:53
// Original author: Aitec
///////////////////////////////////////////////////////////
#if !defined(EA_76114261_477A_4b62_88C9_C98E8452015D__INCLUDED_)
#define EA_76114261_477A_4b62_88C9_C98E8452015D__INCLUDED_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __linux__
#include <unistd.h>
#include <errno.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/un.h>
#include <sys/stat.h>
#elif _WIN32 /* win32 */
#include <winsock2.h>
#include <ws2tcpip.h>
#define ssize_t unsigned int
#define u_int8_t unsigned char
#define u_int16_t unsigned short
#define u_int32_t unsigned int
#define socklen_t int
#else /* other os */
#include <unistd.h>
#include <errno.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/un.h>
#endif
#ifndef UNIX_PATH_MAX
#define UNIX_PATH_MAX 108
#endif
#ifndef IPADDR_LEN
#define IPADDR_LEN 24
#endif
#ifndef RET_SUCCESS
#define RET_SUCCESS 0
#endif
#ifndef RET_ERROR
#define RET_ERROR -1
#endif
class Network
{
public:
// tcp
static int MakeTCPServer ( u_int16_t port );
static int MakeTCPClient ( const char* ipaddr, u_int16_t port );
static int MakeTCPClient ( const char* ipaddr, u_int16_t port, bool const& cancel );
static int MakeTCPClient ( const char* ipaddr, u_int16_t port, int msec );
static int MakeTCPClient ( u_int32_t ipaddr, u_int16_t port );
static int MakeTCPClient ( u_int32_t ipaddr, u_int16_t port, bool const& cancel );
static int MakeTCPClient ( u_int32_t ipaddr, u_int16_t port, int msec );
public:
// udp
static int MakeUDPServer ( u_int16_t port );
static int MakeUDPClient();
static int MakeBcClient();//broadcast client
#ifdef __linux__
public:
// unix
static int MakeUnixServer ( const char* filename );
static int MakeUnixClient ( const char* filename );
#endif
public:
// tcp and unix
static int MakeAccept ( int listenfd );
static int MakeAccept ( int listenfd, struct sockaddr* saddr );
public:
// tcp¡¢udp and unix
static int Select ( int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, struct timeval* tv );
public:
// tcp and unix io
static ssize_t Sendn ( int fd, const void* vptr, ssize_t n );
static ssize_t Recvn ( int fd, void* vptr, ssize_t n );
public:
// udp io
static ssize_t SendTo ( int sockfd, const void* vptr, ssize_t n, int flags, const struct sockaddr* addr, socklen_t len );
static ssize_t RecvFrom ( int sockfd, void* vptr, ssize_t n, int flags, struct sockaddr* addr, socklen_t* len );
static ssize_t RecvFrom ( int sockfd, void* vptr, ssize_t n, int flags );
public:
// tcp¡¢udp and unix
static int Close ( int sockfd );
public:
// udp
static int JoinMultiCast ( int sockfd, struct ip_mreq* command );
static int QuitMultiCast ( int sockfd, struct ip_mreq* command );
public:
// tcp¡¢udp and unix
static int SetNonBlock ( int sockfd );
static int SetBlock ( int sockfd );
public:
// tcp and unix
static int SetNonKeepLive ( int sockfd );
static int SetKeepLive ( int sockfd, int idle, int intvl, int cnt );
public:
// tcp¡¢udp and unix
static int SetSendBuf ( int sockfd, int Size );
static int SetRecvBuf ( int sockfd, int Size );
public:
// tcp and unix
static int SetNonDelay ( int sockfd );
static int SetDelay ( int sockfd );
public:
//tcp
static int SetLinger ( int sockfd );
public:
//udp broadcast
static int SetSockAddr ( struct sockaddr_in* socketAddr, int port );
public:
// tcp and unix
static bool GetLocalIp ( int sockfd, char* ipaddr );
static bool GetRemoteIp ( int sockfd, char* ipaddr );
private:
static int Connect1 ( const char* ipaddr, u_int16_t port, int msec, const bool& cancel );
static int Connect1 ( u_int32_t ipaddr, u_int16_t port, int msec, const bool& cancel );
static int Connect2 ( u_int32_t ipaddr, u_int16_t port, int msec, const bool& cancel );
static int Connect3 ( int sockfd, const struct sockaddr* saptr, socklen_t salen, int msec, const bool& cancel );
public:
static int MakeInetServer ( u_int16_t port );
static int MakeInetClient ( char* ipaddr, u_int16_t port );
static int MakeInetClient ( char* ipaddr, u_int16_t port, int const& cancel );
static int MakeInetClientFast ( char* ipaddr, u_int16_t port, int waitmilliseconds );
static ssize_t Readn ( int fd, void* vptr, ssize_t n );
static ssize_t Writen ( int fd, void* vptr, ssize_t n );
};
#endif // !defined(EA_76114261_477A_4b62_88C9_C98E8452015D__INCLUDED_)