|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * This file contains implemention of Socket, SocketClient, SocketServer and |
|
16 * SocketSelect classes. |
|
17 */ |
|
18 |
|
19 #include <iostream> |
|
20 #include <sstream> |
|
21 #include <errno.h> |
|
22 |
|
23 #include "Socket.h" |
|
24 #include "Util.h" |
|
25 |
|
26 using namespace std; |
|
27 |
|
28 //********************************************************************************** |
|
29 // Class Socket |
|
30 // |
|
31 // This class encapsulates Windows Sockets API Socket functionality |
|
32 //********************************************************************************** |
|
33 |
|
34 int Socket::nofSockets_= 0; |
|
35 |
|
36 void Socket::Start() { |
|
37 if (!nofSockets_) { |
|
38 WSADATA info; |
|
39 if (WSAStartup(MAKEWORD(2,0), &info)) { |
|
40 throw "Could not start WSA"; |
|
41 } |
|
42 } |
|
43 ++nofSockets_; |
|
44 } |
|
45 |
|
46 void Socket::End() { |
|
47 WSACleanup(); |
|
48 } |
|
49 |
|
50 |
|
51 Socket::Socket() : s_(0) { |
|
52 Start(); |
|
53 // UDP: use SOCK_DGRAM instead of SOCK_STREAM |
|
54 s_ = socket(AF_INET,SOCK_STREAM,0); |
|
55 |
|
56 refCounter_ = new int(1); |
|
57 } |
|
58 |
|
59 Socket::Socket(SOCKET s) : s_(s) { |
|
60 Start(); |
|
61 refCounter_ = new int(1); |
|
62 }; |
|
63 |
|
64 |
|
65 Socket::~Socket() { |
|
66 if (! --(*refCounter_)) { |
|
67 Close(); |
|
68 delete refCounter_; |
|
69 } |
|
70 |
|
71 --nofSockets_; |
|
72 if (!nofSockets_) End(); |
|
73 } |
|
74 |
|
75 Socket::Socket(const Socket& o) { |
|
76 refCounter_=o.refCounter_; |
|
77 (*refCounter_)++; |
|
78 s_ =o.s_; |
|
79 |
|
80 nofSockets_++; |
|
81 } |
|
82 |
|
83 Socket& Socket::operator =(Socket& o) { |
|
84 (*o.refCounter_)++; |
|
85 |
|
86 refCounter_=o.refCounter_; |
|
87 s_ =o.s_; |
|
88 |
|
89 nofSockets_++; |
|
90 |
|
91 return *this; |
|
92 } |
|
93 |
|
94 |
|
95 void Socket::Close() { |
|
96 closesocket(s_); |
|
97 } |
|
98 |
|
99 /* |
|
100 * This method is used to receive data bytes socket. The method reads all available and combines them to returned String object |
|
101 */ |
|
102 String Socket::ReceiveBytes() { |
|
103 String ret; |
|
104 char buf[1024]; |
|
105 int rv; |
|
106 while ((rv=recv(s_, buf, 1024,0)) == WSAEMSGSIZE) { |
|
107 String t; |
|
108 t.assign(buf,1024); |
|
109 ret +=t; |
|
110 } |
|
111 |
|
112 String t; |
|
113 t.assign(buf, 0, rv); |
|
114 ret += t; |
|
115 |
|
116 printf("t = %s, rv = %d", t.c_str(), rv); |
|
117 |
|
118 return ret; |
|
119 } |
|
120 |
|
121 /* |
|
122 * This method reads up to maximum amount of bytes from socket to the Data object given as parameter |
|
123 * The maximum amount is given as parameter |
|
124 */ |
|
125 int Socket::ReceiveBytes(BYTE *data, int bytes) { |
|
126 int rv = SOCKET_ERROR; |
|
127 |
|
128 while (rv == SOCKET_ERROR) |
|
129 { |
|
130 rv = recv(s_, (char *)data, bytes, 0); |
|
131 if (rv == 0 || rv == WSAECONNRESET) |
|
132 return -1; |
|
133 else |
|
134 break; |
|
135 } |
|
136 return rv; |
|
137 } |
|
138 |
|
139 |
|
140 /* |
|
141 * This method is used to read characters from socket until line separator is encountered |
|
142 * Returns String including line separator |
|
143 */ |
|
144 String Socket::ReceiveLine() { |
|
145 String ret; |
|
146 while (1) { |
|
147 char r; |
|
148 |
|
149 switch(recv(s_, &r, 1, 0)) { |
|
150 case 0: // not connected anymore; |
|
151 return ""; |
|
152 //SOCKET_ERROR == -1 |
|
153 case -1: |
|
154 if (errno == EAGAIN) { |
|
155 return ret; |
|
156 } else { |
|
157 // not connected anymore |
|
158 return ""; |
|
159 } |
|
160 } |
|
161 |
|
162 ret += r; |
|
163 if (r == '\n') return ret; |
|
164 } |
|
165 } |
|
166 |
|
167 |
|
168 /* |
|
169 *This method is used to send String as a line |
|
170 *The parameter of SendLine is not a const reference |
|
171 *because SendLine modifes the String passed. |
|
172 */ |
|
173 void Socket::SendLine(String s) { |
|
174 s += '\n'; |
|
175 send(s_,s.c_str(),s.length(),0); |
|
176 } |
|
177 |
|
178 /* |
|
179 * This method is used to send String as bytes |
|
180 */ |
|
181 void Socket::SendBytes(const String& s) { |
|
182 send(s_,s.c_str(),s.length(),0); |
|
183 } |
|
184 |
|
185 /* |
|
186 * This method is used to send specific amount of bytes of String |
|
187 * The parameter is a const reference |
|
188 * because the methos does not modify the String passed |
|
189 * (in contrast to SendLine). |
|
190 */ |
|
191 int Socket::SendBytes(const BYTE* data, int bytes) |
|
192 { |
|
193 if (bytes <= 0) return 0; |
|
194 send(s_,(char *)data, bytes, 0); |
|
195 return 0; |
|
196 } |
|
197 |
|
198 //********************************************************************************** |
|
199 // Class SocketServer |
|
200 // |
|
201 // This class is used to: |
|
202 // -listen for incoming connections to a Socket |
|
203 // -connect to socket |
|
204 //********************************************************************************** |
|
205 |
|
206 SocketServer::SocketServer() { |
|
207 } |
|
208 |
|
209 /* |
|
210 * This method binds and opens the given socket to listening mode |
|
211 */ |
|
212 void SocketServer::Accept( Socket*& s,int port, int connections, String& remoteHost ) { |
|
213 |
|
214 sockaddr_in sa; |
|
215 memset(&sa, 0, sizeof(sa)); /* clear our address */ |
|
216 sa.sin_family = PF_INET; |
|
217 sa.sin_port = htons(port); |
|
218 |
|
219 // Use the given socket for listening |
|
220 Socket* listenSocket = s; |
|
221 |
|
222 // bind the socket to the internet address |
|
223 if (bind(listenSocket->s_, (sockaddr *)&sa, sizeof(sockaddr_in)) == |
|
224 SOCKET_ERROR) { |
|
225 throw "INVALID_SOCKET"; |
|
226 } |
|
227 //place a socket in a state in which it is listening for an incoming connection |
|
228 listen(listenSocket->s_, connections); |
|
229 |
|
230 sockaddr_in remoteAddr; |
|
231 int remoteAddrLen = sizeof(sockaddr_in); |
|
232 |
|
233 //extract the first connection on the queue of pending connections on socket |
|
234 //accept blocks if socket is marked blocking |
|
235 SOCKET new_sock = accept(listenSocket->s_, (sockaddr*) &remoteAddr, &remoteAddrLen); |
|
236 if (new_sock == INVALID_SOCKET) |
|
237 { |
|
238 throw "Invalid Socket"; |
|
239 } |
|
240 |
|
241 // Return remote host and port to caller |
|
242 stringstream msg; |
|
243 msg<<inet_ntoa(remoteAddr.sin_addr)<<":"<<ntohs(remoteAddr.sin_port); |
|
244 remoteHost.append( msg.str() ); |
|
245 |
|
246 s = new Socket(new_sock); |
|
247 delete listenSocket; |
|
248 } |
|
249 |
|
250 /* |
|
251 * This method establishes a connection to the socket |
|
252 */ |
|
253 void SocketServer::Connect(Socket* s, const char* remoteHost, int remotePort) { |
|
254 |
|
255 Socket* connectSocket = s; |
|
256 sockaddr_in sa; |
|
257 memset(&sa, 0, sizeof(sa)); /* clear our address */ |
|
258 sa.sin_family = AF_INET; |
|
259 if( isalpha(remoteHost[0]) ) |
|
260 { |
|
261 HOSTENT* Hostent; |
|
262 if( (Hostent = gethostbyname( remoteHost )) != 0 ) |
|
263 sa.sin_addr.s_addr = *(long*) Hostent->h_addr; |
|
264 else |
|
265 throw "Unknown host!"; |
|
266 } |
|
267 else |
|
268 sa.sin_addr.s_addr = inet_addr( remoteHost ); |
|
269 |
|
270 sa.sin_port = htons( remotePort ); |
|
271 |
|
272 // Connect to server. |
|
273 if ( connect( connectSocket->s_, (SOCKADDR*) &sa, sizeof(sa) ) == SOCKET_ERROR) { |
|
274 throw "SOCKET_ERROR"; |
|
275 } |
|
276 } |
|
277 |
|
278 //********************************************************************************** |
|
279 // Class SocketClient |
|
280 // |
|
281 //********************************************************************************** |
|
282 |
|
283 SocketClient::SocketClient(const String& host, int port) : Socket() { |
|
284 String error; |
|
285 |
|
286 hostent *he; |
|
287 if ((he = gethostbyname(host.c_str())) == NULL) { |
|
288 error = strerror(errno); |
|
289 throw error; |
|
290 } |
|
291 |
|
292 sockaddr_in addr; |
|
293 addr.sin_family = AF_INET; |
|
294 addr.sin_port = htons(port); |
|
295 addr.sin_addr = *((in_addr *)he->h_addr); |
|
296 memset(&(addr.sin_zero), 0, 8); |
|
297 |
|
298 if (::connect(s_, (sockaddr *) &addr, sizeof(sockaddr))) { |
|
299 error = strerror(WSAGetLastError()); |
|
300 throw error; |
|
301 } |
|
302 } |
|
303 |
|
304 //********************************************************************************** |
|
305 // Class SocketSelect |
|
306 // |
|
307 //********************************************************************************** |
|
308 |
|
309 SocketSelect::SocketSelect(Socket const * const s1, Socket const * const s2) { |
|
310 FD_ZERO(&fds_); |
|
311 FD_SET(const_cast<Socket*>(s1)->s_,&fds_); |
|
312 FD_SET(const_cast<Socket*>(s2)->s_,&fds_); |
|
313 |
|
314 if (select (0, &fds_, (fd_set*) 0, (fd_set*) 0, (timeval*) 0) |
|
315 == SOCKET_ERROR) throw "Error in select"; |
|
316 } |
|
317 |
|
318 |
|
319 bool SocketSelect::Readable(Socket const * const s) { |
|
320 if (FD_ISSET(s->s_,&fds_)) return true; |
|
321 return false; |
|
322 } |
|
323 |
|
324 // End of the file |
|
325 |