/*************************************************************************** * Copyright (C) 2006 by Gerd v. Egidy * * gve@intra2net.com * * * * This library is free software; you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License version * * 2.1 as published by the Free Software Foundation. * * * * This library is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Lesser General Public License for more details. * * * * You should have received a copy of the GNU Lesser General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "socket_client.hxx" #include "t2n_exception.hxx" using namespace std; namespace libt2n { socket_client_connection::socket_client_connection(const std::string& _server, int _port, int _max_retries) : client_connection(), socket_handler(0,tcp_s) { max_retries=_max_retries; server=_server; port=_port; connect(); } socket_client_connection::socket_client_connection(const std::string& _path, int _max_retries) : client_connection(), socket_handler(0,unix_s) { max_retries=_max_retries; path=_path; connect(); } void socket_client_connection::connect() { if (get_type() == unix_s) { struct sockaddr_un unix_addr; unix_addr.sun_family = AF_UNIX; strcpy (unix_addr.sun_path, path.c_str()); sock = socket(PF_UNIX, SOCK_STREAM, 0); if (!sock) throw t2n_connect_error(string("socket() error: ")+strerror(errno)); if (::connect(sock,(struct sockaddr *) &unix_addr, sizeof(unix_addr))) throw t2n_connect_error(string("connect() error: ")+strerror(errno)); } else if (get_type() == tcp_s) { struct sockaddr_in sock_addr; sock_addr.sin_family = AF_INET; sock_addr.sin_port = htons(port); // find the target ip if (inet_aton(server.c_str(),&sock_addr.sin_addr)==0) { struct hostent *server_hent; server_hent=gethostbyname(server.c_str()); if (server_hent == NULL) throw t2n_connect_error(string("can't find server ")+server); memcpy(&sock_addr.sin_addr,server_hent->h_addr_list[0],sizeof(sock_addr.sin_addr)); } sock = socket(PF_INET, SOCK_STREAM, 0); if (!sock) throw t2n_connect_error(string("socket() error: ")+strerror(errno)); if (::connect(sock,(struct sockaddr *) &sock_addr, sizeof(sock_addr))) throw t2n_connect_error(string("connect() error: ")+strerror(errno)); } else throw t2n_connect_error(string("invalid connection type")); set_socket_options(sock); do_callbacks(new_connection); } void socket_client_connection::close() { if (!client_connection::is_closed()) { socket_handler::close(); client_connection::close(); } } }