本文介绍了无法连接到python中的抽象unix套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个用c ++编写的服务器,它创建并绑定到一个名为空间地址为\0hidden的抽象unix套接字。我也有一个用c ++编写的客户端,这个客户端可以成功连接到我的服务器。 BTW,我没有这个客户端的源代码。现在我试图连接到我的服务器使用客户端我写在python没有成功。我不明白为什么我的python客户端不工作。我发布了我的服务器和客户端代码的相关部分。 服务器 #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,开始侦听); 和我的客户代码 #! / opt / python / bin / python import os import socket import sys #创建UDS套接字 sock = socket.socket(socket.AF_UNIX,socket .SOCK_STREAM) server_address =\0hidden print>> sys.stderr,'连接到%s'%server_address.decode(utf-8) try: sock.connect(server_address)除了socket.error,msg: print>> sys.stderr,msg sys.exit(1) 运行客户端后,我得到以下错误输出: 连接到隐藏 [Errno 111]连接被拒绝 对于一些额外的信息,我发布我的工作的c ++客户端和非工作的python客户端的strace输出的相关部分: 工作c ++客户端: socket(PF_FILE,SOCK_STREAM,0)= 3 connect(3,{sa_family = AF_FILE,path = @ $ mmap2(NULL,4096,0)= 0 mmap2(1,{st_mode = S_IFCHR | 0620,st_rdev = makedev(136,0),...} PROT_READ | PROT_WRITE,MAP_PRIVATE | MAP_ANONYMOUS,-1,0)= 0xb77d7000 write(1,发送消息是:00014 www.googl...,38)= 38 write 00014 www.google.com,20)= 20 recv(3,014 Search Engines,99,0)= 18 write(1,014 Search Engines\\\,19 )= 19 close(3)= 0 exit_group(0)=? 无法使用python客户端: socket(PF_FILE,SOCK_STREAM,0)= 3 connect(3,{sa_family = AF_FILE,path = @hidden...},9)= -1 ECONNREFUSED拒绝) write(2,Traceback(most recent call last...,35)= 35 write(2,File \./ uds.py\,line 13 ,in 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 \\\import...,4096)= 839 write(2,,4)= 4 write(2,sock.connect('\\0hidden' )\\\,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 {st_mode = S_IFREG | 0755,st_size = 20234,...})= 0 mmap2(NULL,4096,PROT_READ | PROT_WRITE,MAP_PRIVATE | MAP_ANONYMOUS,-1,0)= 0xb7792000 read ,#Wrapper module for _socket,pr...,4096)= 4096 read(4,oo long.\\\\ errorTab [10064] =...,4096)= 4096 write(2,,4)= 4 write(2,return getattr(self._sock,name)(...,39)= 39 close )= 0 munmap(0xb7792000,4096)= 0 write(2,socket,6)= 6 write(2,。,1)= 1 写入(2,错误,5)= 5 写入(2,:,2)= 2 写入(2,[Errno 111] Connection refused,30)= 30 write(2,\\\,1)= 1 rt_sigaction(SIGINT,{SIG_DFL,[],0},{0x810fbe0,[],0},8)= 0 close(3)= 0 exit_group(1)=? 并且当我运行我的c ++客户端时,我从服务器得到这个strace输出: 0,NULL)= 12 futex(0x80646a4,FUTEX_CMP_REQUEUE_PRIVATE,1,2147483647,0x8064688,360)= 10 futex(0x8064688,FUTEX_WAKE_PRIVATE,1)= 1 接受(5, 我运行我的python客户端,没有输出显示strace。所以看起来我试图连接到一个错误的地址,但我的地址定义为\0hidden 你的C ++并不是你想象的那样,这行:解决方案 strncpy(addr.sun_path,UD_SOCKET_PATH,sizeof(addr.sun_path)-1); 将单个空字符'\0'复制到 addr.sun_path 。注意 strncpy()的联机帮助页中的这一行: 如果src的长度小于n,strncpy()将额外的字节写入dest以确保写入总共n个字节。 因此,你的C ++实际上连接到一个抽象域套接字\0。 Python在这里做正确的事,并连接到\0hidden上的抽象域套接字。 I have a server written in c++ which creates and binds to an abstract unix socket with a namespace address of "\0hidden". I also have a client which is written in c++ also and this client can successfully connect to my server. BTW, I do not have the source code of this client. Now I am trying to connect to my server using a client I have written in python with no success. I do not understand why my python client is not working. I am posting the relevant parts of my server and 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 my client code#! /opt/python/bin/pythonimport osimport socketimport sys# Create a UDS socketsock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)server_address = "\0hidden"print >>sys.stderr, 'connecting to %s' % server_address.decode("utf-8")try: sock.connect(server_address)except socket.error, msg: print >>sys.stderr, msg sys.exit(1)After running the client I get the following error output:connecting to hidden[Errno 111] Connection refusedAnd for some extra information I am posting the relevant parts of the strace outputs of my working c++ client and non-working python client:Working c++ client:socket(PF_FILE, SOCK_STREAM, 0) = 3connect(3, {sa_family=AF_FILE, path=@""}, 110) = 0fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb77d7000write(1, "Sent message is: 00014 www.googl"..., 38) = 38write(3, "00014 www.google.com", 20) = 20recv(3, "014 Search Engines", 99, 0) = 18write(1, "014 Search Engines\n", 19) = 19close(3) = 0exit_group(0) = ?None working python client:socket(PF_FILE, SOCK_STREAM, 0) = 3connect(3, {sa_family=AF_FILE, path=@"hidden"...}, 9) = -1 ECONNREFUSED (Connection refused)write(2, "Traceback (most recent call last"..., 35) = 35write(2, " File \"./uds.py\", line 13, in <"..., 40) = 40open("./uds.py", O_RDONLY|O_LARGEFILE) = 4fstat64(4, {st_mode=S_IFREG|0755, st_size=839, ...}) = 0mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7792000read(4, "#! /opt/python/bin/python\nimport"..., 4096) = 839write(2, " ", 4) = 4write(2, "sock.connect('\\0hidden')\n", 25) = 25close(4) = 0munmap(0xb7792000, 4096) = 0write(2, " File \"/opt/python/lib/python2."..., 64) = 64open("/opt/python/lib/python2.7/socket.py", O_RDONLY|O_LARGEFILE) = 4fstat64(4, {st_mode=S_IFREG|0755, st_size=20234, ...}) = 0mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7792000read(4, "# Wrapper module for _socket, pr"..., 4096) = 4096read(4, "oo long.\"\n errorTab[10064] = "..., 4096) = 4096write(2, " ", 4) = 4write(2, "return getattr(self._sock,name)("..., 39) = 39close(4) = 0munmap(0xb7792000, 4096) = 0write(2, "socket", 6) = 6write(2, ".", 1) = 1write(2, "error", 5) = 5write(2, ": ", 2) = 2write(2, "[Errno 111] Connection refused", 30) = 30write(2, "\n", 1) = 1rt_sigaction(SIGINT, {SIG_DFL, [], 0}, {0x810fbe0, [], 0}, 8) = 0close(3) = 0exit_group(1) = ?And also when I run my c++ client, I get this strace output from my server:0, NULL) = 12futex(0x80646a4, FUTEX_CMP_REQUEUE_PRIVATE, 1, 2147483647, 0x8064688, 360) = 10futex(0x8064688, FUTEX_WAKE_PRIVATE, 1) = 1accept(5,But when I run my python client, no output is shown on strace. So it seems like I am trying to connect to a wrong address, but my address is defined as "\0hidden" in both my server and my client. 解决方案 Your C++ doesn't do quite what you think it does. This line:strncpy(addr.sun_path, UD_SOCKET_PATH, sizeof(addr.sun_path)-1);Copies a single null character '\0' into addr.sun_path. Note this line in the manpage for strncpy(): If the length of src is less than n, strncpy() writes additional null bytes to dest to ensure that a total of n bytes are written.As a result your C++ actually connects to an abstract domain socket at "\0". Python does the right thing here and connects to an abstract domain socket at "\0hidden". 这篇关于无法连接到python中的抽象unix套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-01 20:23