English 中文(简体)
是否有“组合(组合)”的目的,即不区分客户的域名。
原标题:Is there any purpose to `bind()` unix domain socket client processes?

在使用<代码>AF_UNIX(unix area sockets)时,在从未打电话到bind(<>的程序中,?

在我的系统方案讲座和实验室中,我们奉命在不定的域名客户程序中打电话bind(。 是否有任何、无证件或实际申请要求client- onlyunix area socket process? 根据我的理解,bind(>>建立了特别的袖珍档案,这是服务器程序的责任。 这是否正确?

这里是一个例子,其依据是分类中讨论的概念:

server.c

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>

int main() {
    int s0, s1;
    struct sockaddr sa0 = {AF_UNIX, "a"};

    s0 = socket(AF_UNIX, SOCK_STREAM, 0);

    bind(s0, &sa0, sizeof(sa0) + sizeof("a"));
    listen(s0, 1);
    for (;;) {
        s1 = accept(s0, NULL, NULL);
        puts("connected!");
        for (;;) {
            char text[1];
            ssize_t n = read(s1, &text, sizeof(text));
            if (n < 1) {
                break;
            }
            putchar(text[0]);
        }
        close(s1);
    }
    close(s0);
    unlink("a");
    return 0;
}

client.c

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>

int main() {
    int s0;
    struct sockaddr sa0 = {AF_UNIX, "b"};
    struct sockaddr sa1 = {AF_UNIX, "a"};
    socklen_t sa1_len;

    s0 = socket(AF_UNIX, SOCK_STREAM, 0);
    bind(s0, &sa0, sizeof(sa0) + sizeof("b")); // What does this do??
    connect(s0, &sa1, sizeof(sa1) + sizeof("b"));
    for (;;) {
        int c = fgetc(stdin);
        if (c == EOF) {
            break;
        }
        write(s0, &c, sizeof(c));
    }
    close(s0);
    unlink("b");
    return 0;
}
问题回答

只有在需要与<代码>SOCK_STREAM链接时,方可要求打电话到bind(>。 表格类型,但bind()的行为取决于SOCKET。 http://linux.die.net/man/7/unix“rel=“nofollow noretinger”>manual 。 专用网页。

有用信息:

Address format

A UNIX domain socket address is represented in the following structure:

#define UNIX_PATH_MAX    108

struct sockaddr_un {
    sa_family_t sun_family;               /* AF_UNIX */
    char        sun_path[UNIX_PATH_MAX];  /* pathname */
};

Three types of address are distinguished in this structure:

  • pathname: a UNIX domain socket can be bound to a null-terminated file system pathname using bind(2). When the address of the socket is returned by getsockname(2), getpeername(2), and accept(2), its length is offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1, and sun_path contains the null-terminated pathname.

  • unnamed: A stream socket that has not been bound to a pathname using bind(2) has no name. Likewise, the two sockets created by socketpair(2) are unnamed. When the address of an unnamed socket is returned by getsockname(2), getpeername(2), and accept(2), its length is sizeof(sa_family_t), and sun_path should not be inspected.

  • abstract: an abstract socket address is distinguished by the fact that sun_path[0] is a null byte ( ). The socket s address in this namespace is given by the additional bytes in sun_path that are covered by the specified length of the address structure. (Null bytes in the name have no special significance.) The name has no connection with file system pathnames. When the address of an abstract socket is returned by getsockname(2), getpeername(2), and accept(2), the returned addrlen is greater than sizeof(sa_family_t) (i.e., greater than 2), and the name of the socket is contained in the first (addrlen

  • sizeof(sa_family_t)) bytes of sun_path. The abstract socket namespace is a nonportable Linux extension.

Binding to a socket with a filename creates a socket in the file system that must be deleted by the caller when it is no longer needed (using unlink(2)). The usual UNIX close-behind semantics apply; the socket can be unlinked at any time and will be finally removed from the file system when the last reference to it is closed.


因此:

  • bind() is not necessary in a client.
  • bind() in your context give a name to yours sockets "a" and "b"
  • bind(s0, &sa0, sizeof(sa0) + sizeof("b")); and similar line in yours code are undefined behavior; it gives a wrong size to bind() that exceeds the bound of &sa0. The correct code is bind(s0, &sa0, sizeof sa0);
  • bind() in this context (Linux, AF_UNIX) does create a special socket file; if you want to remove it, you must call unlink() or remove().

   When  a  socket  is  created  with socket(2), it exists in a name space
   (address family) but has no address assigned to it.  bind() assigns the
   address  specified  by  addr  to  the  socket  referred  to by the file
   descriptor sockfd.  addrlen  specifies  the  size,  in  bytes,  of  the
   address structure pointed to by addr.  Traditionally, this operation is
   called “assigning a name to a socket”.

   It is normally necessary to assign a local address using bind()  before
   a SOCK_STREAM socket may receive connections (see accept(2)).

bind()列入“Unix-domain socket”,无意打上accept(>,这是确保只有一份程序副本的有效办法。 它比依赖一个过程名称更加强大,因为双筒可以被复制并用另一个名称操作。

但是,关于非正常终止的清理(SIGSEGV,即-9.的目标)是一个问题。 如果你的申请是用信号手写的,那么就应当删除这块空白。

我只是用不定的图表表碰到像这样的东西。 wpa_supplicant有一个控制接口,使用不清的数据图。 客户必须把车单的尾声连接到一条路上,尽管客户的袖珍书将连接到轮椅服务器上。 如果这一步骤没有做到,那么服务器就不能向客户发出回复,但尝试失败的是ENOTCONN错误。

我是一位长期以来的C方案家,这是我第一次碰到这一行为。 看来像Wpa_supplicant一样,基本上试图使用数据表,因为它们是流层的,我不理解为什么。

在使用<代码>AF_UNIX(unix area sockets)时,在从未打电话到bind(<>的程序中,?

短式:是。 某些袖珍议定书是没有关联的,你可以发出和接收,而无需做任何联系或倾听或接受;你可以把<条码>(<>条/条码”发送到一个相关的袖珍目录上,但你可以<条码><<>sendto(<>>>>>>>>>>条/代码>。 <代码>AF_UNIX, SOCK_DGRAMsockets, 唯一可以找到的地址是一条途径。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签