libsocket
dgramclient.cpp
Go to the documentation of this file.
1 #include <string.h>
2 #include <unistd.h>
3 #include <string>
4 
5 /*
6  The committers of the libsocket project, all rights reserved
7  (c) 2012, dermesser <lbo@spheniscida.de>
8 
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions are met:
11 
12  1. Redistributions of source code must retain the above copyright notice,
13  this list of conditions and the following disclaimer.
14  2. Redistributions in binary form must reproduce the above copyright notice,
15  this list of conditions and the following disclaimer in the documentation
16  and/or other materials provided with the distribution.
17 
18  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS “AS IS” AND ANY
19  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 */
30 
51 #include <dgramclient.hpp>
52 #include <exception.hpp>
53 
54 namespace libsocket {
55 using std::string;
56 
57 dgram_client_socket::dgram_client_socket(void) : connected(false) {}
58 
73 ssize_t dgram_client_socket::rcv(void* buf, size_t len, int flags) {
74  ssize_t bytes;
75 
76  memset(buf, 0, len);
77 
78  if (-1 == (bytes = recv(sfd, buf, len, flags)))
79  throw socket_exception(__FILE__, __LINE__,
80  "dgram_client_socket::rcv() - recv() failed!");
81 
82  return bytes;
83 }
84 
96  ssize_t read_bytes;
97 
98  char* buffer = new char[dest.size()];
99 
100  memset(buffer, 0, dest.size());
101 
102  if (-1 == (read_bytes = read(sock.sfd, buffer, dest.size()))) {
103  delete[] buffer;
104  if (sock.is_nonblocking && errno == EWOULDBLOCK) {
105  dest.clear();
106  return sock;
107  } else
108  throw socket_exception(__FILE__, __LINE__,
109  ">>(dgram_client_socket, std::string) "
110  "input: Error while reading!");
111  }
112 
113  if (read_bytes < static_cast<ssize_t>(dest.size()))
114  dest.resize(read_bytes); // So the client doesn't print content more
115  // than one time
116  // and it can check if the string's length is 0 (end of transmission)
117 
118  dest.assign(buffer, read_bytes);
119 
120  delete[] buffer;
121 
122  return sock;
123 }
124 
135 ssize_t dgram_client_socket::snd(const void* buf, size_t len, int flags) {
136  ssize_t bytes;
137 
138  if (connected != true)
139  throw socket_exception(
140  __FILE__, __LINE__,
141  "dgram_client_socket::snd() - Socket is not connected!", false);
142 
143  if (-1 == (bytes = send(sfd, buf, len, flags)))
144  throw socket_exception(__FILE__, __LINE__,
145  "dgram_client_socket::snd() - send() failed!");
146 
147  return bytes;
148 }
149 
156  if (str == NULL)
157  throw socket_exception(
158  __FILE__, __LINE__,
159  "dgram_client_socket <<(const char*) output: Null buffer given!");
160  if (sock.connected == false)
161  throw socket_exception(__FILE__, __LINE__,
162  "dgram_client_socket <<(const char*) output: "
163  "DGRAM socket not connected!");
164 
165  size_t len = strlen(str);
166 
167  if (-1 == write(sock.sfd, str, len))
168  throw socket_exception(
169  __FILE__, __LINE__,
170  "dgram_client_socket <<(const char*) output: Write failed!");
171 
172  return sock;
173 }
174 
181  if (sock.connected == false)
182  throw socket_exception(__FILE__, __LINE__,
183  "dgram_client_socket <<(std::string) output: "
184  "DGRAM socket not connected!");
185  if (-1 == write(sock.sfd, str.c_str(), str.size()))
186  throw socket_exception(
187  __FILE__, __LINE__,
188  "dgram_client_socket <<(std::string) output: Write failed!");
189 
190  return sock;
191 }
192 
198 bool dgram_client_socket::getconn(void) const { return is_connected(); }
199 
203 bool dgram_client_socket::is_connected(void) const { return connected; }
204 } // namespace libsocket
205 
Contains libsocket elements.
Definition: dgramclient.hpp:41
Represents a generic "client" datagram socket, i.e. a datagram socket which can be connected.
Definition: dgramclient.hpp:53
This class is instantiated and thrown when an error occurs. If there's an error somewhere in libsocke...
Definition: exception.hpp:52
dgram_client_socket & operator>>(dgram_client_socket &sock, string &dest)
Receive data from connected datagram socket.
Definition: dgramclient.cpp:95
dgram_client_socket & operator<<(dgram_client_socket &sock, const string &str)
Send data to connected peer.
int sfd
sfd is the sockets API file descriptor
Definition: socket.hpp:74