-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtcpsocket.cpp
More file actions
129 lines (106 loc) · 3.56 KB
/
Copy pathtcpsocket.cpp
File metadata and controls
129 lines (106 loc) · 3.56 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
#include <string>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "settings.h"
#include "tcpsocket.h"
#include "socketexception.h"
using namespace std;
TcpSocket::TcpSocket():
sockHandle(-1)
{
memset(&sockAddr, 0, sizeof(sockaddr_in));
}
TcpSocket::~TcpSocket()
{
if (sockHandle >= 0)
::shutdown(sockHandle, SHUT_RDWR);
}
void TcpSocket::create()
{
if (sockHandle >= 0)
throw SocketException("Socket already initialized.\n");
sockHandle = ::socket(AF_INET, SOCK_STREAM, 0);
if (sockHandle < 0)
throw SocketException("Unable to create a TCP socket.\n");
/* Set socket to bind to an existing IP/PORT combination */
int reuseAddrOpt = 1;
if (::setsockopt(sockHandle, SOL_SOCKET, SO_REUSEADDR, (void *) &reuseAddrOpt, sizeof(int) ) < 0)
throw SocketException("Unable to set socket options.\n");
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr=htonl(INADDR_ANY);
sockAddr.sin_port=htons(Settings::instance().getListenerPort());
if (::bind(sockHandle, (sockaddr *)&sockAddr, sizeof(sockaddr)) < 0)
throw SocketException("Unable to bind the socket the the IP:PORT.\n");
/* Starts listening to any connection attempts */
if (::listen(sockHandle, Settings::instance().getMaxAllowedConnections()) < 0)
throw SocketException("Unable to start listening to upcoming connections.\n");
}
void TcpSocket::accept(TcpSocket& newClient)
{
int newSocketHandle;
socklen_t addrLength = sizeof(sockaddr);
newSocketHandle = ::accept(sockHandle, (sockaddr*)&sockAddr, &addrLength);
if (newSocketHandle < 0)
throw SocketException("Error while accepting a new TCP connection.\n");
newClient.setHandle(newSocketHandle);
}
void TcpSocket::connect(string& IP, int port)
{
if (sockHandle >= 0)
throw SocketException("Socket already initialized.\n");
sockHandle = ::socket(AF_INET, SOCK_STREAM, 0);
if (sockHandle < 0)
throw SocketException("Unable to create a TCP socket.\n");
/* Set socket to connect to an existing IP/PORT combination */
int reuseAddrOpt = 1;
if (::setsockopt(sockHandle, SOL_SOCKET, SO_REUSEADDR, (void *) &reuseAddrOpt, sizeof(int) ) < 0)
throw SocketException("Unable to set socket options.\n");
/* setup the address to connect to */
if (inet_pton(AF_INET, IP.c_str(), &sockAddr.sin_addr) != 1)
throw SocketException("Could convert IP address to a sockaddr structure.\n");
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(port);
/* Connect */
if (::connect(sockHandle, (sockaddr*)&sockAddr, sizeof(sockaddr)) < 0)
throw SocketException("Cound not connect to the server socket.\n");
}
void TcpSocket::close()
{
if (sockHandle >= 0)
::shutdown(sockHandle, SHUT_RDWR);
memset(&sockAddr, 0, sizeof(sockaddr_in));
sockHandle = -1;
}
void TcpSocket::setHandle(int newHandle)
{
if (sockHandle < 0)
sockHandle = newHandle;
else
throw SocketException("Attempt to change a socket handle which is already initialized.\n");
}
const TcpSocket& TcpSocket::operator << (const string& message) const
{
if (sockHandle >= 0) {
if (::send(sockHandle, message.c_str(), message.size(), 0) < message.size())
throw SocketException("Error while transmitting message.\n");
} else
throw SocketException("Socket not initialized.\n");
return *this;
}
const TcpSocket& TcpSocket::operator >> (string& message) const
{
if (sockHandle >= 0) {
char buf[1024];
int res;
res = ::recv(sockHandle, buf, 1024, 0);
if (res > 0)
message = buf;
else
throw SocketException("Received 0 bytes on TCP socket.\n");
} else
throw SocketException("Socket not initialized.\n");
return *this;
}