42 #include <sys/socket.h> 74 "stream_client_socket::rcv() - Socket has already been shut down!",
80 "stream_client_socket::rcv() - Socket is not connected!",
false);
82 if (buf == NULL || len == 0)
85 "stream_client_socket::rcv() - Buffer or length is null!",
false);
89 if (-1 == (recvd = BERKELEY::recv(
sfd, buf, len, flags))) {
90 if (is_nonblocking && errno == EWOULDBLOCK)
95 "stream_client_socket::rcv() - Error while reading!");
123 "stream_client_socket::operator>>(std::string) " 124 "- Socket has already been shut down!",
127 using std::unique_ptr;
128 unique_ptr<char[]> buffer(
new char[dest.size()]);
130 memset(buffer.get(), 0, dest.size());
132 if (sock.
sfd == -1) {
134 ">>(std::string) input: Socket not connected!",
138 if (-1 == (read_bytes = read(sock.
sfd, buffer.get(), dest.size()))) {
139 if (sock.is_nonblocking && errno == EWOULDBLOCK) {
145 ">>(std::string) input: Error while reading!");
148 if (read_bytes < static_cast<ssize_t>(dest.size()))
149 dest.resize(read_bytes);
153 dest.assign(buffer.get(), read_bytes);
177 "stream_client_socket::operator<<(const char*) " 178 "- Socket has already been shut down!",
182 "<<(const char*) output: Socket not connected!",
186 "<<(const char*) output: Null buffer given!",
189 size_t len = strlen(str);
191 if (-1 == write(sock.
sfd, str, len))
193 "<<(const char*) output: Write failed!");
216 "stream_client_socket::operator<<(std::string) " 217 "- Socket has already been shut down!",
221 "<<(std::string) output: Socket not connected!",
224 if (-1 == write(sock.
sfd, str.c_str(), str.size()))
226 "<<(std::string) output: Write failed!");
249 "stream_client_socket::snd() - Socket has already been shut down!",
254 "stream_client_socket::snd() - Socket not connected!",
false);
255 if (buf == NULL || len == 0)
258 "stream_client_socket::snd() - Buffer or length is null!",
false);
260 if (-1 == (snd_bytes = BERKELEY::send(
sfd, buf, len, flags))) {
261 if (is_nonblocking && errno == EWOULDBLOCK)
266 "stream_client_socket::snd() - Error while sending");
283 if ((method & (LIBSOCKET_READ | LIBSOCKET_WRITE)) && (
shut_rd ==
true) &&
286 if ((method & LIBSOCKET_READ) && (
shut_rd ==
true))
return;
287 if ((method & LIBSOCKET_WRITE) && (
shut_wr ==
true))
return;
289 #if LIBSOCKET_LINUX || BD_ANDROID 290 using BERKELEY::SHUT_RD;
291 using BERKELEY::SHUT_RDWR;
292 using BERKELEY::SHUT_WR;
295 if (method == (LIBSOCKET_READ | LIBSOCKET_WRITE))
296 u_method = SHUT_RDWR;
297 else if (method == LIBSOCKET_READ)
299 else if (method == LIBSOCKET_WRITE)
305 BERKELEY::shutdown(
sfd, u_method))
310 "stream_client_socket::shutdown() - Could not shutdown socket");
313 if (method & LIBSOCKET_READ)
shut_rd =
true;
314 if (method & LIBSOCKET_WRITE)
shut_wr =
true;
Contains libsocket elements.
This class is instantiated and thrown when an error occurs. If there's an error somewhere in libsocke...
ssize_t rcv(void *buf, size_t len, int flags=0)
Receive data from socket.
dgram_client_socket & operator>>(dgram_client_socket &sock, string &dest)
Receive data from connected datagram socket.
stream_client_socket()
Void constructor.
dgram_client_socket & operator<<(dgram_client_socket &sock, const char *str)
Send data to connected peer.
ssize_t snd(const void *buf, size_t len, int flags=0)
Send data to socket.
int sfd
sfd is the sockets API file descriptor
Generic socket for active stream sockets (TCP client sockets, UNIX Stream clients)
void shutdown(int method=LIBSOCKET_WRITE)
Shut a socket down.
Contains all functions available in the C libsocket.