1 | // Implementation of the Socket class. |
---|
2 | |
---|
3 | |
---|
4 | #include "Socket.h" |
---|
5 | #include "string.h" |
---|
6 | #include <string.h> |
---|
7 | #include <errno.h> |
---|
8 | #include <fcntl.h> |
---|
9 | #include <iostream> |
---|
10 | |
---|
11 | using namespace std; |
---|
12 | |
---|
13 | Socket::Socket() : |
---|
14 | m_sock ( -1 ) |
---|
15 | { |
---|
16 | |
---|
17 | memset ( &m_addr, |
---|
18 | 0, |
---|
19 | sizeof ( m_addr ) ); |
---|
20 | |
---|
21 | } |
---|
22 | |
---|
23 | Socket::~Socket() |
---|
24 | { |
---|
25 | if ( is_valid() ) |
---|
26 | ::close ( m_sock ); |
---|
27 | } |
---|
28 | |
---|
29 | bool Socket::create() |
---|
30 | { |
---|
31 | m_sock = socket ( AF_INET, |
---|
32 | SOCK_STREAM, |
---|
33 | 0 ); |
---|
34 | |
---|
35 | if ( ! is_valid() ) |
---|
36 | return false; |
---|
37 | |
---|
38 | |
---|
39 | // TIME_WAIT - argh |
---|
40 | int on = 1; |
---|
41 | if ( setsockopt ( m_sock, SOL_SOCKET, SO_REUSEADDR, ( const char* ) &on, sizeof ( on ) ) == -1 ) |
---|
42 | return false; |
---|
43 | |
---|
44 | |
---|
45 | return true; |
---|
46 | |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | |
---|
51 | bool Socket::bind ( const int port ) |
---|
52 | { |
---|
53 | |
---|
54 | if ( ! is_valid() ) |
---|
55 | { |
---|
56 | return false; |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | |
---|
61 | m_addr.sin_family = AF_INET; |
---|
62 | m_addr.sin_addr.s_addr = INADDR_ANY; |
---|
63 | m_addr.sin_port = htons ( port ); |
---|
64 | |
---|
65 | int bind_return = ::bind ( m_sock, |
---|
66 | ( struct sockaddr * ) &m_addr, |
---|
67 | sizeof ( m_addr ) ); |
---|
68 | |
---|
69 | |
---|
70 | if ( bind_return == -1 ) |
---|
71 | { |
---|
72 | return false; |
---|
73 | } |
---|
74 | |
---|
75 | return true; |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | bool Socket::listen() const |
---|
80 | { |
---|
81 | if ( ! is_valid() ) |
---|
82 | { |
---|
83 | return false; |
---|
84 | } |
---|
85 | |
---|
86 | int listen_return = ::listen ( m_sock, MAXCONNECTIONS ); |
---|
87 | |
---|
88 | |
---|
89 | if ( listen_return == -1 ) |
---|
90 | { |
---|
91 | return false; |
---|
92 | } |
---|
93 | |
---|
94 | return true; |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | bool Socket::accept ( Socket& new_socket ) const |
---|
99 | { |
---|
100 | int addr_length = sizeof ( m_addr ); |
---|
101 | new_socket.m_sock = ::accept ( m_sock, ( sockaddr * ) &m_addr, ( socklen_t * ) &addr_length ); |
---|
102 | |
---|
103 | if ( new_socket.m_sock <= 0 ) |
---|
104 | return false; |
---|
105 | else |
---|
106 | return true; |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | bool Socket::send ( const std::string s ) const |
---|
111 | { |
---|
112 | int status = ::send ( m_sock, s.c_str(), s.size(), MSG_NOSIGNAL ); |
---|
113 | if ( status == -1 ) |
---|
114 | { |
---|
115 | return false; |
---|
116 | } |
---|
117 | else |
---|
118 | { |
---|
119 | return true; |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | int Socket::recv ( std::string& s ) const |
---|
125 | { |
---|
126 | char buf [ MAXRECV + 1 ]; |
---|
127 | |
---|
128 | s = ""; |
---|
129 | |
---|
130 | memset ( buf, 0, MAXRECV + 1 ); |
---|
131 | |
---|
132 | int status = ::recv ( m_sock, buf, MAXRECV, 0 ); |
---|
133 | |
---|
134 | if ( status == -1 ) |
---|
135 | { |
---|
136 | cout << "status == -1 errno == " << errno << " in Socket::recv\n"; |
---|
137 | return 0; |
---|
138 | } |
---|
139 | else if ( status == 0 ) |
---|
140 | { |
---|
141 | return 0; |
---|
142 | } |
---|
143 | else |
---|
144 | { |
---|
145 | s = buf; |
---|
146 | return status; |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | |
---|
152 | bool Socket::connect ( const std::string host, const int port ) |
---|
153 | { |
---|
154 | if ( ! is_valid() ) return false; |
---|
155 | |
---|
156 | m_addr.sin_family = AF_INET; |
---|
157 | m_addr.sin_port = htons ( port ); |
---|
158 | |
---|
159 | int status = inet_pton ( AF_INET, host.c_str(), &m_addr.sin_addr ); |
---|
160 | |
---|
161 | if ( errno == EAFNOSUPPORT ) return false; |
---|
162 | |
---|
163 | status = ::connect ( m_sock, ( sockaddr * ) &m_addr, sizeof ( m_addr ) ); |
---|
164 | |
---|
165 | if ( status == 0 ) |
---|
166 | return true; |
---|
167 | else |
---|
168 | return false; |
---|
169 | } |
---|
170 | |
---|
171 | void Socket::set_non_blocking ( const bool b ) |
---|
172 | { |
---|
173 | |
---|
174 | int opts; |
---|
175 | |
---|
176 | opts = fcntl ( m_sock, |
---|
177 | F_GETFL ); |
---|
178 | |
---|
179 | if ( opts < 0 ) |
---|
180 | { |
---|
181 | return; |
---|
182 | } |
---|
183 | |
---|
184 | if ( b ) |
---|
185 | opts = ( opts | O_NONBLOCK ); |
---|
186 | else |
---|
187 | opts = ( opts & ~O_NONBLOCK ); |
---|
188 | |
---|
189 | fcntl ( m_sock, |
---|
190 | F_SETFL,opts ); |
---|
191 | |
---|
192 | } |
---|