1 #include "daq/slc/system/TCPSocket.h"
3 #include <daq/slc/base/IOException.h>
8 #include <sys/socket.h>
12 #include <netinet/in.h>
18 int TCPSocket::connect(
const std::string& ip,
unsigned short port)
25 int TCPSocket::connect()
31 memset(&addr, 0,
sizeof(sockaddr_in));
32 addr.sin_family = AF_INET;
33 addr.sin_addr.s_addr = INADDR_ANY;
34 addr.sin_port = htons(m_port);
36 if ((m_fd = ::socket(PF_INET, SOCK_STREAM, 0)) < 0) {
39 struct hostent* host = NULL;
40 host = gethostbyname(m_ip.c_str());
42 unsigned long ip_address = inet_addr(m_ip.c_str());
43 if ((
signed long) ip_address < 0) {
46 host = gethostbyaddr((
char*)&ip_address,
sizeof(ip_address), AF_INET);
52 m_ip.c_str(), m_port));
54 addr.sin_addr.s_addr = (*(
unsigned long*) host->h_addr_list[0]);
56 if (::connect(m_fd, (
struct sockaddr*)&addr,
sizeof(addr)) < 0) {
59 m_ip.c_str(), m_port));
65 void TCPSocket::setBufferSize(
int size)
68 if (setsockopt(m_fd, SOL_SOCKET, SO_SNDBUF, &size,
sizeof(size)) != 0) {
69 throw (
IOException(
"failed to SO_SNDBUF: %s\n", strerror(errno)));
71 if (setsockopt(m_fd, SOL_SOCKET, SO_RCVBUF, &size,
sizeof(size)) != 0) {
72 throw (
IOException(
"error on SO_RCVBUF: %s\n", strerror(errno)));
77 size_t TCPSocket::write(
const void* buf,
size_t count)
83 ret = send(m_fd, ((
unsigned char*)buf + c), (count - c), MSG_NOSIGNAL);
101 size_t TCPSocket::read(
void* buf,
size_t count)
107 ret = recv(m_fd, ((
unsigned char*)buf + c), (count - c), 0);
110 case EINTR:
continue;
111 case EAGAIN:
continue;
113 throw (
IOException(
"TCPSocket::read Error while reading."));
121 size_t TCPSocket::read_once(
void* buf,
size_t count)
126 ret = recv(m_fd, buf, count, 0);
129 case EINTR:
continue;
130 case EAGAIN:
continue;
132 throw (
IOException(
"TCPSocket::read_once Error while reading."));
140 void TCPSocket::print()
143 memset(&sa, 0,
sizeof(sockaddr_in));
144 socklen_t sa_len =
sizeof(sa);
145 if (getsockname(m_fd, (
struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) {
146 perror(
"getsockname");
148 printf(
"Local IP address is: %s\n", inet_ntoa(sa.sin_addr));
149 printf(
"Local port is: %d\n", (
int) ntohs(sa.sin_port));
152 const std::string TCPSocket::getLocalIP()
155 memset(&sa, 0,
sizeof(sockaddr_in));
156 socklen_t sa_len =
sizeof(sa);
157 if (getsockname(m_fd, (
struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) {
160 return inet_ntoa(sa.sin_addr);
163 int TCPSocket::getLocalAddress()
166 memset(&sa, 0,
sizeof(sockaddr_in));
167 socklen_t sa_len =
sizeof(sa);
168 if (getsockname(m_fd, (
struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) {
171 return sa.sin_addr.s_addr;
174 int TCPSocket::getLocalPort()
177 memset(&sa, 0,
sizeof(sockaddr_in));
178 socklen_t sa_len =
sizeof(sa);
179 if (getsockname(m_fd, (
struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) {
182 return ntohs(sa.sin_port);
185 unsigned int TCPSocket::getAddress()
187 struct hostent* host = NULL;
188 host = gethostbyname(m_ip.c_str());
190 unsigned long ip_address = inet_addr(m_ip.c_str());
191 if ((
signed long) ip_address < 0) {
192 throw (std::exception());
195 host = gethostbyaddr((
char*)&ip_address,
sizeof(ip_address), AF_INET);
198 return (*(
unsigned long*) host->h_addr_list[0]);