[0b990d] | 1 | #ifdef __GNUG__
|
---|
| 2 | #pragma implementation
|
---|
| 3 | #endif
|
---|
| 4 |
|
---|
| 5 | #include <sys/types.h>
|
---|
| 6 | #include <sys/socket.h>
|
---|
| 7 | #include <arpa/inet.h>
|
---|
| 8 | #include <errno.h>
|
---|
| 9 | #include <netinet/in.h>
|
---|
| 10 | #include <netdb.h>
|
---|
| 11 |
|
---|
| 12 | #include <iostream>
|
---|
| 13 |
|
---|
| 14 | #include "socket.h"
|
---|
| 15 | #include "except.h"
|
---|
| 16 |
|
---|
| 17 | /////////////////////////////////////////////////////////////////////
|
---|
| 18 | // TCPSocket
|
---|
| 19 |
|
---|
| 20 | TCPSocket::TCPSocket()
|
---|
| 21 | {
|
---|
| 22 | socket_ = 0;
|
---|
| 23 | initialized_ = false;
|
---|
| 24 | bound_ = false;
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | TCPSocket::~TCPSocket()
|
---|
| 28 | {
|
---|
| 29 | if (initialized_) close();
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | void
|
---|
| 33 | TCPSocket::create()
|
---|
| 34 | {
|
---|
| 35 | do { errno = 0; socket_ = socket(PF_INET, SOCK_STREAM, 0); } while (errno == EINTR);
|
---|
| 36 | if (socket_ < 0) throw errno_exception("TCPSocket::create");
|
---|
| 37 | initialized_ = true;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | void
|
---|
| 41 | TCPSocket::bind(u_int16_t port_begin, u_int16_t port_fence)
|
---|
| 42 | {
|
---|
| 43 | if (!initialized_)
|
---|
| 44 | throw std::runtime_error("TCPSocket::bind: not initialized");
|
---|
| 45 |
|
---|
| 46 | struct sockaddr_in ssockaddr;
|
---|
| 47 | ssockaddr.sin_addr.s_addr = INADDR_ANY;
|
---|
| 48 | ssockaddr.sin_family = AF_INET;
|
---|
| 49 | u_int16_t port = port_begin;
|
---|
| 50 | for (port=port_begin; port < port_fence; port++) {
|
---|
| 51 | ssockaddr.sin_port = htons(port);
|
---|
| 52 | if (::bind(socket_, (sockaddr*)&ssockaddr, sizeof(ssockaddr))==0) {
|
---|
| 53 | port_ = port;
|
---|
| 54 | bound_ = true;
|
---|
| 55 | return;
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | throw errno_exception("TCPSocket::bind: failed");
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | void
|
---|
| 62 | TCPSocket::close()
|
---|
| 63 | {
|
---|
| 64 | if (!initialized_)
|
---|
| 65 | throw std::runtime_error("TCPSocket::close: not initialized");
|
---|
| 66 |
|
---|
| 67 | if (::close(socket_) != 0)
|
---|
| 68 | throw errno_exception("TCPSocket::close");
|
---|
| 69 |
|
---|
| 70 | initialized_ = false;
|
---|
| 71 | bound_ = false;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | u_int32_t
|
---|
| 75 | TCPSocket::addr()
|
---|
| 76 | {
|
---|
| 77 | char hostname[256];
|
---|
| 78 | gethostname(hostname,256);
|
---|
| 79 | struct hostent *hent = gethostbyname(hostname);
|
---|
| 80 | std::cout << "hostname = " << hostname << std::endl;
|
---|
| 81 | u_int32_t add = htonl(*(unsigned long*)hent->h_addr_list[0]);
|
---|
| 82 | std::cout << "TCPSocket::addr() = " << (void*)add << std::endl;
|
---|
| 83 | return add;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /////////////////////////////////////////////////////////////////////
|
---|
| 87 | // TCPIOSocket
|
---|
| 88 |
|
---|
| 89 | int
|
---|
| 90 | TCPIOSocket::read(void *d, int n)
|
---|
| 91 | {
|
---|
| 92 | if (!initialized_) throw std::runtime_error("not inited for read");
|
---|
| 93 |
|
---|
| 94 | int nleft = n;
|
---|
| 95 | while (nleft) {
|
---|
| 96 | int ntransfer = ::read(socket_, d, n);
|
---|
| 97 | if (ntransfer == -1 ) throw errno_exception("socket read");
|
---|
| 98 | nleft -= ntransfer;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | return n;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | int
|
---|
| 105 | TCPIOSocket::write(const void *d, int n)
|
---|
| 106 | {
|
---|
| 107 | if (!initialized_) throw std::runtime_error("not inited for write");
|
---|
| 108 |
|
---|
| 109 | int nleft = n;
|
---|
| 110 | while (nleft) {
|
---|
| 111 | int ntransfer = ::write(socket_, d, n);
|
---|
| 112 | if (ntransfer == -1 ) throw errno_exception("socket write");
|
---|
| 113 | nleft -= ntransfer;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | return n;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | int
|
---|
| 120 | TCPIOSocket::read_string(std::string &s)
|
---|
| 121 | {
|
---|
| 122 | int nbytes = 0;
|
---|
| 123 | int size;
|
---|
| 124 | nbytes += read_int(size);
|
---|
| 125 | char *dat = new char[size+1];
|
---|
| 126 | nbytes += read(dat, size);
|
---|
| 127 | dat[size] = '\0';
|
---|
| 128 | s = dat;
|
---|
| 129 | delete[] dat;
|
---|
| 130 | return nbytes;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | int
|
---|
| 134 | TCPIOSocket::write_string(const std::string &s)
|
---|
| 135 | {
|
---|
| 136 | int nbytes = 0;
|
---|
| 137 | int size = s.size();
|
---|
| 138 | nbytes += write_int(size);
|
---|
| 139 | nbytes += write(s.c_str(), s.size());
|
---|
| 140 | return nbytes;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | int
|
---|
| 144 | TCPIOSocket::read_int(int &i)
|
---|
| 145 | {
|
---|
| 146 | return read(&i, sizeof(int));
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | int
|
---|
| 150 | TCPIOSocket::write_int(int i)
|
---|
| 151 | {
|
---|
| 152 | return write(&i, sizeof(int));
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | int
|
---|
| 156 | TCPIOSocket::read_uint32(u_int32_t &i)
|
---|
| 157 | {
|
---|
| 158 | return read(&i, sizeof(u_int32_t));
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | int
|
---|
| 162 | TCPIOSocket::write_uint32(u_int32_t i)
|
---|
| 163 | {
|
---|
| 164 | return write(&i, sizeof(u_int32_t));
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | /////////////////////////////////////////////////////////////////////
|
---|
| 168 | // TCPServerSocket
|
---|
| 169 |
|
---|
| 170 | void
|
---|
| 171 | TCPServerSocket::listen(int queue_length)
|
---|
| 172 | {
|
---|
| 173 | if (!initialized_)
|
---|
| 174 | throw std::runtime_error("TCPServerSocket::listen: not initialized");
|
---|
| 175 |
|
---|
| 176 | if (::listen(socket_, queue_length))
|
---|
| 177 | throw errno_exception("TCPServerSocket::listen");
|
---|
| 178 |
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | /////////////////////////////////////////////////////////////////////
|
---|
| 182 | // TCPServerConnection
|
---|
| 183 |
|
---|
| 184 | void
|
---|
| 185 | TCPServerConnection::accept(const TCPServerSocket &s)
|
---|
| 186 | {
|
---|
| 187 | // this should not be initialized if used with accept
|
---|
| 188 | if (initialized_)
|
---|
| 189 | throw std::runtime_error("TCPServerConnection::accept: already initialized");
|
---|
| 190 |
|
---|
| 191 | struct sockaddr_in ssockaddr;
|
---|
| 192 | socklen_t len = sizeof(ssockaddr);
|
---|
| 193 | socket_ = ::accept(s.socket_,
|
---|
| 194 | (sockaddr*)&ssockaddr, &len);
|
---|
| 195 | if (socket_ < 0)
|
---|
| 196 | throw errno_exception("TCPServerConnection::accept");
|
---|
| 197 |
|
---|
| 198 | initialized_ = true;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | /////////////////////////////////////////////////////////////////////
|
---|
| 202 | // TCPClientConnection
|
---|
| 203 |
|
---|
| 204 | TCPClientConnection::TCPClientConnection()
|
---|
| 205 | {
|
---|
| 206 | connected_ = false;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | void
|
---|
| 210 | TCPClientConnection::close()
|
---|
| 211 | {
|
---|
| 212 | connected_ = false;
|
---|
| 213 | TCPIOSocket::close();
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | void
|
---|
| 217 | TCPClientConnection::connect(const char *remote_hostname,
|
---|
| 218 | u_int16_t remote_port)
|
---|
| 219 | {
|
---|
| 220 | if (!initialized_)
|
---|
| 221 | throw std::runtime_error("TCPClientConnection::connect: not initialized");
|
---|
| 222 |
|
---|
| 223 | struct hostent * remote_he = gethostbyname(remote_hostname);
|
---|
| 224 | u_int32_t remote_host = htonl(*(unsigned long*)remote_he->h_addr_list[0]);
|
---|
| 225 | connect(remote_host,remote_port);
|
---|
| 226 | connected_ = true;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | void
|
---|
| 230 | TCPClientConnection::connect(u_int32_t remote_host, u_int16_t remote_port)
|
---|
| 231 | {
|
---|
| 232 | if (!initialized_)
|
---|
| 233 | throw std::runtime_error("TCPClientConnection::connect: not initialized");
|
---|
| 234 |
|
---|
| 235 | struct sockaddr_in ssockaddr;
|
---|
| 236 | ssockaddr.sin_addr.s_addr = htonl(remote_host);
|
---|
| 237 | ssockaddr.sin_family = AF_INET;
|
---|
| 238 | ssockaddr.sin_port = htons(remote_port);
|
---|
| 239 |
|
---|
| 240 | if (::connect(socket_, (struct sockaddr*)&ssockaddr, sizeof(ssockaddr))) {
|
---|
| 241 | throw errno_exception("TCPClientConnection::connect");
|
---|
| 242 | }
|
---|
| 243 | connected_ = true;
|
---|
| 244 | }
|
---|
| 245 |
|
---|