40#include <netinet/in.h>
43#define INVALID_SOCKET -1
44#define SOCKET_ERROR -1
70 ((
struct sockaddr_in *) &
m_HostAddr)->sin_family = AF_INET;
71 ((
struct sockaddr_in *) &
m_PeerAddr)->sin_family = AF_INET;
134 tmp = inet_ntoa(((
struct sockaddr_in *) &
m_HostAddr)->sin_addr);
135 ret += tmp ? tmp :
"none:none";
138 sprintf(nbuf,
"%d", ntohs(((
struct sockaddr_in *) &
m_HostAddr)->sin_port));
142 tmp = inet_ntoa(((
struct sockaddr_in *) &
m_PeerAddr)->sin_addr);
143 ret += tmp ? tmp :
"none:none";
146 sprintf(nbuf,
"%d", ntohs(((
struct sockaddr_in *) &
m_PeerAddr)->sin_port));
153connect(
const char *
const Peer,
int PeerPort,
const char *
const Host,
int HostPort)
167 if (!
setPeer(Peer, PeerPort)) {
183listen(
const char *
const Host,
int Port)
221 len =
sizeof(
struct sockaddr);
235 int flags = fcntl(accepted->
m_Socket, F_GETFL, 0);
236 flags &= ~O_NONBLOCK;
237 fcntl(accepted->
m_Socket, F_SETFL, flags);
246 *Peer = inet_ntoa(((
struct sockaddr_in *) &accepted->
m_PeerAddr)->sin_addr);
255 fd_set fileDescriptorSet;
256 FD_ZERO(&fileDescriptorSet);
257 FD_SET(
m_Socket, &fileDescriptorSet);
258 FD_SET(cancellationFd, &fileDescriptorSet);
259 int result = select(max(
m_Socket, cancellationFd) + 1, &fileDescriptorSet,
nullptr,
nullptr,
nullptr);
262 if (result == -1 && errno == EINTR) {
267 if (FD_ISSET(cancellationFd, &fileDescriptorSet)) {
285 return (select(
m_Socket + 1, &io, NULL, NULL, &t) != 0) ? true :
false;
308 bp = buff =
new unsigned char[l];
321 return (
a.getLen() == 0) ? 0 : 1;
328 uint32_t hl = htonl(l);
334 b.
addBytes(
reinterpret_cast<const unsigned char *
>(&hl),
sizeof(hl));
352recv(
void *buf,
int len,
int flags)
363send(
const void *
const buf,
int len,
int flags)
406 if (setsockopt(
m_Socket, SOL_SOCKET, SO_REUSEADDR,
407 (
const char *)&one,
sizeof(
int)) < 0)
408 cerr <<
"Warning: Unable to set SO_REUSEADDR option\n";
425bindInRange(
const char *
const Host,
int Low,
int High,
int Retries)
444 if (Retries > High - Low) {
445 for (port = Low; port <= High; port++) {
456 for (i = 0; i < Retries; i++) {
457 port = Low + (rand() % (High - Low));
473linger(
bool LingerOn,
int LingerTime)
487 l.l_linger = LingerTime;
492 i = setsockopt(
m_Socket, SOL_SOCKET, SO_LINGER, (
const char *) &l,
sizeof(l));
509 m_Socket = ::socket(PF_INET, SOCK_STREAM, 0);
523setPeer(
const char *
const Peer,
int Port)
525 struct hostent *he = NULL;
529 if (!isdigit(Peer[0]))
532 he = gethostbyname(Peer);
534 struct in_addr ipaddr;
536 if (!inet_aton(Peer, &ipaddr)) {
540 he = gethostbyaddr((
const char *)&ipaddr.s_addr,
sizeof(ipaddr.s_addr), PF_INET);
546 memcpy(&((
struct sockaddr_in *)&
m_PeerAddr)->sin_addr, he->h_addr_list[0],
547 sizeof(((
struct sockaddr_in *)&
m_PeerAddr)->sin_addr));
551 ((
struct sockaddr_in *)&
m_PeerAddr)->sin_port = htons(Port);
556getPeer(
string *Peer,
int *Port)
561 peer = inet_ntoa(((
struct sockaddr_in *) &
m_PeerAddr)->sin_addr);
569 *Port = ntohs(((
struct sockaddr_in *) &
m_PeerAddr)->sin_port);
574setHost(
const char *
const Host,
int Port)
580 if (!isdigit(Host[0]))
583 he = gethostbyname(Host);
584 he = gethostbyname(Host);
586 struct in_addr ipaddr;
588 if (!inet_aton(Host, &ipaddr)) {
592 he = gethostbyaddr((
const char *)&ipaddr.s_addr,
sizeof(ipaddr.s_addr), PF_INET);
598 memcpy(&((
struct sockaddr_in *)&
m_HostAddr)->sin_addr, he->h_addr_list[0],
599 sizeof(((
struct sockaddr_in *)&
m_HostAddr)->sin_addr));
604 ((
struct sockaddr_in *)&
m_HostAddr)->sin_port = htons(Port);
609getHost(
string *Host,
int *Port)
614 host = inet_ntoa(((
struct sockaddr_in *)&
m_HostAddr)->sin_addr);
622 *Port = ntohs(((
struct sockaddr_in *)&
m_HostAddr)->sin_port);
A simple thread-safe wrapper for select()
void remIO(const int fd)
Removes a file descriptor from the set of descriptors.
void addIO(const int fd)
Adds a file descriptor to the set of descriptors.
A class for dealing with sockets.
bool bindInRange(const char *const Host, int Low, int High, int Retries)
Tries repeated binds to a local address and port.
bool closeSocket(void)
Closes the connection.
bool linger(bool LingerOn, int LingerTime=0)
Sets the linger parameter of the socket.
bool setPeer(const char *const Peer, int Port)
void setWatch(IOWatch *watch)
Registers an IOWatch for this socket.
virtual bool listen(const char *const Host, int Port)
Starts listening.
bool getPeer(std::string *Peer, int *Port)
Retrieves peer information.
virtual bool reconnect()
Reopens the connection after closing it.
bool setHost(const char *const Host, int Port)
struct sockaddr m_HostAddr
TCPSocket()
Constructs a TCPSocket.
int getBufferStore(bufferStore &a, bool wait=true)
Receive data into a bufferStore .
bool sendBufferStore(const bufferStore &a)
Sends data from a bufferStore .
virtual bool createSocket(void)
Creates the socket.
bool getHost(std::string *Host, int *Port)
Retrieves local information.
virtual std::string toString()
Retrieve a string representation of the TCPSocket.
int send(const void *const buf, int len, int flags)
bool bindSocket(const char *const Host, int Port)
Binds to a local address and port.
TCPSocket * accept(std::string *Peer)
Accept a connection; blocking, non-cancellable.
struct sockaddr m_PeerAddr
int recv(void *buf, int len, int flags)
bool dataToGet(int sec, int usec) const
Check and optionally wait for incoming data.
virtual ~TCPSocket()
Destructor.
virtual bool connect(const char *const Peer, int PeerPort, const char *const Host=NULL, int HostPort=0)
Connects to a given host.
A generic container for an array of bytes.
void addBuff(const bufferStore &b, long maxLen=-1)
Appends data to the content of this instance.
void addBytes(const unsigned char *buf, int len)
Appends data to the content of this instance.
const char * getString(long pos=0) const
Retrieves the characters at index pos.