bitz-server
0.1.6
Main Page
Related Pages
Classes
Files
File List
lib
socket
socket.h
1
/*
2
* C++ sockets on Unix and Windows
3
* Copyright (C) 2002 <unknown>
4
* Copyright (C) 2010 Uditha Atukorala
5
*
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 3 of the License, or
9
* (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
*/
20
21
#ifndef SL_SOCKET_H
22
#define SL_SOCKET_H
23
24
#include <string>
// For string
25
#include <exception>
// For exception class
26
#include <vector>
// For vectors
27
28
#ifdef WIN32
29
#include <winsock.h>
// For socket(), connect(), send(), and recv()
30
typedef
int
socklen_t;
31
typedef
char
raw_type;
// Type used for raw data on this platform
32
#else
33
#include <netinet/in.h>
// For sockaddr_in
34
typedef
void
raw_type;
// Type used for raw data on this platform
35
#endif
36
37
using namespace
std
;
38
39
namespace
socketlibrary
{
40
44
void
fillAddr(
const
string
&address,
unsigned
short
port, sockaddr_in &addr);
45
49
class
SocketException
:
public
exception {
50
public
:
57
SocketException
(
const
string
&message,
bool
inclSysMsg =
false
)
throw
();
58
62
~
SocketException
()
throw
();
63
68
const
char
*what()
const
throw
();
69
70
private
:
71
string
userMessage;
// Exception message
72
};
73
74
78
class
Socket
{
79
public
:
83
~
Socket
();
84
90
string
getLocalAddress()
throw
(
SocketException
);
91
97
unsigned
short
getLocalPort()
throw
(
SocketException
);
98
105
void
setLocalPort(
unsigned
short
localPort)
throw
(
SocketException
);
106
115
void
setLocalAddressAndPort(
const
string
&localAddress,
unsigned
short
localPort = 0)
throw
(
SocketException
);
116
130
static
void
cleanUp()
throw
(
SocketException
);
131
138
static
unsigned
short
resolveService(
const
string
&service,
const
string
&protocol =
"tcp"
);
139
140
private
:
141
// Prevent the user from trying to use value semantics on this object
142
Socket
(
const
Socket
&sock);
143
void
operator=(
const
Socket
&sock);
144
145
protected
:
146
int
sock;
// Socket descriptor
147
Socket
(
int
type,
int
protocol)
throw
(
SocketException
);
148
Socket
(
int
sock);
149
};
150
154
class
CommunicatingSocket
:
public
Socket
{
155
public
:
163
void
connect(
const
string
&foreignAddress,
unsigned
short
foreignPort)
throw
(
SocketException
);
164
172
void
send(
const
void
*buffer,
int
bufferLen)
throw
(
SocketException
);
173
182
int
recv(
void
*buffer,
int
bufferLen)
throw
(
SocketException
);
183
194
int
peek(
void
*buffer,
int
bufferLen)
throw
(
SocketException
);
195
206
int
readLine(
char
*buffer,
int
bufferLen,
const
char
delimiter =
'\n'
)
throw
(
SocketException
);
207
213
string
getForeignAddress()
throw
(
SocketException
);
214
220
unsigned
short
getForeignPort()
throw
(
SocketException
);
221
222
protected
:
223
CommunicatingSocket
(
int
type,
int
protocol)
throw
(
SocketException
);
224
CommunicatingSocket
(
int
newSD);
225
};
226
230
class
TCPSocket
:
public
CommunicatingSocket
{
231
public
:
236
TCPSocket
()
throw
(
SocketException
);
237
245
TCPSocket
(
const
string
&foreignAddress,
unsigned
short
foreignPort)
throw
(
SocketException
);
246
247
private
:
248
friend
class
TCPServerSocket
;
// Access for TCPServerSocket::accept() connection creation
249
friend
class
TCPServerSocketM
;
// Access for TCPServerSocketM::accept() connection creation
250
TCPSocket
(
int
newSD);
251
};
252
256
class
TCPServerSocket
:
public
Socket
{
257
public
:
267
TCPServerSocket
(
unsigned
short
localPort,
int
queueLen = 5)
throw
(
SocketException
);
268
278
TCPServerSocket
(
const
string
&localAddress,
unsigned
short
localPort,
int
queueLen = 5)
throw
(
SocketException
);
279
285
TCPSocket
*accept()
throw
(
SocketException
);
286
287
private
:
288
void
setListen(
int
queueLen)
throw
(
SocketException
);
289
};
290
294
class
TCPServerSocketM
:
public
TCPServerSocket
{
295
public
:
296
306
TCPServerSocketM
(
unsigned
short
localPort,
int
queueLen = 5)
throw
(
SocketException
);
307
313
TCPSocket
*accept()
throw
(
SocketException
);
314
320
bool
pendingConnections()
throw
(
SocketException
);
321
328
int
getWaitingClients(vector<TCPSocket *> &clients)
throw
(
SocketException
);
329
334
void
closeClientConnection(
TCPSocket
*client);
335
336
private
:
337
fd_set fds_master;
// master file descriptor list
338
int
fdmax;
// maximum file descriptor number
339
340
vector<TCPSocket *> v_clients;
341
};
342
346
class
UDPSocket
:
public
CommunicatingSocket
{
347
public
:
352
UDPSocket
()
throw
(
SocketException
);
353
359
UDPSocket
(
unsigned
short
localPort)
throw
(
SocketException
);
360
367
UDPSocket
(
const
string
&localAddress,
unsigned
short
localPort)
throw
(
SocketException
);
368
374
void
disconnect()
throw
(
SocketException
);
375
386
void
sendTo(
const
void
*buffer,
int
bufferLen,
const
string
&foreignAddress,
unsigned
short
foreignPort)
throw
(
SocketException
);
387
398
int
recvFrom(
void
*buffer,
int
bufferLen,
string
&sourceAddress,
unsigned
short
&sourcePort)
throw
(
SocketException
);
399
405
void
setMulticastTTL(
unsigned
char
multicastTTL)
throw
(
SocketException
);
406
412
void
joinGroup(
const
string
&multicastGroup)
throw
(
SocketException
);
413
419
void
leaveGroup(
const
string
&multicastGroup)
throw
(
SocketException
);
420
421
private
:
422
void
setBroadcast();
423
};
424
425
}
// End of namespace SocketLibrary
426
427
#endif
/* !SL_SOCKET_H */
socketlibrary::TCPServerSocket
Definition:
socket.h:256
socketlibrary::TCPSocket
Definition:
socket.h:230
socketlibrary::Socket
Definition:
socket.h:78
socketlibrary::UDPSocket
Definition:
socket.h:346
std
socketlibrary::SocketException
Definition:
socket.h:49
socketlibrary::CommunicatingSocket
Definition:
socket.h:154
socketlibrary::TCPServerSocketM
Definition:
socket.h:294
socketlibrary
Definition:
socket.cpp:37
Generated on Tue Jan 19 2016 23:14:10 for bitz-server by
1.8.9.1