Belle II Software development
TCPSocket Class Reference
Inheritance diagram for TCPSocket:
FileDescriptor

Public Member Functions

 TCPSocket (const std::string &ip, unsigned short port)
 
void setIP (const std::string &ip)
 
void setPort (unsigned short port)
 
const std::string & getIP () const
 
unsigned short getPort () const
 
int connect ()
 
int connect (const std::string &ip, unsigned short port)
 
void setBufferSize (int size)
 
void print ()
 
const std::string getLocalIP ()
 
int getLocalAddress ()
 
int getLocalPort ()
 
unsigned int getAddress ()
 
virtual size_t write (const void *v, size_t count)
 
virtual size_t read (void *v, size_t count)
 
size_t read_once (void *v, size_t count)
 
int get_fd () const
 
bool select (int sec=-1, int usec=-1)
 
bool select2 (int sec=-1, int usec=-1)
 
bool close ()
 

Protected Attributes

int m_fd
 

Private Member Functions

 TCPSocket (int fd)
 

Private Attributes

std::string m_ip
 
unsigned short m_port
 

Friends

class TCPServerSocket
 

Detailed Description

Definition at line 21 of file TCPSocket.h.

Constructor & Destructor Documentation

◆ TCPSocket() [1/3]

TCPSocket ( )
inline

Definition at line 26 of file TCPSocket.h.

26: m_ip(""), m_port(0) {}

◆ TCPSocket() [2/3]

TCPSocket ( const std::string &  ip,
unsigned short  port 
)
inline

Definition at line 27 of file TCPSocket.h.

28 : m_ip(ip), m_port(port) {}

◆ ~TCPSocket()

virtual ~TCPSocket ( )
inlinevirtual

Definition at line 29 of file TCPSocket.h.

29{}

◆ TCPSocket() [3/3]

TCPSocket ( int  fd)
inlineprivate

Definition at line 32 of file TCPSocket.h.

32: FileDescriptor(fd), m_ip(""), m_port(0) {}

Member Function Documentation

◆ close()

bool close ( )
inherited

Definition at line 90 of file FileDescriptor.cc.

91{
92 if (m_fd > 0) {
93 if (::close(m_fd) != 0) {
94 return false;
95 }
96 }
97 m_fd = -1;
98 return true;
99}

◆ connect() [1/2]

int connect ( )

Definition at line 32 of file TCPSocket.cc.

33{
34 if (m_fd > 0) {
35 throw (IOException("Socket is working already."));
36 }
37 sockaddr_in addr;
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);
42
43 if ((m_fd = ::socket(PF_INET, SOCK_STREAM, 0)) < 0) {
44 throw (IOException("Failed to create socket"));
45 }
46 struct hostent* host = NULL;
47 host = gethostbyname(m_ip.c_str());
48 if (host == NULL) {
49 unsigned long ip_address = inet_addr(m_ip.c_str());
50 if ((signed long) ip_address < 0) {
51 throw (IOException("Wrong host name or ip"));
52 } else {
53 host = gethostbyaddr((char*)&ip_address, sizeof(ip_address), AF_INET);
54 }
55 }
56 if (host == NULL) {
57 close();
58 throw (IOException("Failed to connect host %s:%d",
59 m_ip.c_str(), m_port));
60 }
61 addr.sin_addr.s_addr = (*(unsigned long*) host->h_addr_list[0]);
62
63 if (::connect(m_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
64 close();
65 throw (IOException("Failed to connect host %s:%d",
66 m_ip.c_str(), m_port));
67 }
68
69 return m_fd;
70}

◆ connect() [2/2]

int connect ( const std::string &  ip,
unsigned short  port 
)

Definition at line 25 of file TCPSocket.cc.

26{
27 m_ip = ip;
28 m_port = port;
29 return connect();
30}

◆ get_fd()

int get_fd ( ) const
inherited

Definition at line 33 of file FileDescriptor.cc.

34{
35 return m_fd;
36}

◆ getAddress()

unsigned int getAddress ( )

Definition at line 189 of file TCPSocket.cc.

190{
191 struct hostent* host = NULL;
192 host = gethostbyname(m_ip.c_str());
193 if (host == NULL) {
194 unsigned long ip_address = inet_addr(m_ip.c_str());
195 if ((signed long) ip_address < 0) {
196 throw (std::exception());
197 // throw (IOException("Wrong host name or ip")); // TODO which throw is the correct one?
198 } else {
199 host = gethostbyaddr((char*)&ip_address, sizeof(ip_address), AF_INET);
200 }
201 }
202 return (*(unsigned long*) host->h_addr_list[0]);
203}

◆ getIP()

const std::string & getIP ( ) const
inline

Definition at line 37 of file TCPSocket.h.

37{ return m_ip; }

◆ getLocalAddress()

int getLocalAddress ( )

Definition at line 167 of file TCPSocket.cc.

168{
169 sockaddr_in sa;
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) {
173 return 0;
174 }
175 return sa.sin_addr.s_addr;
176}

◆ getLocalIP()

const std::string getLocalIP ( )

Definition at line 156 of file TCPSocket.cc.

157{
158 sockaddr_in sa;
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) {
162 return "";
163 }
164 return inet_ntoa(sa.sin_addr);
165}

◆ getLocalPort()

