8#include "daq/slc/system/TCPSocket.h"
10#include <daq/slc/base/IOException.h>
15#include <sys/socket.h>
19#include <netinet/in.h>
25int TCPSocket::connect(
const std::string& ip,
unsigned short port)
32int 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));
72void 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)));
84size_t TCPSocket::write(
const void* buf,
size_t count)
89 int ret = send(m_fd, ((
unsigned char*)buf + c), (count - c), MSG_NOSIGNAL);
107size_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."));
126size_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."));
144void TCPSocket::print()
147 memset(&sa, 0,
sizeof(sockaddr_in));
148 socklen_t sa_len =
sizeof(sa);
149 if (getsockname(m_fd, (
struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) {
150 perror(
"getsockname");
152 printf(
"Local IP address is: %s\n", inet_ntoa(sa.sin_addr));
153 printf(
"Local port is: %d\n", (
int) ntohs(sa.sin_port));
156const std::string TCPSocket::getLocalIP()
159 memset(&sa, 0,
sizeof(sockaddr_in));
160 socklen_t sa_len =
sizeof(sa);
161 if (getsockname(m_fd, (
struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) {
164 return inet_ntoa(sa.sin_addr);
167int TCPSocket::getLocalAddress()
170 memset(&sa, 0,
sizeof(sockaddr_in));
171 socklen_t sa_len =
sizeof(sa);
172 if (getsockname(m_fd, (
struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) {
175 return sa.sin_addr.s_addr;
178int TCPSocket::getLocalPort()
181 memset(&sa, 0,
sizeof(sockaddr_in));
182 socklen_t sa_len =
sizeof(sa);
183 if (getsockname(m_fd, (
struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) {
186 return ntohs(sa.sin_port);
189unsigned int TCPSocket::getAddress()
191 struct hostent* host = NULL;
192 host = gethostbyname(m_ip.c_str());
194 unsigned long ip_address = inet_addr(m_ip.c_str());
195 if ((
signed long) ip_address < 0) {
196 throw (std::exception());
199 host = gethostbyaddr((
char*)&ip_address,
sizeof(ip_address), AF_INET);
202 return (*(
unsigned long*) host->h_addr_list[0]);
Abstract base class for different kinds of events.