13 #include <ecl/config/ecl.hpp> 23 #include "../../include/ecl/devices/socket_connection_status.hpp" 24 #include "../../include/ecl/devices/socket_server_pos.hpp" 36 SocketServer::SocketServer(
const unsigned int &port_number)
ecl_throw_decl(StandardException) :
43 }
ecl_catch (
const StandardException &e ) {
48 bool SocketServer::open(
const unsigned int& port_number )
ecl_throw_decl(StandardException) {
50 if ( this->open() ) { this->close(); }
60 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
62 if ( socket_fd == -1 ) {
63 ecl_throw(devices::socket_exception(LOC));
64 error_handler = devices::socket_error();
74 ret = setsockopt( socket_fd, SOL_SOCKET, SO_REUSEADDR, &on,
sizeof(on) );
76 struct sockaddr_in server;
77 server.sin_family = AF_INET;
78 server.sin_addr.s_addr = INADDR_ANY;
79 server.sin_port = htons(port);
80 memset(server.sin_zero,
'\0',
sizeof server.sin_zero);
85 int bind_result = bind(socket_fd, (
struct sockaddr *) &server,
sizeof(server));
86 if ( bind_result == - 1 ) {
89 error_handler = devices::bind_error();
97 int SocketServer::listen() ecl_throw_decl(StandardException) {
102 ::listen(socket_fd,1);
107 struct sockaddr_in client;
108 int client_length =
sizeof(client);
110 client_socket_fd = accept(socket_fd, (
struct sockaddr *) &client, (socklen_t *) &client_length);
111 if (client_socket_fd < 0) {
112 ecl_throw(devices::accept_exception(LOC));
113 error_handler =devices::accept_error();
117 return client_socket_fd;
125 long SocketServer::read(
char *s,
const unsigned long &n)
ecl_debug_throw_decl(StandardException) {
129 int bytes_read = ::recv(client_socket_fd, s, n, 0);
130 if ( bytes_read < 0 ) {
132 error_handler = devices::receive_error();
136 if ( bytes_read == 0 ) {
145 long SocketServer::peek(
char *s,
const unsigned long &n)
ecl_debug_throw_decl(StandardException) {
147 int bytes_read = ::recv(client_socket_fd, s, n, MSG_PEEK);
148 if ( bytes_read < 0 ) {
150 error_handler = devices::receive_error();
157 long SocketServer::remaining() {
159 int result = ioctl(client_socket_fd, FIONREAD, &bytes);
160 if ( result == -1 ) {
162 error_handler = devices::ioctl_error();
174 long SocketServer::write(
const char *s,
unsigned long n)
ecl_debug_throw_decl(StandardException) {
176 int bytes_written = ::send(client_socket_fd,s,n,0|MSG_NOSIGNAL);
178 int bytes_written = ::send(client_socket_fd,s,n,0);
180 if ( bytes_written < 0 ) {
188 error_handler = devices::send_error();
194 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_debug_throw_decl(exception)
#define ecl_debug_throw(exception)
#define ecl_catch(exception)
Was some error in the last operation..