35 #include <sys/types.h> 36 #include <sys/socket.h> 37 #include <arpa/inet.h> 50 #include "BESInternalError.h" 52 Socket::Socket(
int socket,
struct sockaddr *addr) :
53 _socket(socket), _connected(true), _listening(false), _addr_set(true)
58 switch (addr->sa_family) {
60 inet_ntop(AF_INET, &(((
struct sockaddr_in *) addr)->sin_addr), ip,
sizeof(ip));
61 port = ntohs (((
struct sockaddr_in *)addr)->sin_port);
64 inet_ntop(AF_INET6, &(((
struct sockaddr_in6 *) addr)->sin6_addr), ip,
sizeof(ip));
65 port = ntohs (((
struct sockaddr_in6 *)addr)->sin6_port);
68 snprintf(ip,
sizeof(ip),
"UNKNOWN FAMILY: %d", addr->sa_family);
86 void Socket::send(
const string &str,
int start,
int end)
88 string send_str = str.substr(start, end);
89 int bytes_written = write(_socket, send_str.c_str(), send_str.length());
90 if (bytes_written == -1) {
91 string err(
"socket failure, writing on stream socket");
92 const char* error_info = strerror(errno);
93 if (error_info) err +=
" " + (string) error_info;
98 int Socket::receive(
char *inBuff,
const int inSize)
105 while ((bytesRead = read(_socket, inBuff, inSize)) < 1) {
106 if (errno == EINTR || errno == EAGAIN) {
110 *(BESLog::TheLog()) <<
"Socket::receive: errno: " << strerror(errno) <<
", bytesRead: " << bytesRead << endl;
116 std::ostringstream oss;
117 oss <<
"Socket::receive: socket failure, reading on stream socket: " << strerror(errno) <<
", bytesRead: " 121 else if (bytesRead == 0)
136 strm << BESIndent::LMarg <<
"Socket::dump - (" << (
void *)
this <<
")" << endl;
138 strm << BESIndent::LMarg <<
"socket: " << _socket << endl;
139 strm << BESIndent::LMarg <<
"is connected? " << _connected << endl;
140 strm << BESIndent::LMarg <<
"is listening? " << _listening << endl;
141 strm << BESIndent::LMarg <<
"socket address set? " << _addr_set << endl;
143 strm << BESIndent::LMarg <<
"socket port: " << _port << endl;
144 strm << BESIndent::LMarg <<
"socket ip: " << _ip << endl;
146 BESIndent::UnIndent();
exception thrown if inernal error encountered
virtual void dump(ostream &strm) const
dumps information about this object