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)
89 int ret = send(m_fd, ((
unsigned char*)buf + c), (count - c), MSG_NOSIGNAL);
107 size_t TCPSocket::read(
void* buf,
size_t count)
112 int ret = recv(m_fd, ((
unsigned char*)buf + c), (count - c), 0);
115 case EINTR:
continue;
116 case EAGAIN:
continue;
118 throw (
IOException(
"TCPSocket::read Error while reading."));
126 size_t TCPSocket::read_once(
void* buf,
size_t count)
130 int ret = recv(m_fd, buf, count, 0);
133 case EINTR:
continue;
134 case EAGAIN:
continue;
136 throw (
IOException(
"TCPSocket::read_once Error while reading."));
145 void TCPSocket::print()
148 memset(&sa, 0,
sizeof(sockaddr_in));
149 socklen_t sa_len =
sizeof(sa);
150 if (getsockname(m_fd, (
struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) {
151 perror(
"getsockname");
153 printf(
"Local IP address is: %s\n", inet_ntoa(sa.sin_addr));
154 printf(
"Local port is: %d\n", (
int) ntohs(sa.sin_port));
157 const std::string TCPSocket::getLocalIP()
160 memset(&sa, 0,
sizeof(sockaddr_in));
161 socklen_t sa_len =
sizeof(sa);
162 if (getsockname(m_fd, (
struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) {
165 return inet_ntoa(sa.sin_addr);
168 int TCPSocket::getLocalAddress()
171 memset(&sa, 0,
sizeof(sockaddr_in));
172 socklen_t sa_len =
sizeof(sa);
173 if (getsockname(m_fd, (
struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) {
176 return sa.sin_addr.s_addr;
179 int TCPSocket::getLocalPort()
182 memset(&sa, 0,
sizeof(sockaddr_in));
183 socklen_t sa_len =
sizeof(sa);
184 if (getsockname(m_fd, (
struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) {
187 return ntohs(sa.sin_port);
190 unsigned int TCPSocket::getAddress()
192 struct hostent* host = NULL;
193 host = gethostbyname(m_ip.c_str());
195 unsigned long ip_address = inet_addr(m_ip.c_str());
196 if ((
signed long) ip_address < 0) {
197 throw (std::exception());
200 host = gethostbyaddr((
char*)&ip_address,
sizeof(ip_address), AF_INET);
203 return (*(
unsigned long*) host->h_addr_list[0]);
Abstract base class for different kinds of events.