8 #include "daq/slc/system/TCPSocket.h"
10 #include <daq/slc/base/IOException.h>
15 #include <sys/socket.h>
16 #include <arpa/inet.h>
19 #include <netinet/in.h>
25 int TCPSocket::connect(
const std::string& ip,
unsigned short port)
32 int TCPSocket::connect()
38 memset(&addr, 0,
sizeof(sockaddr_in));
39 addr.sin_family = AF_INET;
40 addr.sin_addr.s_addr = INADDR_ANY;
41 addr.sin_port = htons(m_port);
43 if ((m_fd = ::socket(PF_INET, SOCK_STREAM, 0)) < 0) {
46 struct hostent* host = NULL;
47 host = gethostbyname(m_ip.c_str());
49 unsigned long ip_address = inet_addr(m_ip.c_str());
50 if ((
signed long) ip_address < 0) {
53 host = gethostbyaddr((
char*)&ip_address,
sizeof(ip_address), AF_INET);
59 m_ip.c_str(), m_port));
61 addr.sin_addr.s_addr = (*(
unsigned long*) host->h_addr_list[0]);
63 if (::connect(m_fd, (
struct sockaddr*)&addr,
sizeof(addr)) < 0) {
66 m_ip.c_str(), m_port));
72 void TCPSocket::setBufferSize(
int size)
75 if (setsockopt(m_fd, SOL_SOCKET, SO_SNDBUF, &size,
sizeof(size)) != 0) {
76 throw (
IOException(
"failed to SO_SNDBUF: %s\n", strerror(errno)));
78 if (setsockopt(m_fd, SOL_SOCKET, SO_RCVBUF, &size,
sizeof(size)) != 0) {
79 throw (
IOException(
"error on SO_RCVBUF: %s\n", strerror(errno)));
84 size_t TCPSocket::write(
const void* buf,
size_t count)
90 ret = send(m_fd, ((
unsigned char*)buf + c), (count - c), MSG_NOSIGNAL);
108 size_t TCPSocket::read(
void* buf,
size_t count)
114 ret = recv(m_fd, ((
unsigned char*)buf + c), (count - c), 0);
117 case EINTR:
continue;
118 case EAGAIN:
continue;
120 throw (
IOException(
"TCPSocket::read Error while reading."));
128 size_t TCPSocket::read_once(
void* buf,
size_t count)
133 ret = recv(m_fd, buf, count, 0);
136 case EINTR:
continue;
137 case EAGAIN:
continue;
139 throw (
IOException(
"TCPSocket::read_once Error while reading."));
147 void TCPSocket::print()
150 memset(&sa, 0,
sizeof(sockaddr_in));
151 socklen_t sa_len =
sizeof(sa);
152 if (getsockname(m_fd, (
struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) {
153 perror(
"getsockname");
155 printf(
"Local IP address is: %s\n", inet_ntoa(sa.sin_addr));
156 printf(
"Local port is: %d\n", (
int) ntohs(sa.sin_port));
159 const std::string TCPSocket::getLocalIP()
162 memset(&sa, 0,
sizeof(sockaddr_in));
163 socklen_t sa_len =
sizeof(sa);
164 if (getsockname(m_fd, (
struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) {
167 return inet_ntoa(sa.sin_addr);
170 int TCPSocket::getLocalAddress()
173 memset(&sa, 0,
sizeof(sockaddr_in));
174 socklen_t sa_len =
sizeof(sa);
175 if (getsockname(m_fd, (
struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) {
178 return sa.sin_addr.s_addr;
181 int TCPSocket::getLocalPort()
184 memset(&sa, 0,
sizeof(sockaddr_in));
185 socklen_t sa_len =
sizeof(sa);
186 if (getsockname(m_fd, (
struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) {
189 return ntohs(sa.sin_port);
192 unsigned int TCPSocket::getAddress()
194 struct hostent* host = NULL;
195 host = gethostbyname(m_ip.c_str());
197 unsigned long ip_address = inet_addr(m_ip.c_str());
198 if ((
signed long) ip_address < 0) {
199 throw (std::exception());
202 host = gethostbyaddr((
char*)&ip_address,
sizeof(ip_address), AF_INET);
205 return (*(
unsigned long*) host->h_addr_list[0]);
Abstract base class for different kinds of events.