36 #include <sys/socket.h> 37 #include <sys/types.h> 52 string err(
"Socket is already listening" ) ;
58 string err(
"Socket is already connected" ) ;
62 struct sockaddr_un client_addr ;
63 struct sockaddr_un server_addr ;
66 unsigned int max_len =
sizeof( client_addr.sun_path ) ;
69 getcwd( path,
sizeof( path ) ) ;
73 _tempSocket +=
".unixSocket" ;
77 if( _tempSocket.length() > max_len - 1 )
79 string msg =
"path to temporary unix socket " ;
80 msg += _tempSocket +
" is too long" ;
83 if( _unixSocket.length() > max_len - 1 )
85 string msg =
"path to unix socket " ;
86 msg += _unixSocket +
" is too long" ;
90 strncpy(server_addr.sun_path, _unixSocket.c_str(), _unixSocket.size());
91 server_addr.sun_path[_unixSocket.size()] =
'\0';
92 server_addr.sun_family = AF_UNIX ;
94 int descript = socket( AF_UNIX, SOCK_STREAM, 0 ) ;
97 strncpy( client_addr.sun_path, _tempSocket.c_str(), _tempSocket.size());
98 client_addr.sun_path[_tempSocket.size()] =
'\0';
99 client_addr.sun_family = AF_UNIX ;
101 int clen =
sizeof( client_addr.sun_family ) ;
102 clen += strlen( client_addr.sun_path ) + 1;
104 if( bind( descript, (
struct sockaddr*)&client_addr, clen + 1) != -1 )
106 int slen =
sizeof( server_addr.sun_family ) ;
107 slen += strlen( server_addr.sun_path) + 1;
112 if( ::
connect( descript, (
struct sockaddr*)&server_addr, slen ) != -1)
120 string msg =
"could not connect via " ;
122 char *err = strerror( errno ) ;
124 msg = msg +
"\n" + err ;
126 msg = msg +
"\nCould not retrieve error message" ;
132 string msg =
"could not bind to Unix socket " ;
134 char *err = strerror( errno ) ;
136 msg = msg +
"\n" + err ;
138 msg = msg +
"\nCould not retrieve error message" ;
144 string msg =
"could not create a Unix socket" ;
145 char *err = strerror( errno ) ;
147 msg = msg +
"\n" + err ;
149 msg = msg +
"\nCould not retrieve error message" ;
159 string err(
"Socket is already connected" ) ;
165 string err(
"Socket is already listening" ) ;
170 static struct sockaddr_un server_add ;
171 _socket = socket( AF_UNIX,SOCK_STREAM, 0 ) ;
174 server_add.sun_family = AF_UNIX;
177 strncpy( server_add.sun_path, _unixSocket.c_str(), 103) ;
178 server_add.sun_path[103] =
'\0';
180 (void)unlink( _unixSocket.c_str() ) ;
181 if( setsockopt( _socket, SOL_SOCKET, SO_REUSEADDR,
182 (
char*)&on,
sizeof( on ) ) )
184 string error(
"could not set SO_REUSEADDR on Unix socket" ) ;
185 const char *error_info = strerror( errno ) ;
187 error +=
" " + (string)error_info ;
195 if( bind( _socket, (
struct sockaddr*)&server_add,
sizeof( server_add.sun_family ) + strlen( server_add.sun_path ) + 1) != -1)
197 if( ::
listen( _socket, 5 ) == 0 )
203 string error(
"could not listen Unix socket" ) ;
204 const char* error_info = strerror( errno ) ;
206 error +=
" " + (string)error_info ;
212 string error(
"could not bind Unix socket" ) ;
213 const char* error_info = strerror( errno ) ;
215 error +=
" " + (string)error_info ;
221 string error(
"could not get Unix socket" ) ;
222 const char *error_info = strerror( errno ) ;
224 error +=
" " + (string)error_info ;
233 if( _tempSocket !=
"" )
235 if( !access( _tempSocket.c_str(), F_OK ) )
237 (void)
remove( _tempSocket.c_str() ) ;
243 if( !access( _unixSocket.c_str(), F_OK ) )
245 (void)
remove( _unixSocket.c_str() ) ;
270 << (
void *)
this <<
")" << endl ;
exception thrown if inernal error encountered
static ostream & LMarg(ostream &strm)
virtual bool allowConnection()
is there any wrapper code for unix sockets
virtual void dump(ostream &strm) const
dumps information about this object
static string create_temp_name()
Create a uniq name which is used to create a unique name for a Unix socket in a client ...
virtual void dump(ostream &strm) const
dumps information about this object