-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathEasysocket.h
More file actions
236 lines (183 loc) · 9.12 KB
/
Copy pathEasysocket.h
File metadata and controls
236 lines (183 loc) · 9.12 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// This file contains the definitions for the three socket classes: Basic, Data, and Listening.
#ifndef EASYSOCKET_H
#define EASYSOCKET_H
// ========================================================================
// Include Files
// ========================================================================
#include "SocketTypes.h"
#include "SocketErrors.h"
namespace SocketLib
{
// ========================================================================
// Class: Socket
// Purpose: A very basic socket base class that will give the user
// the ability to get port and IP information, but not much
// else.
// ========================================================================
class Socket
{
public:
// ====================================================================
// Function: GetSock
// Purpose: this function returns the internal API socket
// representation. Used by classes and functions that need
// to interface directly with the BSD Sockets or Winsock
// libraries.
// ====================================================================
inline sock GetSock() const
{
return m_sock;
}
// ====================================================================
// Function: GetLocalPort
// Purpose: gets the local port of the socket
// ====================================================================
inline port GetLocalPort() const
{
return ntohs( m_localinfo.sin_port );
}
// ====================================================================
// Function: GetLocalAddress
// Purpose: gets the local address of the socket
// ====================================================================
inline ipaddress GetLocalAddress() const
{
return m_localinfo.sin_addr.s_addr;
}
// ====================================================================
// Function: Close
// Purpose: closes the socket.
// ====================================================================
void Close();
// ====================================================================
// Function: SetBlocking
// Purpose: sets whether the socket is blocking or not.
// ====================================================================
void SetBlocking( bool p_blockmode );
protected:
// ====================================================================
// Function: Socket
// Purpose: hidden constructor, meant to prevent people from
// instantiating this class. You should be using direct
// implementations of this class instead, such as
// ListeningSocket and DataSocket.
// ====================================================================
Socket( sock p_socket = -1 );
sock m_sock; // this is the underlying representation
// of the actual socket.
struct sockaddr_in m_localinfo; // structure containing information
// about the local connection
bool m_isblocking; // this tells whether the socket is
// blocking or not.
};
// ========================================================================
// Class: DataSocket
// Purpose: A variation of the BasicSocket base class that handles
// TCP/IP data connections.
// ========================================================================
class DataSocket : public Socket
{
public:
// ====================================================================
// Function: DataSocket
// Purpose: Constructs the data socket with optional values
// ====================================================================
DataSocket( sock p_socket = -1 );
// ====================================================================
// Function: GetRemoteAddress
// Purpose: get the IP address of the remote host.
// ====================================================================
inline ipaddress GetRemoteAddress() const
{
return m_remoteinfo.sin_addr.s_addr;
}
// ====================================================================
// Function: GetRemotePort
// Purpose: gets the remote port of the socket
// ====================================================================
inline port GetRemotePort() const
{
return ntohs( m_remoteinfo.sin_port );
}
// ====================================================================
// Function: IsConnected
// Purpose: Determines if the socket is connected or not.
// ====================================================================
inline bool IsConnected() const
{
return m_connected;
}
// ====================================================================
// Function: Connect
// Purpose: Connects this socket to another socket. This will fail
// if the socket is already connected, or the server
// rejects the connection.
// ====================================================================
void Connect( ipaddress p_addr, port p_port );
// ====================================================================
// Function: Send
// Purpose: Attempts to send data, and returns the number of
// of bytes sent
// ====================================================================
int Send( const char* p_buffer, int p_size );
// ====================================================================
// Function: Receive
// Purpose: Attempts to receive data from a socket, and returns the
// amount of data received.
// ====================================================================
int Receive( char* p_buffer, int p_size );
// ====================================================================
// Function: Close
// Purpose: closes the socket.
// ====================================================================
void Close();
protected:
bool m_connected; // is the socket connected?
struct sockaddr_in m_remoteinfo;// structure containing information
// about the remote connection
};
// ========================================================================
// Class: ListeningSocket
// Purpose: A variation of the BasicSocket base class that handles
// incomming TCP/IP connection requests.
// ========================================================================
class ListeningSocket : public Socket
{
public:
// ====================================================================
// Function: ListeningSocket
// Purpose: Constructor. Constructs the socket with initial values
// ====================================================================
ListeningSocket();
// ====================================================================
// Function: Listen
// Purpose: this function will tell the socket to listen on a
// certain port
// p_port: This is the port that the socket will listen on.
// ====================================================================
void Listen( port p_port );
// ====================================================================
// Function: Accept
// Purpose: This is a blocking function that will accept an
// incomming connection and return a data socket with info
// about the new connection.
// ====================================================================
DataSocket Accept();
// ====================================================================
// Function: IsListening
// Purpose: Determines if the socket is listening or not.
// ====================================================================
inline bool IsListening() const
{
return m_listening;
}
// ====================================================================
// Function: Close
// Purpose: closes the socket.
// ====================================================================
void Close();
protected:
bool m_listening; // is the socket listening?
};
} // end namespace SocketLib
#endif