我是两天前编写的第一个程序的python初学者。我在 AF_UNIX 的 python 客户端和 C 服务器中遇到连接问题。我有带有 AF_LOCAL 的 C 套接字服务器。
#define NAME "#/tmp/kvsd"
int
main()
{
int sock, msgsock, rval;
struct sockaddr_un server;
char buf[1024];
unlink(NAME);
printf("before socket \n");
sock = socket(AF_LOCAL, SOCK_STREAM, 0);
if (sock < 0) {
perror("opening stream socket");
exit(1);
}
memset(&server, 0, (sizeof (server)));
server.sun_family = AF_LOCAL;
memcpy(server.sun_path, NAME, strlen(NAME));
server.sun_path[0] = 0;
printf("before bind \n");
int len = strlen(server.sun_path) + sizeof(server.sun_family);
if (bind(sock, (struct sockaddr *) &server, len)) {
perror("binding stream socket");
exit(1);
}
printf("before listen \n");
if (listen(sock, 5) == -1) {
perror("listen");
exit(1);
}
printf("before accept \n");
msgsock = accept(sock, 0, 0);
printf("accepted \n");
if (msgsock == -1)
perror("accept");
else do {
bzero(buf, sizeof(buf));
printf("before read \n");
if ((rval = read(msgsock, buf, 1024)) < 0)
perror("reading stream message");
else if (rval == 0)
printf("Ending connection\n");
else
printf("-->%s\n", buf);
} while (rval > 0);
close(msgsock);
close(sock);
unlink(NAME);
}
和 Python AF_UNIX client.py:-
####### CLIENT CODE #######
from socket import *
# Create an unbond and not-connected socket.
sock = socket(AF_UNIX, SOCK_STREAM)
# Connect to the peer registered as "MyBindName" in the abstract namespace. Note the '\0'.
str = "\0/tmp/kvsd\0"
print "len ", len (str)
sock.connect("\0/tmp/kvsd")
# Wait for message
msg = sock.recv(100)
print msg
# Send reply
sock.send("Hi there!\n")
# Block until new message arrives
msg = sock.recv(100)
# When the socket is closed cleanly, recv unblocks and returns ""
if not msg:
print "It seems the other side has closed its connection"
# Close it
sock.close()
但是当我运行客户端时,出现以下错误:
[root@mat afunix]# python ./client.py len 11 Traceback(最近一次调用最后):文件“./client.py”,第 13 行,在 sock.connect("\0/tmp/kvsd") 文件中"",第 1 行,在连接 socket.error: [Errno 111] Connection denied [root@mat afunix]#
我正在尝试为 UNIX 套接字使用抽象命名空间,但我的 python 客户端无法连接到 c 服务器。
我试过没有抽象名称空间它可以工作。(将 server.c 中的 NAME 宏更改为“/tmp/kvsd”,并将 sock.connect 的参数更改为“/tmp/kvsd”)。
有人可以帮我弄清楚可能是什么问题吗?
提前致谢。