10 #include <sys/socket.h>
11 #include <netinet/in.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
23 #include "artdaq/DAQdata/Globals.hh"
30 struct hostent* hostent_sp;
34 if (regex_match(host_in, mm, std::regex(
"([^:]+):(\\d+)")))
38 else if (regex_match(host_in, mm, std::regex(
":{0,1}(\\d+)")))
40 host = std::string(
"127.0.0.1");
42 else if (regex_match(host_in, mm, std::regex(
"([^:]+):{0,1}")))
44 host = mm[1].str().c_str();
48 host = std::string(
"127.0.0.1");
50 TLOG_INFO(
"TCPConnect") <<
"Resolving host " << host << TLOG_ENDL;
52 bzero((
char *)&addr,
sizeof(addr));
54 if (regex_match(host.c_str(), mm, std::regex(
"\\d+(\\.\\d+){3}")))
55 inet_aton(host.c_str(), &addr);
58 hostent_sp = gethostbyname(host.c_str());
61 perror(
"gethostbyname");
64 addr = *(
struct in_addr *)(hostent_sp->h_addr_list[0]);
70 int ResolveHost(
char const* host_in,
int dflt_port, sockaddr_in& sin)
74 struct hostent* hostent_sp;
78 if (regex_match(host_in, mm, std::regex(
"([^:]+):(\\d+)")))
81 port = strtoul(mm[2].str().c_str(), NULL, 0);
83 else if (regex_match(host_in, mm, std::regex(
":{0,1}(\\d+)")))
85 host = std::string(
"127.0.0.1");
86 port = strtoul(mm[1].str().c_str(), NULL, 0);
88 else if (regex_match(host_in, mm, std::regex(
"([^:]+):{0,1}")))
90 host = mm[1].str().c_str();
95 host = std::string(
"127.0.0.1");
98 TLOG_INFO(
"TCPConnect") <<
"Resolving host " << host <<
", on port " << std::to_string(port) << TLOG_ENDL;
100 if (host ==
"localhost") host =
"127.0.0.1";
102 bzero((
char *)&sin,
sizeof(sin));
103 sin.sin_family = AF_INET;
104 sin.sin_port = htons(port);
106 if (regex_match(host.c_str(), mm, std::regex(
"\\d+(\\.\\d+){3}")))
107 inet_aton(host.c_str(), &sin.sin_addr);
110 hostent_sp = gethostbyname(host.c_str());
113 perror(
"gethostbyname");
116 sin.sin_addr = *(
struct in_addr *)(hostent_sp->h_addr_list[0]);
128 struct sockaddr_in sin;
131 s_fd = socket(PF_INET, SOCK_STREAM, 0);
135 perror(
"socket error");
146 sts = connect(s_fd, (
struct sockaddr *)&sin,
sizeof(sin));
156 sts = fcntl(s_fd, F_SETFL, flags);
157 TRACE( 4,
"TCPConnect fcntl(fd=%d,flags=0x%lx)=%d",s_fd,flags,sts );
163 socklen_t lenlen =
sizeof(len);
165 sts = getsockopt(s_fd, SOL_SOCKET, SO_SNDBUF, &len, &lenlen);
166 TRACE(3,
"TCPConnect SNDBUF initial: %d sts/errno=%d/%d lenlen=%d", len, sts, errno, lenlen);
168 sts = setsockopt(s_fd, SOL_SOCKET, SO_SNDBUF, &len, lenlen);
170 TRACE(0,
"Error with setsockopt SNDBUF %d", errno);
172 sts = getsockopt(s_fd, SOL_SOCKET, SO_SNDBUF, &len, &lenlen);
173 if (len < (sndbufsiz * 2))
174 TRACE(1,
"SNDBUF %d not expected (%d) sts/errno=%d/%d"
175 , len, sndbufsiz, sts, errno);
177 TRACE(3,
"SNDBUF %d sts/errno=%d/%d", len, sts, errno);
int ResolveHost(char const *host_in, in_addr &addr)
Convert a string hostname to a in_addr suitable for socket communication.
int TCPConnect(char const *host_in, int dflt_port, long flags=0, int sndbufsiz=0)
Connect to a host on a given port.