int getLocalPort ( )

Definition at line 178 of file TCPSocket.cc.

179{
180 sockaddr_in sa;
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) {
184 return 0;
185 }
186 return ntohs(sa.sin_port);
187}

◆ getPort()

unsigned short getPort ( ) const
inline

Definition at line 38 of file TCPSocket.h.

38{ return m_port; }

◆ print()

void print ( )

Definition at line 144 of file TCPSocket.cc.

145{
146 sockaddr_in sa;
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");
151 }
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));
154}

◆ read()

size_t read ( void *  v,
size_t  count 
)
virtual

Definition at line 107 of file TCPSocket.cc.

108{
109 size_t c = 0;
110 while (c < count) {
111 errno = 0;
112 int ret = recv(m_fd, ((unsigned char*)buf + c), (count - c), 0);
113 if (ret <= 0) {
114 switch (errno) {
115 case EINTR: continue;
116 case EAGAIN: continue;
117 default:
118 throw (IOException("TCPSocket::read Error while reading."));
119 }
120 }
121 c += ret;
122 }
123 return c;
124}

◆ read_once()

size_t read_once ( void *  v,
size_t  count 
)

Definition at line 126 of file TCPSocket.cc.

127{
128 errno = 0;
129 while (true) {
130 int ret = recv(m_fd, buf, count, 0);
131 if (ret <= 0) {
132 switch (errno) {
133 case EINTR: continue;
134 case EAGAIN: continue;
135 default:
136 throw (IOException("TCPSocket::read_once Error while reading."));
137 }
138 }
139 return ret;
140 }
141 return 0;
142}

◆ select()

bool select ( int  sec = -1,
int  usec = -1 
)
inherited

Definition at line 38 of file FileDescriptor.cc.

39{
40 if (m_fd <= 0) {
41 return false;
42 }
43 fd_set fds;
44 FD_ZERO(&fds);
45 FD_SET(m_fd, &fds);
46 int ret;
47 if (sec >= 0 && usec >= 0) {
48 timeval t = {sec, usec};
49 ret = ::select(FD_SETSIZE, &fds, NULL, NULL, &t);
50 } else {
51 ret = ::select(FD_SETSIZE, &fds, NULL, NULL, NULL);
52 }
53 if (ret < 0) {
54 perror("select");
55 throw (IOException("Failed to select"));
56 }
57 if (FD_ISSET(m_fd, &fds)) {
58 return true;
59 } else {
60 return false;
61 }
62}

◆ select2()

bool select2 ( int  sec = -1,
int  usec = -1 
)
inherited

Definition at line 64 of file FileDescriptor.cc.

65{
66 if (m_fd <= 0) {
67 return false;
68 }
69 fd_set fds;
70 FD_ZERO(&fds);
71 FD_SET(m_fd, &fds);
72 int ret;
73 if (sec >= 0 && usec >= 0) {
74 timeval t = {sec, usec};
75 ret = ::select(FD_SETSIZE, NULL, &fds, NULL, &t);
76 } else {
77 ret = ::select(FD_SETSIZE, NULL, &fds, NULL, NULL);
78 }
79 if (ret < 0) {
80 perror("select");
81 throw (IOException("Failed to select"));
82 }
83 if (FD_ISSET(m_fd, &fds)) {
84 return true;
85 } else {
86 return false;
87 }
88}

◆ setBufferSize()

void setBufferSize ( int  size)

Definition at line 72 of file TCPSocket.cc.

73{
74 if (size > 0) {
75 if (setsockopt(m_fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)) != 0) {
76 throw (IOException("failed to SO_SNDBUF: %s\n", strerror(errno)));
77 }
78 if (setsockopt(m_fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)) != 0) {
79 throw (IOException("error on SO_RCVBUF: %s\n", strerror(errno)));
80 }
81 }
82}

◆ setIP()

void setIP ( const std::string &  ip)
inline

Definition at line 35 of file TCPSocket.h.

35{ m_ip = ip; }

◆ setPort()

void setPort ( unsigned short  port)
inline

Definition at line 36 of file TCPSocket.h.

36{ m_port = port; }

◆ write()

size_t write ( const void *  v,
size_t  count 
)
virtual

Definition at line 84 of file TCPSocket.cc.

85{
86 size_t c = 0;
87 while (c < count) {
88 errno = 0;
89 int ret = send(m_fd, ((unsigned char*)buf + c), (count - c), MSG_NOSIGNAL);
90 if (ret <= 0) {
91 switch (errno) {
92 case EINTR: continue;
93 case ENETUNREACH:
94 case EHOSTUNREACH:
95 case ETIMEDOUT:
96 usleep(500);
97 continue;
98 default:
99 throw (IOException("Error while writing"));
100 }
101 }
102 c += ret;
103 }
104 return c;
105}

Friends And Related Function Documentation

◆ TCPServerSocket

friend class TCPServerSocket
friend

Definition at line 23 of file TCPSocket.h.

Member Data Documentation

◆ m_fd

int m_fd
protectedinherited

Definition at line 31 of file FileDescriptor.h.

◆ m_ip

std::string m_ip
private

Definition at line 54 of file TCPSocket.h.

◆ m_port

unsigned short m_port
private

Definition at line 55 of file TCPSocket.h.


The documentation for this class was generated from the following files: