1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
//
// socketstream.cxx, derived from Socket.cpp by Maciej Sobczak
// C/C++ User Journal, December 2001.
// Modified by Hugues Talbot to allow for a timeout on read.
//
// this file contains the wrappers (implementation)
// that can be used as a iostream-compatible TCP/IP sockets
//
// on Windows the program that uses this library
// should be linked with Ws2_32.lib
//
// Copyright (C) 2001 Maciej Sobczak
//
// you can use this code for any purpose without limitations
// (and for your own risk) as long as this notice remains
//
#include <sstream>
#include "socketstream.hxx"
using namespace std;
// class SocketException
SocketRunTimeException::SocketRunTimeException(const string &what)
: runtime_error(what)
{
#ifdef WIN32_NOTCYGWIN
errnum = ::WSAGetLastError();
#else
errnum = errno;
#endif
}
const char * SocketRunTimeException::what() const throw()
{
ostringstream ss;
ss << runtime_error::what();
ss << ", " << strerror(errnum);
msg = ss.str();
return msg.c_str();
}
// class TCPSocketWrapper::TCPAcceptedSocket
TCPSocketWrapper::TCPAcceptedSocket::TCPAcceptedSocket
(socket_type s, sockaddr_in a)
: sock(s), addr(a)
{
}
// class TCPSocketWrapper
TCPSocketWrapper::TCPSocketWrapper()
: sockstate(CLOSED)
{
}
TCPSocketWrapper::TCPSocketWrapper
(const TCPSocketWrapper::TCPAcceptedSocket &as)
: sock(as.sock), sockaddress(as.addr), sockstate(ACCEPTED)
{
}
TCPSocketWrapper::~TCPSocketWrapper()
{
if (sockstate != CLOSED) {
#ifdef WIN32_NOTCYGWIN
closesocket(sock);
#else
::close(sock);
#endif
}
}
void TCPSocketWrapper::listen(int port, int backlog)
{
if (sockstate != CLOSED)
throw SocketLogicException("socket not in CLOSED state");
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET) {
throw SocketRunTimeException("socket failed");
}
sockaddr_in local;
memset(&local, 0, sizeof(local));
local.sin_family = AF_INET;
local.sin_port = htons((u_short)port);
local.sin_addr.s_addr = htonl(INADDR_ANY);
if (::bind(sock, (sockaddr*)&local, sizeof(local)) == SOCKET_ERROR)
throw SocketRunTimeException("bind failed");
if (::listen(sock, backlog) == SOCKET_ERROR)
throw SocketRunTimeException("listen failed");
memset(&sockaddress, 0, sizeof(sockaddress));
sockstate = LISTENING;
}
TCPSocketWrapper::TCPAcceptedSocket TCPSocketWrapper::accept()
{
if (sockstate != LISTENING)
throw SocketLogicException("socket not listening");
sockaddr_in from;
#ifdef ACCEPT_USES_SOCKLEN_T
socklen_t len = sizeof(from);
#else
int len = sizeof(from);
#endif
memset(&from, 0, len);
socket_type newsocket = ::accept(sock, (sockaddr*)&from, &len);
if (newsocket == INVALID_SOCKET)
throw SocketRunTimeException("accept failed");
return TCPAcceptedSocket(newsocket, from);
}
void TCPSocketWrapper::connect(const char *address, int port)
{
if (sockstate != CLOSED)
throw SocketLogicException("socket not in CLOSED state");
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET) {
throw SocketRunTimeException("socket failed");
}
hostent *hp;
unsigned long addr = inet_addr(address);
if (addr != INADDR_NONE)
hp = gethostbyaddr((const char*)&addr, 4, AF_INET);
else
hp = gethostbyname(address);
if (hp == NULL)
throw SocketRunTimeException("cannot resolve address");
if (hp->h_addrtype != AF_INET)
throw SocketRunTimeException
("address resolved with TCP incompatible type");
memset(&sockaddress, 0, sizeof(sockaddress));
memcpy(&(sockaddress.sin_addr), hp->h_addr_list[0], hp->h_length);
sockaddress.sin_family = AF_INET;
sockaddress.sin_port = htons((u_short)port);
if (::connect(sock, (sockaddr*)&sockaddress, sizeof(sockaddress))
== SOCKET_ERROR)
throw SocketRunTimeException("connect failed");
sockstate = CONNECTED;
}
const char * TCPSocketWrapper::address() const
{
if (sockstate != CONNECTED && sockstate != ACCEPTED)
throw SocketLogicException("socket not connected");
return inet_ntoa(sockaddress.sin_addr);
}
int TCPSocketWrapper::port() const
{
if (sockstate != CONNECTED && sockstate != ACCEPTED)
throw SocketLogicException("socket not connected");
return ntohs(sockaddress.sin_port);
}
void TCPSocketWrapper::write(const void *buf, int len)
{
if (sockstate != CONNECTED && sockstate != ACCEPTED)
throw SocketLogicException("socket not connected");
int written;
while (len > 0) {
if ((written = send(sock, (const char*)buf, len, 0)) == SOCKET_ERROR)
throw SocketRunTimeException("write failed");
len -= written;
buf = (const char*)buf + written;
}
}
int TCPSocketWrapper::read(void *buf, int len)
{
if (sockstate != CONNECTED && sockstate != ACCEPTED)
throw SocketLogicException("socket not connected");
int readn = recv(sock, (char*)buf, len, 0);
if (readn == SOCKET_ERROR)
throw SocketRunTimeException("read failed");
return readn;
}
void TCPSocketWrapper::close()
{
if (sockstate != CLOSED) {
#ifdef WIN32_NOTCYGWIN
if (closesocket(sock) == SOCKET_ERROR)
throw SocketRunTimeException("close failed");
#else
if (::close(sock))
throw SocketRunTimeException("close failed");
#endif
sockstate = CLOSED;
}
}
bool socketsInit()
{
#ifdef WIN32_NOTCYGWIN
WSADATA wsadata;
if (WSAStartup(MAKEWORD(2, 0), &wsadata) == 0)
return true;
else
return false;
#else
// Linux/Unix do not require any initialization
return true;
#endif
}
void socketsEnd()
{
#ifdef WIN32_NOTCYGWIN
// we do not care about the error codes
// anyway, we end the program
WSACleanup();
#endif
}
|