c++ - Can not connect to an abstract unix socket in python -


i have server written in c++ creates , binds abstract unix socket namespace address of "\0hidden". have client written in c++ , client can successfully connect server. btw, not have source code of client. trying connect server using client have written in python no success. not understand why python client not working. posting relevant parts of server , client codes.

server

#define ud_socket_path          "\0hidden" struct sockaddr_un addr; int fd,cl;  if ( (fd = socket(af_unix, sock_stream, 0)) == -1)  {     syslog(log_crit, "error creating socket!");     exit(1); }  memset(&addr, 0, sizeof(addr)); addr.sun_family = af_unix; strncpy(addr.sun_path, ud_socket_path, sizeof(addr.sun_path)-1); unlink(ud_socket_path);   if (::bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1)  {     syslog(log_crit, "bind error");     exit(1); }  if (listen(fd, max_conn_pending) == -1)  {     syslog(log_crit, "listen error");     exit(1); }  syslog(log_info, "start listening."); 

and client code

#! /opt/python/bin/python import os import socket import sys # create uds socket sock = socket.socket(socket.af_unix, socket.sock_stream)  server_address = "\0hidden" print >>sys.stderr, 'connecting %s' % server_address.decode("utf-8") try:     sock.connect(server_address) except socket.error, msg:     print >>sys.stderr, msg     sys.exit(1) 

after running client following error output:

connecting hidden [errno 111] connection refused 

and information posting relevant parts of strace outputs of working c++ client , non-working python client:

working c++ client:

socket(pf_file, sock_stream, 0)         = 3 connect(3, {sa_family=af_file, path=@""}, 110) = 0 fstat64(1, {st_mode=s_ifchr|0620, st_rdev=makedev(136, 0), ...}) = 0 mmap2(null, 4096, prot_read|prot_write, map_private|map_anonymous, -1, 0) = 0xb77d7000 write(1, "sent message is: 00014 www.googl"..., 38) = 38 write(3, "00014 www.google.com", 20)    = 20 recv(3, "014 search engines", 99, 0)    = 18 write(1, "014 search engines\n", 19)    = 19 close(3)                                = 0 exit_group(0)                           = ? 

none working python client:

socket(pf_file, sock_stream, 0)         = 3 connect(3, {sa_family=af_file, path=@"hidden"...}, 9) = -1 econnrefused (connection refused) write(2, "traceback (most recent call last"..., 35) = 35 write(2, "  file \"./uds.py\", line 13, in <"..., 40) = 40 open("./uds.py", o_rdonly|o_largefile)  = 4 fstat64(4, {st_mode=s_ifreg|0755, st_size=839, ...}) = 0 mmap2(null, 4096, prot_read|prot_write, map_private|map_anonymous, -1, 0) = 0xb7792000 read(4, "#! /opt/python/bin/python\nimport"..., 4096) = 839 write(2, "    ", 4)                     = 4 write(2, "sock.connect('\\0hidden')\n", 25) = 25 close(4)                                = 0 munmap(0xb7792000, 4096)                = 0 write(2, "  file \"/opt/python/lib/python2."..., 64) = 64 open("/opt/python/lib/python2.7/socket.py", o_rdonly|o_largefile) = 4 fstat64(4, {st_mode=s_ifreg|0755, st_size=20234, ...}) = 0 mmap2(null, 4096, prot_read|prot_write, map_private|map_anonymous, -1, 0) = 0xb7792000 read(4, "# wrapper module _socket, pr"..., 4096) = 4096 read(4, "oo long.\"\n    errortab[10064] = "..., 4096) = 4096 write(2, "    ", 4)                     = 4 write(2, "return getattr(self._sock,name)("..., 39) = 39 close(4)                                = 0 munmap(0xb7792000, 4096)                = 0 write(2, "socket", 6)                   = 6 write(2, ".", 1)                        = 1 write(2, "error", 5)                    = 5 write(2, ": ", 2)                       = 2 write(2, "[errno 111] connection refused", 30) = 30 write(2, "\n", 1)                       = 1 rt_sigaction(sigint, {sig_dfl, [], 0}, {0x810fbe0, [], 0}, 8) = 0 close(3)                                = 0 exit_group(1)                           = ? 

and when run c++ client, strace output server:

0, null)                      = 12 futex(0x80646a4, futex_cmp_requeue_private, 1, 2147483647, 0x8064688, 360) = 10 futex(0x8064688, futex_wake_private, 1) = 1 accept(5,  

but when run python client, no output shown on strace. seems trying connect wrong address, address defined "\0hidden" in both server , client.

your c++ doesn't quite think does. line:

strncpy(addr.sun_path, ud_socket_path, sizeof(addr.sun_path)-1); 

copies single null character '\0' addr.sun_path. note line in manpage strncpy():

if length of src less n, strncpy() writes additional null bytes dest ensure total of n bytes written.

as result c++ connects abstract domain socket @ "\0". python right thing here , connects abstract domain socket @ "\0hidden".


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -