1 /***************************************************************************
2 * Copyright (C) 2006 by Gerd v. Egidy *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Lesser General Public License version *
7 * 2.1 as published by the Free Software Foundation. *
9 * This library is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU Lesser General Public License for more details. *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
24 #include <sys/types.h>
25 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #include <netinet/in.h>
38 #include "socket_client.hxx"
39 #include "t2n_exception.hxx"
46 socket_client_connection::socket_client_connection(const std::string& _server, int _port, int _max_retries)
47 : client_connection(), socket_handler(0,tcp_s)
49 max_retries=_max_retries;
57 socket_client_connection::socket_client_connection(const std::string& _path, int _max_retries)
58 : client_connection(), socket_handler(0,unix_s)
60 max_retries=_max_retries;
67 void socket_client_connection::connect()
69 if (get_type() == unix_s)
71 struct sockaddr_un unix_addr;
73 unix_addr.sun_family = AF_UNIX;
74 strcpy (unix_addr.sun_path, path.c_str());
76 sock = socket(PF_UNIX, SOCK_STREAM, 0);
78 throw t2n_connect_error(string("socket() error: ")+strerror(errno));
80 if (::connect(sock,(struct sockaddr *) &unix_addr, sizeof(unix_addr)))
81 throw t2n_connect_error(string("connect() error: ")+strerror(errno));
83 else if (get_type() == tcp_s)
85 struct sockaddr_in sock_addr;
87 sock_addr.sin_family = AF_INET;
88 sock_addr.sin_port = htons(port);
91 if (inet_aton(server.c_str(),&sock_addr.sin_addr)==0)
93 struct hostent *server_hent;
94 server_hent=gethostbyname(server.c_str());
95 if (server_hent == NULL)
96 throw t2n_connect_error(string("can't find server ")+server);
98 memcpy(&sock_addr.sin_addr,server_hent->h_addr_list[0],sizeof(sock_addr.sin_addr));
101 sock = socket(PF_INET, SOCK_STREAM, 0);
103 throw t2n_connect_error(string("socket() error: ")+strerror(errno));
105 if (::connect(sock,(struct sockaddr *) &sock_addr, sizeof(sock_addr)))
106 throw t2n_connect_error(string("connect() error: ")+strerror(errno));
109 throw t2n_connect_error(string("invalid connection type"));
111 set_socket_options(sock);
113 do_callbacks(new_connection);
116 void socket_client_connection::close()
118 if (!client_connection::is_closed())
120 socket_handler::close();
121 client_connection::close();