通用的SOCKET类
这次又要到Linux下做了,把之前的自己写的socket类改成了通用的,当然目前的结构还是太简单了,很多以前封装的不符合面向对象的函数又被我砍掉了,慢慢完善吧~~
Socket.h 1 #if !defined(__DZLIB_SOCKET_H__)
2 #define __DZLIB_SOCKET_H__
3
4 #if defined(WIN32)
5 # include <WINSOCK2.H>
6 #else
7 # include <stdio.h>
8 # include <netinet/in.h>
9 #endif
10
11 // -----------------------------------------------------------------------------
12 // namespace DZLIB -------------------------------------------------------------
13 namespace DZLIB
14 {
15
16
17 // -----------------------------------------------------------------------------
18 // class SocketEnvironment -----------------------------------------------------
19 #if defined(WIN32)
20 class SocketEnvironment
21 {
22 public:
23 // constructors and destructors
24 SocketEnvironment();
25 ~SocketEnvironment();
26
27 // static funcs
28 static bool Init();
29 static bool Exit();
30 };
31 #endif
32
33
34 // -----------------------------------------------------------------------------
35 // class SocketAddr ------------------------------------------------------------
36 class SocketAddr
37 {
38 friend class TcpSocket;
39 friend class UdpSocket;
40
41 public:
42 // constructors and destructors
43 SocketAddr();
44 SocketAddr(u_short port, const char* cIp);
45 SocketAddr(u_short port, u_long nIp);
46
47 // public funcs
48 void SetStrIP(const char* cIp);
49 void SetNetIP(u_long nIp);
50 void SetPort(u_short port);
51 char* GetStrIP() const;
52 u_short GetPort() const;
53
54 // operations
55 operator sockaddr*();
56 operator sockaddr*() const;
57
58 private:
59 // properties
60 short _family;
61 u_short _port;
62 u_long _ip;
63 char _zero[8];
64 };
65
66
67 // -----------------------------------------------------------------------------
68 // base class Socket -----------------------------------------------------------
69 class Socket
70 {
71 public:
72 // constructors and destructors
73 Socket();
74 Socket(u_int sockfd);
75 virtual ~Socket();
76
77 // pure virtual funcs
78 virtual int Create() = 0;
79 virtual int Create(u_short port, const char* cIp = NULL) = 0;
80
81 // public funcs
82 int Bind(u_short port, const char* cIp = NULL);
83 int Close();
84 int SetSockOpt(int optname, const void* optval, int optlen, int level = SOL_SOCKET);
85 int GetSockOpt(int optname, void* optval, int& optlen, int level = SOL_SOCKET);
86
87 // operations
88 operator u_int();
89
90 protected:
91 // protected defines
92 #if defined(WIN32)
93 typedef int SOCKLEN;
94 #else
95 typedef u_int SOCKLEN;
96 #endif
97 // protected properties
98 u_int _sockfd;
99 };
100
101
102 // -----------------------------------------------------------------------------
103 // class TcpSocket : Socket ----------------------------------------------------
104 class TcpSocket : public Socket
105 {
106 public:
107 // constructors
108 TcpSocket();
109 TcpSocket(u_int sockfd);
110
111 // public funcs
112 int Create();
113 int Create(u_short port, const char* cIp = NULL);
114 int Listen(int backlog = 10);
115 int Accept(TcpSocket& peer);
116 int Accept(TcpSocket& peer, SocketAddr& peerAddr);
117 int Connect(u_long nIp, u_short port);
118 int Connect(const char* cIp, u_short port);
119 int Connect(const SocketAddr& addr);
120 int Send(const void* buf, u_int len);
121 int Recv(void* buf, u_int len);
122 };
123
124
125 // -----------------------------------------------------------------------------
126 // class UdpSocket : Socket ----------------------------------------------------
127 class UdpSocket : public Socket
128 {
129 public:
130 // constructors
131 UdpSocket();
132 UdpSocket(u_int sockfd);
133
134 // public funcs
135 int Create();
136 int Create(u_short port, const char* cIp = NULL);
137 int SendTo(const void* buf, u_int len, u_long nIp, u_short port);
138 int SendTo(const void* buf, u_int len, const char* cIp, u_short port);
139 int SendTo(const void* buf, u_int len, const SocketAddr& to);
140 int RecvFrom(void* buf, u_int len);
141 int RecvFrom(void* buf, u_int len, SocketAddr& from);
142 };
143
144
145 } // namespace DZLIB
146
147 #endif // __DZLIB_SOCKET_H__
148
2 #define __DZLIB_SOCKET_H__
3
4 #if defined(WIN32)
5 # include <WINSOCK2.H>
6 #else
7 # include <stdio.h>
8 # include <netinet/in.h>
9 #endif
10
11 // -----------------------------------------------------------------------------
12 // namespace DZLIB -------------------------------------------------------------
13 namespace DZLIB
14 {
15
16
17 // -----------------------------------------------------------------------------
18 // class SocketEnvironment -----------------------------------------------------
19 #if defined(WIN32)
20 class SocketEnvironment
21 {
22 public:
23 // constructors and destructors
24 SocketEnvironment();
25 ~SocketEnvironment();
26
27 // static funcs
28 static bool Init();
29 static bool Exit();
30 };
31 #endif
32
33
34 // -----------------------------------------------------------------------------
35 // class SocketAddr ------------------------------------------------------------
36 class SocketAddr
37 {
38 friend class TcpSocket;
39 friend class UdpSocket;
40
41 public:
42 // constructors and destructors
43 SocketAddr();
44 SocketAddr(u_short port, const char* cIp);
45 SocketAddr(u_short port, u_long nIp);
46
47 // public funcs
48 void SetStrIP(const char* cIp);
49 void SetNetIP(u_long nIp);
50 void SetPort(u_short port);
51 char* GetStrIP() const;
52 u_short GetPort() const;
53
54 // operations
55 operator sockaddr*();
56 operator sockaddr*() const;
57
58 private:
59 // properties
60 short _family;
61 u_short _port;
62 u_long _ip;
63 char _zero[8];
64 };
65
66
67 // -----------------------------------------------------------------------------
68 // base class Socket -----------------------------------------------------------
69 class Socket
70 {
71 public:
72 // constructors and destructors
73 Socket();
74 Socket(u_int sockfd);
75 virtual ~Socket();
76
77 // pure virtual funcs
78 virtual int Create() = 0;
79 virtual int Create(u_short port, const char* cIp = NULL) = 0;
80
81 // public funcs
82 int Bind(u_short port, const char* cIp = NULL);
83 int Close();
84 int SetSockOpt(int optname, const void* optval, int optlen, int level = SOL_SOCKET);
85 int GetSockOpt(int optname, void* optval, int& optlen, int level = SOL_SOCKET);
86
87 // operations
88 operator u_int();
89
90 protected:
91 // protected defines
92 #if defined(WIN32)
93 typedef int SOCKLEN;
94 #else
95 typedef u_int SOCKLEN;
96 #endif
97 // protected properties
98 u_int _sockfd;
99 };
100
101
102 // -----------------------------------------------------------------------------
103 // class TcpSocket : Socket ----------------------------------------------------
104 class TcpSocket : public Socket
105 {
106 public:
107 // constructors
108 TcpSocket();
109 TcpSocket(u_int sockfd);
110
111 // public funcs
112 int Create();
113 int Create(u_short port, const char* cIp = NULL);
114 int Listen(int backlog = 10);
115 int Accept(TcpSocket& peer);
116 int Accept(TcpSocket& peer, SocketAddr& peerAddr);
117 int Connect(u_long nIp, u_short port);
118 int Connect(const char* cIp, u_short port);
119 int Connect(const SocketAddr& addr);
120 int Send(const void* buf, u_int len);
121 int Recv(void* buf, u_int len);
122 };
123
124
125 // -----------------------------------------------------------------------------
126 // class UdpSocket : Socket ----------------------------------------------------
127 class UdpSocket : public Socket
128 {
129 public:
130 // constructors
131 UdpSocket();
132 UdpSocket(u_int sockfd);
133
134 // public funcs
135 int Create();
136 int Create(u_short port, const char* cIp = NULL);
137 int SendTo(const void* buf, u_int len, u_long nIp, u_short port);
138 int SendTo(const void* buf, u_int len, const char* cIp, u_short port);
139 int SendTo(const void* buf, u_int len, const SocketAddr& to);
140 int RecvFrom(void* buf, u_int len);
141 int RecvFrom(void* buf, u_int len, SocketAddr& from);
142 };
143
144
145 } // namespace DZLIB
146
147 #endif // __DZLIB_SOCKET_H__
148

浙公网安备 33010602011771号