-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocket.cpp
105 lines (93 loc) · 1.86 KB
/
Socket.cpp
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
#include <iostream>
#include "include/Socket.hpp"
Socket::Socket(void)
: _sd(-1), _port(0), _len(sizeof(_sin)), _proto(0)
{
}
Socket::Socket(Socket const& other)
: _sd(-1), _port(0), _len(sizeof(_sin)), _proto(0)
{
*this = other;
}
Socket& Socket::operator=(Socket const& other)
{
if (this == &other)
return *this;
return *this;
}
Socket::~Socket(void)
{
if (_sd > 2)
close(_sd);
}
void Socket::setSd(int sd)
{
_sd = sd;
}
int Socket::sd(void) const
{
return _sd;
}
unsigned int& Socket::port(void)
{
return _port;
}
unsigned int& Socket::len(void)
{
return _len;
}
sockaddr_in& Socket::sin(void)
{
return _sin;
}
protoent* Socket::proto(void)
{
return _proto;
}
int Socket::makeSocket(unsigned int port)
{
int on;
_proto = getprotobyname("tcp");
if (!_proto)
throw protoException();
_sin.sin_family = AF_INET;
_sin.sin_port = htons(port);
_sin.sin_addr.s_addr = htonl(INADDR_ANY);
_sd = socket(AF_INET, SOCK_STREAM, _proto->p_proto);
if (_sd == -1)
throw socketException();
on = 1;
if (setsockopt(_sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
throw socketException();
return _sd;
}
int Socket::makeSocket(unsigned int port, unsigned long addr)
{
int on;
_proto = getprotobyname("tcp");
if (!_proto)
throw protoException();
_sin.sin_family = AF_INET;
_sin.sin_port = htons(port);
_sin.sin_addr.s_addr = htonl(addr);
_sd = socket(AF_INET, SOCK_STREAM, _proto->p_proto);
if (_sd == -1)
throw socketException();
on = 1;
if (setsockopt(_sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
throw socketException();
return _sd;
}
void Socket::makeNonBlocking(void)
{
if (fcntl(_sd, F_SETFL, O_NONBLOCK) == -1)
throw socketException();
}
const char* Socket::protoException::what(void) const throw()
{
return "Proto Error\n";
}
const char* Socket::socketException::what(void) const throw()
{
return "Socket Error\n";
}