43#include <sys/socket.h>
51#include "BESInternalError.h"
55Socket::Socket(
int socket,
struct sockaddr *addr) :
56 _socket(socket), _connected(true), _listening(false), _addr_set(true)
61 switch (addr->sa_family) {
63 inet_ntop(AF_INET, &(((
struct sockaddr_in *) addr)->sin_addr), ip,
sizeof(ip));
64 port = ntohs (((
struct sockaddr_in *)addr)->sin_port);
67 inet_ntop(AF_INET6, &(((
struct sockaddr_in6 *) addr)->sin6_addr), ip,
sizeof(ip));
68 port = ntohs (((
struct sockaddr_in6 *)addr)->sin6_port);
71 snprintf(ip,
sizeof(ip),
"UNKNOWN FAMILY: %d", addr->sa_family);
89void Socket::send(
const string &str,
int start,
int end)
91 string send_str = str.substr(start, end);
92 int bytes_written = write(_socket, send_str.c_str(), send_str.length());
93 if (bytes_written == -1) {
94 string err(
"socket failure, writing on stream socket");
95 const char* error_info = strerror(errno);
96 if (error_info) err +=
" " + (string) error_info;
101int Socket::receive(
char *inBuff,
const int inSize)
108 while ((bytesRead = read(_socket, inBuff, inSize)) < 1) {
109 if (errno == EINTR || errno == EAGAIN) {
113 ERROR_LOG(
"Socket::receive: errno: " << strerror(errno) <<
", bytesRead: " << bytesRead << endl);
119 std::ostringstream oss;
120 oss <<
"Socket::receive: socket failure, reading on stream socket: " << strerror(errno) <<
", bytesRead: "
124 else if (bytesRead == 0)
139 strm << BESIndent::LMarg <<
"Socket::dump - (" << (
void *)
this <<
")" << endl;
141 strm << BESIndent::LMarg <<
"socket: " << _socket << endl;
142 strm << BESIndent::LMarg <<
"is connected? " << _connected << endl;
143 strm << BESIndent::LMarg <<
"is listening? " << _listening << endl;
144 strm << BESIndent::LMarg <<
"socket address set? " << _addr_set << endl;
146 strm << BESIndent::LMarg <<
"socket port: " << _port << endl;
147 strm << BESIndent::LMarg <<
"socket ip: " << _ip << endl;
149 BESIndent::UnIndent();
exception thrown if internal error encountered
virtual void dump(std::ostream &strm) const
dumps information about this object