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)
ecl_throw_decl(StandardException) :
53 open(host_name, port_number);
59 void SocketClient::close() {
64 bool SocketClient::open(
const std::string &host_name,
const unsigned int& port_number )
ecl_throw_decl(StandardException) {
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();
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();
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();
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;
Embedded control libraries.
#define ecl_throw(exception)
#define ecl_throw_decl(exception)
Used to state when a server/client detects that there is no other connection at the other end...
Used to signify when a server/client detects that the other end has 'hung up'.
#define ecl_assert_throw_decl(exception)
#define ecl_debug_throw(exception)
#define ecl_catch(exception)
Was some error in the last operation..