13 #include <ecl/config/ecl.hpp>
25 #include "../../include/ecl/devices/detail/socket_error_handler_pos.hpp"
26 #include "../../include/ecl/devices/detail/socket_exception_handler_pos.hpp"
27 #include "../../include/ecl/devices/socket_connection_status.hpp"
28 #include "../../include/ecl/devices/socket_client_pos.hpp"
46 SocketClient::SocketClient(
const std::string &host_name,
const unsigned int &port_number) :
53 open(host_name, port_number);
59 void SocketClient::close() {
64 bool SocketClient::open(
const std::string &host_name,
const unsigned int& port_number ) {
66 if ( this->open() ) { this->close(); }
78 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
80 if ( socket_fd == -1 ) {
81 ecl_throw(devices::socket_exception(LOC));
82 error_handler = devices::socket_error();
88 struct hostent *host_entry;
98 host_entry = gethostbyname(hostname.c_str());
99 if ( host_entry == NULL ) {
101 ecl_throw(devices::gethostbyname_exception(LOC,hostname));
102 error_handler = devices::gethostbyname_error();
109 struct sockaddr_in destination;
110 destination.sin_family = AF_INET;
111 destination.sin_addr = *((
struct in_addr *)host_entry->h_addr);
112 destination.sin_port = htons(port);
113 memset(destination.sin_zero,
'\0',
sizeof destination.sin_zero);
118 int connect_result = connect( socket_fd, (
struct sockaddr *) &destination,
sizeof(destination) );
119 if ( connect_result == -1 ) {
122 ecl_throw(devices::connection_exception(LOC));
123 error_handler = devices::connection_error();
135 long SocketClient::read(
char &c) {
139 long SocketClient::read(
char *s,
const unsigned long &n) {
152 int bytes_read = ::recv(socket_fd, s, n, 0);
157 if ( bytes_read == 0 ) {
162 if ( bytes_read < 0 ) {
163 if ( errno == ECONNRESET ) {
168 error_handler = devices::receive_error();
176 long SocketClient::peek(
char *s,
const unsigned long &n) {
180 int bytes_read = ::recv(socket_fd, s, n, MSG_PEEK);
181 if ( bytes_read < 0 ) {
183 error_handler = devices::receive_error();
190 long SocketClient::remaining() {
195 int result = ioctl(socket_fd, FIONREAD, &bytes);
196 if ( result == -1 ) {
198 error_handler = devices::ioctl_error();
208 long SocketClient::write(
const char &c) {
213 long SocketClient::write(
const char *s,
unsigned long n) {
220 int bytes_written = ::send(socket_fd,s,n,MSG_NOSIGNAL);
222 if ( bytes_written < 0 ) {
230 error_handler = devices::send_error();
235 return bytes_written;