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