1 | #ifdef OSF1
|
---|
2 | #define _XOPEN_SOURCE 500
|
---|
3 | #endif
|
---|
4 | #include "swrapsock.h"
|
---|
5 | #include "sopnamsp.h"
|
---|
6 |
|
---|
7 | #include <time.h>
|
---|
8 |
|
---|
9 | #include <arpa/inet.h>
|
---|
10 | #include <netdb.h>
|
---|
11 | #include <sys/errno.h>
|
---|
12 | #include <unistd.h>
|
---|
13 |
|
---|
14 | #include <iostream>
|
---|
15 | #include <stdlib.h>
|
---|
16 | #include <string.h>
|
---|
17 |
|
---|
18 | /*!
|
---|
19 | \class SOPHYA::Socket
|
---|
20 | \ingroup SysTools
|
---|
21 | \brief Base class for server and client sockets.
|
---|
22 |
|
---|
23 | The class Socket is not intended for direct use. Only the derived classes
|
---|
24 | ServerSocket and ClientSocket should be used.
|
---|
25 | The Close() method should be called explicitly.
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* --Methode-- */
|
---|
29 | Socket::Socket(int s)
|
---|
30 | {
|
---|
31 | skt = s;
|
---|
32 | tstart = tlast = (long)time(NULL);
|
---|
33 | totsnd = totrcv = 0;
|
---|
34 | dts = dtr = 0;
|
---|
35 | nbbs = nbbr = 0;
|
---|
36 | lstnbb = 0; lstdt = 0;
|
---|
37 | errcnt = 0;
|
---|
38 | }
|
---|
39 |
|
---|
40 | /* --Methode-- */
|
---|
41 | Socket::Socket(Socket const & a)
|
---|
42 | {
|
---|
43 | Set(a);
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | /* --Methode-- */
|
---|
48 | Socket::~Socket()
|
---|
49 | {
|
---|
50 | // Close();
|
---|
51 | }
|
---|
52 |
|
---|
53 | /* --Methode-- */
|
---|
54 | void Socket::Set(Socket const & a)
|
---|
55 | {
|
---|
56 | skt = a.skt;
|
---|
57 | tstart = a.tstart; tlast = a.tlast;
|
---|
58 | totsnd = a.totsnd; totrcv = a.totrcv;
|
---|
59 | dts = a.dts; dtr = a.dtr;
|
---|
60 | nbbs = a.nbbs; nbbr = a.nbbr;
|
---|
61 | lstnbb = a.lstnbb; lstdt = a.lstdt;
|
---|
62 | errcnt = a.errcnt;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | /* --Methode-- */
|
---|
67 | size_t Socket::Send(const char * buff, size_t len, int flag)
|
---|
68 | {
|
---|
69 | ssize_t rc;
|
---|
70 | rc = send(skt, buff, len, flag);
|
---|
71 | if (rc > 0) {
|
---|
72 | // tlast = (long)time(NULL);
|
---|
73 | totsnd += rc;
|
---|
74 | return(rc);
|
---|
75 | }
|
---|
76 | else return 0;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /* --Methode-- */
|
---|
80 | size_t Socket::Receive(char * buff, size_t len, int flag)
|
---|
81 | {
|
---|
82 | ssize_t rc;
|
---|
83 | // int fromlen = 0;
|
---|
84 | bool encore = true;
|
---|
85 | while (encore) {
|
---|
86 | rc = recv(skt, buff, len, flag);
|
---|
87 | // rc = recvfrom(skt, buff, len, flag, NULL, &fromlen);
|
---|
88 | if ((rc < 1) && (errno == EAGAIN)) usleep(20000);
|
---|
89 | else encore = false;
|
---|
90 | }
|
---|
91 | if (rc > 0) {
|
---|
92 | // tlast = (long)time(NULL);
|
---|
93 | totrcv += rc;
|
---|
94 | return(rc);
|
---|
95 | }
|
---|
96 | else return 0;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /* --Methode-- */
|
---|
100 | size_t Socket::SendAll(const char * buff, size_t len)
|
---|
101 | {
|
---|
102 | size_t nst = 0;
|
---|
103 | int ntry = 0;
|
---|
104 | while (nst < len) {
|
---|
105 | size_t ns = Send(buff+nst, len-nst);
|
---|
106 | ntry++;
|
---|
107 | if (ns < 1) break;
|
---|
108 | nst += ns;
|
---|
109 | }
|
---|
110 | if (nst < len) {
|
---|
111 | cout << " Socket::SendAll() / ERROR ! ntry=" << ntry
|
---|
112 | << " nst=" << nst << " len=" << len << endl;
|
---|
113 | throw SocketException("Socket::SendAll() Error NBytesSent < len ");
|
---|
114 | }
|
---|
115 | return nst;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /* --Methode-- */
|
---|
119 | size_t Socket::ReceiveAll(char * buff, size_t len)
|
---|
120 | {
|
---|
121 | size_t nrt = 0;
|
---|
122 | int ntry = 0;
|
---|
123 | while (nrt < len) {
|
---|
124 | size_t nr = Receive(buff+nrt, len-nrt);
|
---|
125 | ntry++;
|
---|
126 | if (nr < 1) break;
|
---|
127 | nrt += nr;
|
---|
128 | }
|
---|
129 | if (nrt < len) {
|
---|
130 | cout << " Socket::ReceiveAll / ERROR ! ntry=" << ntry
|
---|
131 | << " nrt=" << nrt << " len=" << len << endl;
|
---|
132 | throw SocketException("Socket::ReceiveAll() Error NBytesRecv < len ");
|
---|
133 | }
|
---|
134 | return nrt;
|
---|
135 | }
|
---|
136 |
|
---|
137 | // extern int errno;
|
---|
138 |
|
---|
139 | /* --Methode-- */
|
---|
140 | int Socket::Close()
|
---|
141 | {
|
---|
142 | if (skt<0) return 0;
|
---|
143 | int rc=-1;
|
---|
144 | rc = shutdown(skt, SHUT_RDWR);
|
---|
145 | if(rc < 0)
|
---|
146 | cout << "Socket::Close() Erreur: Pb shutdown() ErrNo="
|
---|
147 | << errno << endl;
|
---|
148 |
|
---|
149 | rc = close(skt);
|
---|
150 | if(rc < 0)
|
---|
151 | cout << "Socket::Close() Erreur: Pb close() ErrNo="
|
---|
152 | << errno << endl;
|
---|
153 | skt = -1;
|
---|
154 | return(0);
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | /*!
|
---|
159 | \class SOPHYA::ServerSocket
|
---|
160 | \ingroup SysTools
|
---|
161 | \brief Socket wrapper class for the server side
|
---|
162 | */
|
---|
163 |
|
---|
164 | /* --Methode-- */
|
---|
165 | ServerSocket::ServerSocket(int port, int nconmax)
|
---|
166 | : Socket()
|
---|
167 | {
|
---|
168 | Initialize(NULL, port, nconmax);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /* --Methode-- */
|
---|
172 | ServerSocket::ServerSocket(const char* inadr, int port, int nconmax)
|
---|
173 | : Socket()
|
---|
174 | {
|
---|
175 | Initialize(inadr, port, nconmax);
|
---|
176 | }
|
---|
177 |
|
---|
178 | /* --Methode-- */
|
---|
179 | ServerSocket::ServerSocket(string const& inadr, int port, int nconmax)
|
---|
180 | : Socket()
|
---|
181 | {
|
---|
182 | Initialize(inadr.c_str(), port, nconmax);
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | /* --Methode-- */
|
---|
187 | void ServerSocket::Initialize(const char* str_inadr, int port, int nconmax)
|
---|
188 | {
|
---|
189 | int s,rc;
|
---|
190 | s = socket(AF_INET, SOCK_STREAM, 0);
|
---|
191 | if (s < 0) {
|
---|
192 | cout << "ServerSocket::Initialize() Pb socketErrNo=" << errno << endl;
|
---|
193 | throw SocketException("ServerSocket::Initialize() Pb socket()");
|
---|
194 | }
|
---|
195 | skt = s;
|
---|
196 | if (nconmax <= 1) nconmax = 1;
|
---|
197 | NConMax = nconmax;
|
---|
198 | nclitot = 0;
|
---|
199 |
|
---|
200 | portid = port;
|
---|
201 | ipskt.sin_family = AF_INET;
|
---|
202 | ipskt.sin_port = htons(port);
|
---|
203 | if (str_inadr==NULL) { // pas d'adresse IP specifie
|
---|
204 | ipskt.sin_addr.s_addr = INADDR_ANY;
|
---|
205 | }
|
---|
206 | else { // adresse IP specifie sous la forme uuu.xxx.yyy.zzz.
|
---|
207 | ipskt.sin_addr.s_addr = inet_addr(str_inadr);
|
---|
208 | }
|
---|
209 | /* Je fais un cast explicit de sockaddr_in * (Internet) en sockaddr * */
|
---|
210 | rc = bind(skt, (struct sockaddr *)(&ipskt), sizeof(ipskt));
|
---|
211 | if (rc < 0) {
|
---|
212 | Close();
|
---|
213 | cout << "ServerSocket::Initialize() Pb bind() ErrNo=" << errno << endl;
|
---|
214 | throw SocketException("ServerSocket::Initialize() Pb bind()");
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | /* --Methode-- */
|
---|
219 | Socket ServerSocket::WaitClientConnection()
|
---|
220 | {
|
---|
221 | int rc,s,len;
|
---|
222 | struct sockaddr_in cli;
|
---|
223 |
|
---|
224 | rc = listen(skt, NConMax);
|
---|
225 | if (rc < 0) {
|
---|
226 | cout << "ServerSocket::WaitClientConnection() Pb listen() ErrNo="
|
---|
227 | << errno << endl;
|
---|
228 | throw SocketException("ServerSocket::WaitClientConnection() Pb listen()");
|
---|
229 | }
|
---|
230 |
|
---|
231 | /* Je fais un cast explicit de sockaddr_in * (Internet) en sockaddr * */
|
---|
232 | socklen_t sl = sizeof(cli);
|
---|
233 | s = accept(skt, (struct sockaddr *)(&cli), &sl);
|
---|
234 | if(s < 0) {
|
---|
235 | cout << "ServerSocket::WaitClientConnection() Pb accpet() ErrNo="
|
---|
236 | << errno << endl;
|
---|
237 | throw SocketException("ServerSocket::WaitClientConnection() Pb accept()");
|
---|
238 | }
|
---|
239 | nclitot++;
|
---|
240 |
|
---|
241 | return Socket(s);
|
---|
242 | }
|
---|
243 |
|
---|
244 |
|
---|
245 | /* --Methode-- */
|
---|
246 | int ServerSocket::Close()
|
---|
247 | {
|
---|
248 | if (skt<0) return 0;
|
---|
249 | int rc=-1;
|
---|
250 | /* il ne faut apparemment pas faire shutdown sur un socket serveur (non connected - ENOTCONN )
|
---|
251 | rc = shutdown(skt, SHUT_RDWR);
|
---|
252 | if(rc < 0)
|
---|
253 | cout << "Socket::Close() Erreur: Pb shutdown() ErrNo="
|
---|
254 | << errno << endl;
|
---|
255 | */
|
---|
256 | rc = close(skt);
|
---|
257 | if(rc < 0)
|
---|
258 | cout << "ServerSocket::Close() Erreur: Pb close() ErrNo="
|
---|
259 | << errno << endl;
|
---|
260 | skt = -1;
|
---|
261 | return(0);
|
---|
262 | }
|
---|
263 |
|
---|
264 |
|
---|
265 | /*!
|
---|
266 | \class SOPHYA::ClientSocket
|
---|
267 | \ingroup SysTools
|
---|
268 | \brief Socket wrapper class for the client side
|
---|
269 | */
|
---|
270 |
|
---|
271 | /* --Methode-- */
|
---|
272 | ClientSocket::ClientSocket(string const& srvname, int port)
|
---|
273 | : Socket()
|
---|
274 | {
|
---|
275 | InitConnection(srvname.c_str(), port);
|
---|
276 | }
|
---|
277 |
|
---|
278 | /* --Methode-- */
|
---|
279 | ClientSocket::ClientSocket(const char* srvname, int port)
|
---|
280 | : Socket()
|
---|
281 | {
|
---|
282 | InitConnection(srvname, port);
|
---|
283 | }
|
---|
284 |
|
---|
285 | /* --Methode-- */
|
---|
286 | ClientSocket::ClientSocket(ClientSocket const& a)
|
---|
287 | : Socket(a)
|
---|
288 | {
|
---|
289 | portid=a.portid;
|
---|
290 | ipskt=a.ipskt;
|
---|
291 | }
|
---|
292 |
|
---|
293 |
|
---|
294 | /* --Methode-- */
|
---|
295 | void ClientSocket::SetC(ClientSocket const& a)
|
---|
296 | {
|
---|
297 | Set(a);
|
---|
298 | portid=a.portid;
|
---|
299 | ipskt=a.ipskt;
|
---|
300 | return;
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | /* --Methode-- */
|
---|
305 | void ClientSocket::InitConnection(const char* name, int port)
|
---|
306 | {
|
---|
307 | struct hostent *server;
|
---|
308 | int s,rc;
|
---|
309 | #if defined(Linux) || defined(linux)
|
---|
310 | typedef unsigned long in_addr_t;
|
---|
311 | #endif
|
---|
312 | in_addr_t inad;
|
---|
313 |
|
---|
314 | server = NULL;
|
---|
315 | /* Try to find out if this is a known machine name */
|
---|
316 | server = gethostbyname(name);
|
---|
317 | if(!server) {
|
---|
318 | /* Try it as an internet address a.b.c.d */
|
---|
319 | /* inad = inet_addr(name);
|
---|
320 | if(inad == INADDR_NONE) {
|
---|
321 | cout << "ClientSocket() Host identification failed inet_addr("
|
---|
322 | << name << ")" << endl;
|
---|
323 | throw SocketException("ClientSocket() Host identification failed ");
|
---|
324 | }
|
---|
325 | */
|
---|
326 | server = gethostbyaddr(name, strlen(name), AF_INET);
|
---|
327 | }
|
---|
328 | if(!server) {
|
---|
329 | cout << "ClientSocket() Host identification failed (2) for"
|
---|
330 | << name << endl;
|
---|
331 | throw SocketException("ClientSocket() Host identification failed (2)");
|
---|
332 | }
|
---|
333 |
|
---|
334 | ipskt.sin_family = AF_INET;
|
---|
335 | ipskt.sin_port = htons(port);
|
---|
336 | memcpy(&(ipskt.sin_addr), server->h_addr, server->h_length);
|
---|
337 | s = socket(AF_INET, SOCK_STREAM, 0);
|
---|
338 | if (s < 0) {
|
---|
339 | cout << "ClientSocket() Erreur: Pb socket() ErrNo=" << errno << endl;
|
---|
340 | throw SocketException("ClientSocket() Erreur: Pb socket()");
|
---|
341 | }
|
---|
342 |
|
---|
343 |
|
---|
344 | skt = s;
|
---|
345 | portid = port;
|
---|
346 | /* Je fais un cast explicit de sockaddr_in * (Internet) en sockaddr * */
|
---|
347 | rc = connect(skt, (struct sockaddr *)(&ipskt), sizeof(struct sockaddr_in));
|
---|
348 | if (rc < 0) {
|
---|
349 | cout << "ClientSocket() Erreur: Pb connect() ErrNo=" << errno << endl;
|
---|
350 | Close();
|
---|
351 | throw SocketException("ClientSocket() Erreur: Pb connect()");
|
---|
352 | }
|
---|
353 | }
|
---|