| 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 |  | 
|---|
| 16 | /*! | 
|---|
| 17 | \class SOPHYA::Socket | 
|---|
| 18 | \ingroup SysTools | 
|---|
| 19 | \brief Base class for server and client sockets. | 
|---|
| 20 |  | 
|---|
| 21 | The class Socket is not intended for direct use. Only the derived classes | 
|---|
| 22 | ServerSocket and ClientSocket should be used. | 
|---|
| 23 | */ | 
|---|
| 24 |  | 
|---|
| 25 | /* --Methode-- */ | 
|---|
| 26 | Socket::Socket(int s) | 
|---|
| 27 | { | 
|---|
| 28 | skt = s; | 
|---|
| 29 | tstart = tlast = (long)time(NULL); | 
|---|
| 30 | totsnd = totrcv = 0; | 
|---|
| 31 | dts = dtr = 0; | 
|---|
| 32 | nbbs = nbbr = 0; | 
|---|
| 33 | lstnbb = 0; lstdt = 0; | 
|---|
| 34 | errcnt = 0; | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | /* --Methode-- */ | 
|---|
| 38 | /*  ---  A faire plus tard ... | 
|---|
| 39 | Socket::Socket(Socket const & a) | 
|---|
| 40 | { | 
|---|
| 41 | skt = a.skt; | 
|---|
| 42 | tstart = a.tstart; tlast = a.tlast; | 
|---|
| 43 | totsnd = a.totsnd; totrcv = a.totrcv; | 
|---|
| 44 | dts = a.dts; dtr = a.dtr; | 
|---|
| 45 | nbbs = a.nbbs; nbbr = a.nbbr; | 
|---|
| 46 | lstnbb = a.lstnbb;   lstdt = a.lstdt; | 
|---|
| 47 | errcnt = a.errcnt; | 
|---|
| 48 | } | 
|---|
| 49 | */ | 
|---|
| 50 |  | 
|---|
| 51 | /* --Methode-- */ | 
|---|
| 52 | Socket::~Socket() | 
|---|
| 53 | { | 
|---|
| 54 | Close(); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | /* --Methode-- */ | 
|---|
| 58 | size_t Socket::Send(const char * buff, size_t len, int flag) | 
|---|
| 59 | { | 
|---|
| 60 | ssize_t rc; | 
|---|
| 61 | rc = send(skt, buff, len, flag); | 
|---|
| 62 | if (rc > 0)  { | 
|---|
| 63 | // tlast = (long)time(NULL); | 
|---|
| 64 | totsnd += rc; | 
|---|
| 65 | return(rc); | 
|---|
| 66 | } | 
|---|
| 67 | else return 0; | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | /* --Methode-- */ | 
|---|
| 71 | size_t Socket::Receive(char * buff, size_t len, int flag) | 
|---|
| 72 | { | 
|---|
| 73 | ssize_t rc; | 
|---|
| 74 | //  int fromlen = 0; | 
|---|
| 75 | bool encore = true; | 
|---|
| 76 | while (encore) { | 
|---|
| 77 | rc = recv(skt, buff, len, flag); | 
|---|
| 78 | //    rc = recvfrom(skt, buff, len, flag, NULL, &fromlen); | 
|---|
| 79 | if ((rc < 1) && (errno == EAGAIN))  usleep(20000); | 
|---|
| 80 | else encore = false; | 
|---|
| 81 | } | 
|---|
| 82 | if (rc > 0)  { | 
|---|
| 83 | // tlast = (long)time(NULL); | 
|---|
| 84 | totrcv += rc; | 
|---|
| 85 | return(rc); | 
|---|
| 86 | } | 
|---|
| 87 | else return 0; | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | // extern int errno; | 
|---|
| 91 |  | 
|---|
| 92 | /* --Methode-- */ | 
|---|
| 93 | int Socket::Close() | 
|---|
| 94 | { | 
|---|
| 95 | if (skt<0) return 0; | 
|---|
| 96 | int rc=-1; | 
|---|
| 97 | rc = shutdown(skt, SHUT_RDWR); | 
|---|
| 98 | if(rc < 0) | 
|---|
| 99 | cout << "Socket::Close() Erreur: Pb shutdown() ErrNo=" | 
|---|
| 100 | << errno << endl; | 
|---|
| 101 |  | 
|---|
| 102 | rc = close(skt); | 
|---|
| 103 | if(rc < 0) | 
|---|
| 104 | cout << "Socket::Close() Erreur: Pb close() ErrNo=" | 
|---|
| 105 | << errno << endl; | 
|---|
| 106 | skt = -1; | 
|---|
| 107 | return(0); | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 |  | 
|---|
| 111 | /*! | 
|---|
| 112 | \class SOPHYA::ServerSocket | 
|---|
| 113 | \ingroup SysTools | 
|---|
| 114 | \brief Socket wrapper class for the server side | 
|---|
| 115 | */ | 
|---|
| 116 |  | 
|---|
| 117 | /* --Methode-- */ | 
|---|
| 118 | ServerSocket::ServerSocket(int port, int nconmax) | 
|---|
| 119 | : Socket() | 
|---|
| 120 | { | 
|---|
| 121 | int s,rc; | 
|---|
| 122 | s = socket(AF_INET, SOCK_STREAM, 0); | 
|---|
| 123 | if (s < 0) { | 
|---|
| 124 | cout << "ServerSocket() Pb socket() ErrNo=" <<  errno << endl; | 
|---|
| 125 | throw SocketException("ServerSocket() Pb socket()"); | 
|---|
| 126 | } | 
|---|
| 127 | skt = s; | 
|---|
| 128 | if (nconmax <= 1)  nconmax = 1; | 
|---|
| 129 | NConMax = nconmax; | 
|---|
| 130 | nclitot = 0; | 
|---|
| 131 |  | 
|---|
| 132 | portid = port; | 
|---|
| 133 | ipskt.sin_family = AF_INET; | 
|---|
| 134 | ipskt.sin_port = htons(port); | 
|---|
| 135 | ipskt.sin_addr.s_addr = INADDR_ANY; | 
|---|
| 136 | /* Je fais un cast explicit de sockaddr_in * (Internet) en sockaddr *   */ | 
|---|
| 137 | rc = bind(skt, (struct sockaddr *)(&ipskt), sizeof(ipskt)); | 
|---|
| 138 | if (rc < 0) { | 
|---|
| 139 | Close(); | 
|---|
| 140 | cout << "ServerSocket() Pb bind() ErrNo=" <<  errno << endl; | 
|---|
| 141 | throw SocketException("ServerSocket() Pb bind()"); | 
|---|
| 142 | } | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | /* --Methode-- */ | 
|---|
| 146 | Socket ServerSocket::WaitClientConnection() | 
|---|
| 147 | { | 
|---|
| 148 | int rc,s,len; | 
|---|
| 149 | struct sockaddr_in cli; | 
|---|
| 150 |  | 
|---|
| 151 | rc = listen(skt, NConMax); | 
|---|
| 152 | if (rc < 0) { | 
|---|
| 153 | cout << "ServerSocket::WaitClientConnection() Pb listen() ErrNo=" | 
|---|
| 154 | << errno << endl; | 
|---|
| 155 | throw SocketException("ServerSocket::WaitClientConnection() Pb listen()"); | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | /* Je fais un cast explicit de sockaddr_in * (Internet) en sockaddr *   */ | 
|---|
| 159 | socklen_t sl = sizeof(cli); | 
|---|
| 160 | s = accept(skt, (struct sockaddr *)(&cli), &sl); | 
|---|
| 161 | if(s < 0) { | 
|---|
| 162 | cout << "ServerSocket::WaitClientConnection() Pb accpet() ErrNo=" | 
|---|
| 163 | << errno << endl; | 
|---|
| 164 | throw SocketException("ServerSocket::WaitClientConnection() Pb accept()"); | 
|---|
| 165 | } | 
|---|
| 166 | nclitot++; | 
|---|
| 167 |  | 
|---|
| 168 | return Socket(s); | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 |  | 
|---|
| 172 | /*! | 
|---|
| 173 | \class SOPHYA::ClientSocket | 
|---|
| 174 | \ingroup SysTools | 
|---|
| 175 | \brief Socket wrapper class for the client side | 
|---|
| 176 | */ | 
|---|
| 177 |  | 
|---|
| 178 | /* --Methode-- */ | 
|---|
| 179 | ClientSocket::ClientSocket(string const& srvname, int port) | 
|---|
| 180 | : Socket() | 
|---|
| 181 | { | 
|---|
| 182 | InitConnection(srvname.c_str(), port); | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | /* --Methode-- */ | 
|---|
| 186 | ClientSocket::ClientSocket(const char* srvname, int port) | 
|---|
| 187 | : Socket() | 
|---|
| 188 | { | 
|---|
| 189 | InitConnection(srvname, port); | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | /* --Methode-- */ | 
|---|
| 193 | void ClientSocket::InitConnection(const char* name, int port) | 
|---|
| 194 | { | 
|---|
| 195 | struct hostent *server; | 
|---|
| 196 | int s,rc; | 
|---|
| 197 | #if defined(Linux)  || defined(linux) | 
|---|
| 198 | typedef unsigned long in_addr_t; | 
|---|
| 199 | #endif | 
|---|
| 200 | in_addr_t inad; | 
|---|
| 201 |  | 
|---|
| 202 | server = NULL; | 
|---|
| 203 | /*  Try to find out if this is a known machine name  */ | 
|---|
| 204 | server = gethostbyname(name); | 
|---|
| 205 | if(!server) { | 
|---|
| 206 | /*  Try it as an internet address  a.b.c.d  */ | 
|---|
| 207 | /*    inad = inet_addr(name); | 
|---|
| 208 | if(inad == INADDR_NONE) { | 
|---|
| 209 | cout << "ClientSocket() Host identification failed inet_addr(" | 
|---|
| 210 | << name << ")" << endl; | 
|---|
| 211 | throw SocketException("ClientSocket() Host identification failed "); | 
|---|
| 212 | } | 
|---|
| 213 | */ | 
|---|
| 214 | server = gethostbyaddr(name, strlen(name), AF_INET); | 
|---|
| 215 | } | 
|---|
| 216 | if(!server) { | 
|---|
| 217 | cout << "ClientSocket() Host identification failed (2) for" | 
|---|
| 218 | << name << endl; | 
|---|
| 219 | throw SocketException("ClientSocket() Host identification failed (2)"); | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 | ipskt.sin_family = AF_INET; | 
|---|
| 223 | ipskt.sin_port = htons(port); | 
|---|
| 224 | memcpy(&(ipskt.sin_addr), server->h_addr, server->h_length); | 
|---|
| 225 | s = socket(AF_INET, SOCK_STREAM, 0); | 
|---|
| 226 | if (s < 0) { | 
|---|
| 227 | cout << "ClientSocket() Erreur: Pb socket() ErrNo=" << errno << endl; | 
|---|
| 228 | throw SocketException("ClientSocket() Erreur: Pb socket()"); | 
|---|
| 229 | } | 
|---|
| 230 |  | 
|---|
| 231 |  | 
|---|
| 232 | skt = s; | 
|---|
| 233 | portid = port; | 
|---|
| 234 | /* Je fais un cast explicit de sockaddr_in * (Internet) en sockaddr *   */ | 
|---|
| 235 | rc = connect(skt, (struct sockaddr *)(&ipskt), sizeof(struct sockaddr_in)); | 
|---|
| 236 | if (rc < 0) { | 
|---|
| 237 | cout << "ClientSocket() Erreur: Pb connect() ErrNo=" << errno << endl; | 
|---|
| 238 | Close(); | 
|---|
| 239 | throw SocketException("ClientSocket() Erreur: Pb connect()"); | 
|---|
| 240 | } | 
|---|
| 241 | } | 
|---|