1 #include "otsdaq-core/NetworkUtilities/Socket.h"
2 #include "otsdaq-core/MessageFacility/MessageFacility.h"
3 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
10 #include <arpa/inet.h>
30 __SS__ <<
"ERROR: This method should never be called. There is something wrong in your inheritance scheme!" << std::endl;
31 __MOUT__ <<
"\n" << ss.str();
37 Socket::Socket(
const std::string &IPAddress,
unsigned int port)
39 , IPAddress_ (IPAddress)
43 __MOUT__ << std::endl;
45 socketAddress_.sin_family = AF_INET;
46 socketAddress_.sin_port = htons(port);
48 __MOUT__ <<
"IPAddress: " << IPAddress <<
" port: " << port <<
" htons: " << socketAddress_.sin_port << std::endl;
49 if(inet_aton(IPAddress.c_str(), &socketAddress_.sin_addr) == 0)
51 __SS__ <<
"FATAL: Invalid IP/Port combination. Is it already in use? " <<
52 IPAddress <<
"/" << port << std::endl;
54 __MOUT__ <<
"\n" << ss.str();
55 throw std::runtime_error(ss.str());
58 memset(&(socketAddress_.sin_zero),
'\0', 8);
64 __MOUT__ <<
"CLOSING THE SOCKET #" << socketNumber_ <<
" IP: " << IPAddress_ <<
" port: " << port_ <<
" htons: " << socketAddress_.sin_port << std::endl;
65 if(socketNumber_ != -1)
70 void Socket::initialize(
void)
72 __MOUT__ <<
" htons: " << socketAddress_.sin_port << std::endl;
73 struct addrinfo hints;
78 memset(&hints, 0,
sizeof hints);
79 hints.ai_family = AF_INET;
80 hints.ai_socktype = SOCK_DGRAM;
81 hints.ai_flags = AI_PASSIVE;
83 bool socketInitialized =
false;
84 int fromPort = FirstSocketPort;
85 int toPort = LastSocketPort;
87 if(ntohs(socketAddress_.sin_port) != 0)
88 fromPort = toPort = ntohs(socketAddress_.sin_port);
90 std::stringstream port;
92 for(
int p=fromPort; p<=toPort && !socketInitialized; p++)
96 std::cout << __COUT_HDR_FL__ << __LINE__ <<
"]\tBinding port: " << port.str() << std::endl;
97 socketAddress_.sin_port = htons(p);
99 if((status = getaddrinfo(NULL, port.str().c_str(), &hints, &res)) != 0)
101 std::cout << __COUT_HDR_FL__ << __LINE__ <<
"]\tGetaddrinfo error status: " << status << std::endl;
106 socketNumber_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
108 std::cout << __COUT_HDR_FL__ << __LINE__ <<
"]\tSocket Number: " << socketNumber_ <<
" for port: " << ntohs(socketAddress_.sin_port) <<
" initialized." << std::endl;
110 if(bind(socketNumber_, res->ai_addr, res->ai_addrlen) == -1)
112 std::cout << __COUT_HDR_FL__ <<
"Error********Error********Error********Error********Error********Error" << std::endl;
113 std::cout << __COUT_HDR_FL__ <<
"FAILED BIND FOR PORT: " << port.str() <<
" ON IP: " << IPAddress_ << std::endl;
114 std::cout << __COUT_HDR_FL__ <<
"Error********Error********Error********Error********Error********Error" << std::endl;
119 std::cout << __COUT_HDR_FL__ <<
":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)" << std::endl;
120 std::cout << __COUT_HDR_FL__ <<
":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)" << std::endl;
121 std::cout << __COUT_HDR_FL__ <<
"SOCKET ON PORT: " << port.str() <<
" ON IP: " << IPAddress_ <<
" INITIALIZED OK!" << std::endl;
122 std::cout << __COUT_HDR_FL__ <<
":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)" << std::endl;
123 std::cout << __COUT_HDR_FL__ <<
":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)" << std::endl;
125 setsockopt(socketNumber_,SOL_SOCKET,SO_REUSEADDR,&yes,
sizeof(
int));
126 socketInitialized =
true;
127 std::cout << __COUT_HDR_FL__ << __LINE__ <<
"]\tSocket Number: " << socketNumber_ <<
" for port: " << ntohs(socketAddress_.sin_port) <<
" initialized." << std::endl;
133 if(!socketInitialized)
135 std::stringstream ss;
136 ss << __COUT_HDR_FL__ << __LINE__ <<
"FATAL: Socket could not initialize socket." << std::endl;
137 std::cout << ss.str();
143 const struct sockaddr_in& Socket::getSocketAddress(
void)
145 return socketAddress_;