5

我是两天前编写的第一个程序的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”)。

有人可以帮我弄清楚可能是什么问题吗?

提前致谢。

4

1 回答 1

4

下一行有问题。

    int len = strlen(server.sun_path) + sizeof(server.sun_family);

server.sun_path 现在有前导空字符。所以 strlen(server.sun_path) 为 0。您需要将上面的行更改如下:

    #include <stddef.h>

    ....

    int len = offsetof(struct sockaddr_un, sun_path) + strlen(NAME);

然后,它将起作用。

编辑:更新代码以使用 offsetof 来避免填充问题。(谢谢你,alk)

PS:我假设服务器和客户端都使用名称而不使用尾随空字节。如果您使用带有尾随空字节的名称,请将 1 添加到len.

于 2013-06-14T02:02:36.467 回答