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 #include <iostream> |
|
19 #include <sstream> |
|
20 #include <errno.h> |
|
21 |
|
22 #include "Socket.h" |
|
23 #include "util.h" |
|
24 |
|
25 using namespace std; |
|
26 |
|
27 int Socket::nofSockets_= 0; |
|
28 |
|
29 void Socket::Start() { |
|
30 if (!nofSockets_) { |
|
31 WSADATA info; |
|
32 if (WSAStartup(MAKEWORD(2,0), &info)) { |
|
33 throw "Could not start WSA"; |
|
34 } |
|
35 } |
|
36 ++nofSockets_; |
|
37 } |
|
38 |
|
39 void Socket::End() { |
|
40 WSACleanup(); |
|
41 } |
|
42 |
|
43 |
|
44 Socket::Socket() : s_(0) { |
|
45 Start(); |
|
46 // UDP: use SOCK_DGRAM instead of SOCK_STREAM |
|
47 s_ = socket(AF_INET,SOCK_STREAM,0); |
|
48 |
|
49 refCounter_ = new int(1); |
|
50 } |
|
51 |
|
52 Socket::Socket(SOCKET s) : s_(s) { |
|
53 Start(); |
|
54 refCounter_ = new int(1); |
|
55 }; |
|
56 |
|
57 |
|
58 Socket::~Socket() { |
|
59 if (! --(*refCounter_)) { |
|
60 Close(); |
|
61 delete refCounter_; |
|
62 } |
|
63 |
|
64 --nofSockets_; |
|
65 if (!nofSockets_) End(); |
|
66 } |
|
67 |
|
68 Socket::Socket(const Socket& o) { |
|
69 refCounter_=o.refCounter_; |
|
70 (*refCounter_)++; |
|
71 s_ =o.s_; |
|
72 |
|
73 nofSockets_++; |
|
74 } |
|
75 |
|
76 Socket& Socket::operator =(Socket& o) { |
|
77 (*o.refCounter_)++; |
|
78 |
|
79 refCounter_=o.refCounter_; |
|
80 s_ =o.s_; |
|
81 |
|
82 nofSockets_++; |
|
83 |
|
84 return *this; |
|
85 } |
|
86 |
|
87 |
|
88 void Socket::Close() { |
|
89 closesocket(s_); |
|
90 } |
|
91 |
|
92 |
|
93 String Socket::ReceiveBytes() { |
|
94 String ret; |
|
95 char buf[1024]; |
|
96 int rv; |
|
97 |
|
98 while ((rv=recv(s_, buf, 1024,0)) == WSAEMSGSIZE) { |
|
99 |
|
100 String t; |
|
101 t.assign(buf,1024); |
|
102 ret +=t; |
|
103 } |
|
104 String t; |
|
105 t.assign(buf, 0, rv); |
|
106 ret += t; |
|
107 |
|
108 printf("t = %s, rv = %d", t.c_str(), rv); |
|
109 |
|
110 return ret; |
|
111 } |
|
112 |
|
113 int Socket::ReceiveBytes(BYTE *data, int bytes) |
|
114 { |
|
115 int rv = SOCKET_ERROR; |
|
116 while (rv == SOCKET_ERROR) |
|
117 { |
|
118 rv = recv(s_, (char *)data, bytes, 0); |
|
119 if (rv == 0 || rv == WSAECONNRESET) |
|
120 return -1; |
|
121 else |
|
122 break; |
|
123 } |
|
124 return rv; |
|
125 } |
|
126 |
|
127 String Socket::ReceiveLine() { |
|
128 String ret; |
|
129 while (1) { |
|
130 char r; |
|
131 |
|
132 switch(recv(s_, &r, 1, 0)) { |
|
133 case 0: // not connected anymore; |
|
134 return ""; |
|
135 case -1: |
|
136 if (errno == EAGAIN) { |
|
137 return ret; |
|
138 } else { |
|
139 // not connected anymore |
|
140 return ""; |
|
141 } |
|
142 } |
|
143 |
|
144 ret += r; |
|
145 if (r == '\n') return ret; |
|
146 } |
|
147 } |
|
148 |
|
149 void Socket::SendLine(String s) { |
|
150 s += '\n'; |
|
151 send(s_,s.c_str(),s.length(),0); |
|
152 } |
|
153 |
|
154 void Socket::SendBytes(const String& s) { |
|
155 send(s_,s.c_str(),s.length(),0); |
|
156 } |
|
157 |
|
158 int Socket::SendBytes(const BYTE* data, int bytes) |
|
159 { |
|
160 if (bytes <= 0) return 0; |
|
161 send(s_,(char *)data, bytes, 0); |
|
162 return 0; |
|
163 } |
|
164 |
|
165 SocketServer::SocketServer() { |
|
166 /* |
|
167 // create a socket |
|
168 s_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
|
169 if (s_ == INVALID_SOCKET) { |
|
170 throw "INVALID_SOCKET"; |
|
171 } |
|
172 */ |
|
173 } |
|
174 |
|
175 |
|
176 void SocketServer::Accept( Socket*& s,int port, int connections, String& remoteHost ) { |
|
177 |
|
178 sockaddr_in sa; |
|
179 memset(&sa, 0, sizeof(sa)); /* clear our address */ |
|
180 sa.sin_family = PF_INET; |
|
181 sa.sin_port = htons(port); |
|
182 |
|
183 // Use the given socket for listening |
|
184 Socket* listenSocket = s; |
|
185 |
|
186 // bind the socket to the internet address |
|
187 if (bind(listenSocket->s_, (sockaddr *)&sa, sizeof(sockaddr_in)) == |
|
188 SOCKET_ERROR) { |
|
189 throw "INVALID_SOCKET"; |
|
190 } |
|
191 |
|
192 listen(listenSocket->s_, connections); |
|
193 |
|
194 sockaddr_in remoteAddr; |
|
195 int remoteAddrLen = sizeof(sockaddr_in); |
|
196 SOCKET new_sock = accept(listenSocket->s_, (sockaddr*) &remoteAddr, &remoteAddrLen); |
|
197 if (new_sock == INVALID_SOCKET) |
|
198 { |
|
199 throw "Invalid Socket"; |
|
200 } |
|
201 // Return remote host and port to caller |
|
202 stringstream msg; |
|
203 msg<<inet_ntoa(remoteAddr.sin_addr)<<":"<<ntohs(remoteAddr.sin_port); |
|
204 remoteHost.append( msg.str() ); |
|
205 |
|
206 |
|
207 s = new Socket(new_sock); |
|
208 delete listenSocket; |
|
209 } |
|
210 |
|
211 void SocketServer::Connect(Socket* s, const char* remoteHost, int remotePort) { |
|
212 |
|
213 Socket* connectSocket = s; |
|
214 sockaddr_in sa; |
|
215 memset(&sa, 0, sizeof(sa)); /* clear our address */ |
|
216 sa.sin_family = AF_INET; |
|
217 if( isalpha(remoteHost[0]) ) |
|
218 { |
|
219 HOSTENT* Hostent; |
|
220 if( (Hostent = gethostbyname( remoteHost )) != 0 ) |
|
221 sa.sin_addr.s_addr = *(long*) Hostent->h_addr; |
|
222 else |
|
223 throw "Unknown host!"; |
|
224 } |
|
225 else |
|
226 sa.sin_addr.s_addr = inet_addr( remoteHost ); |
|
227 |
|
228 sa.sin_port = htons( remotePort ); |
|
229 |
|
230 // Connect to server. |
|
231 if ( connect( connectSocket->s_, (SOCKADDR*) &sa, sizeof(sa) ) == SOCKET_ERROR) { |
|
232 throw "SOCKET_ERROR"; |
|
233 } |
|
234 } |
|
235 |
|
236 |
|
237 SocketClient::SocketClient(const String& host, int port) : Socket() { |
|
238 String error; |
|
239 |
|
240 hostent *he; |
|
241 if ((he = gethostbyname(host.c_str())) == NULL) { |
|
242 error = strerror(errno); |
|
243 throw error; |
|
244 } |
|
245 |
|
246 sockaddr_in addr; |
|
247 addr.sin_family = AF_INET; |
|
248 addr.sin_port = htons(port); |
|
249 addr.sin_addr = *((in_addr *)he->h_addr); |
|
250 memset(&(addr.sin_zero), 0, 8); |
|
251 |
|
252 if (::connect(s_, (sockaddr *) &addr, sizeof(sockaddr))) { |
|
253 error = strerror(WSAGetLastError()); |
|
254 throw error; |
|
255 } |
|
256 } |
|
257 |
|
258 |
|
259 SocketSelect::SocketSelect(Socket const * const s1, Socket const * const s2) { |
|
260 FD_ZERO(&fds_); |
|
261 FD_SET(const_cast<Socket*>(s1)->s_,&fds_); |
|
262 FD_SET(const_cast<Socket*>(s2)->s_,&fds_); |
|
263 |
|
264 if (select (0, &fds_, (fd_set*) 0, (fd_set*) 0, (timeval*) 0) |
|
265 == SOCKET_ERROR) throw "Error in select"; |
|
266 } |
|
267 |
|
268 |
|
269 bool SocketSelect::Readable(Socket const * const s) { |
|
270 if (FD_ISSET(s->s_,&fds_)) return true; |
|
271 return false; |
|
272 } |
|
273 |
|
274 // End of the file |
|
275 |
|