Unix域:connect():没有这样的文件或目录 [英] Unix Domain : connect() : No such file or directory

查看:274
本文介绍了Unix域:connect():没有这样的文件或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题中所述,我对具有相应地址的unix域类型套接字的 connect()调用会导致错误 ENOENT:no such file或directory



两个套接字已正确初始化,并且相应地创建并绑定套接字文件。服务器和客户端套接字在不同的进程中运行,尽管客户端进程是fork()和execl()。这也是我如何解析客户端和服务器套接字的地址,我用于设置客户端套接字。

这是我的 connect()尝试:

  struct sockaddr_un address; 
address.sun_family = AF_UNIX;
memcpy(address.sun_path,filepath.c_str(),filepath.length());
address.sun_path [filepath.length()] ='\0';

if(-1 == connect(this-> unix_domain_descriptor_.descriptor(),\
(struct sockaddr *)& address,\
size)
{
global :: ExitDebug(-1,connect()failed,__FILE__,__LINE__);
return -1;
}

我尝试了不同的尺寸值,例如:

  //这是来自unix(7)手册页。它不工作既不与也没有+1
socklen_t size = offsetof(struct sockaddr_un,sun_path);
size + = strlen(address.sun_path)+ 1;






  /这是从我的一本关于linux编程的书
socklen_t size = sizeof(address);






  /这是从我在互联网上找到的示例代码
socklen_t size = sizeof(address.sun_family)+ strlen(address.sun_path);






  / Update 1:
socklen_t size = SUN_LEN(& address);






  /这是我在查看声明
//之后尝试的结果sockaddr_un
socklen_t size = strlen(address.sun_path);






令人惊讶的是,除了最后一个结果, connect() EINVAL:无效参数错误,我只能使用最后一个 ENOENT:无此类文件或目录。我甚至尝试了从互联网的整个例子,但没有成功。显然,用size_t或 int 交换socklen_t不会改变任何东西。



我已经检查过了:




  • address.sun_path包含从根目录开始的正确的套接字文件路径

  • address.sun_path的长度为61个字符

  • address.sun_family设置为AF_UNIX / AF_LOCAL

  • address.sun_family的大小为2个字节

  • 在创建和绑定两个套接字时没有错误

  • 服务器套接字处于侦听状态

  • sizeof(address)返回110, li>


现在我想知道为什么手册页示例不工作,以及是否有更新没有更新 linux.die.net www.kernel.org 。我的操作系统是Debian Squeeze,如果它是从相关。



任何想法我做错了什么?而如何解决呢?如果你需要更多的代码或有问题,那么不要犹豫,问我(虽然我不需要说明这可能,但这是我的第一篇文章>。<)。



btw,对不起我的英文版。



更新2



解决。

解决方案

在确定我正确处理套接字后,改变我的代码为connect()一点,现在它的工作原理。我只是在我的变量声明之后添加这行:

  memset(& address,0,sizeof(struct sockaddr_un)) ; 

有没有人知道为什么我需要将整个变量设置为0,我应该在一个新主题中问这个问题,还是我可以问这里?


as stated in the title, my connect() call to an unix domain type socket with an according address results in the error ENOENT: no such file or directory.

The two sockets are properly initialized and the socket files are created and bound accordingly. The server and client sockets run in a different process, though the client process is fork()-ed and execl()-ed. This is also how I parse the address for the client and server socket, which I use for setting up the client socket. The server process is using pthreads.

Here is my connect() attempt:

    struct sockaddr_un address;
    address.sun_family = AF_UNIX;
    memcpy(address.sun_path, filepath.c_str(), filepath.length());
    address.sun_path[filepath.length()] = '\0';

    if(-1 == connect(this->unix_domain_descriptor_.descriptor(),       \
                     (struct sockaddr*)&address,                       \
                     size))
    {
        global::ExitDebug(-1, "connect() failed", __FILE__, __LINE__);
        return -1;
    }

I tried out different values for size, such as:

//  this is from unix(7) man page. It doesn't work neither with nor without "+1"
socklen_t size =  offsetof(struct sockaddr_un, sun_path);
          size += strlen(address.sun_path) + 1;


//  this is from one of my books about linux programming
socklen_t size = sizeof(address);


//  this is from a sample code which I found at the internet
socklen_t size = sizeof(address.sun_family) + strlen(address.sun_path);


//  Update 1: 
socklen_t size = SUN_LEN(&address);


//  this is what I tried out after looking into the declaration
//  of struct sockaddr_un
socklen_t size = strlen(address.sun_path);


Surprisingly, all initializations except the last one result in an EINVAL: invalid argument error for connect() and I get the ENOENT: no such file or directory only with the last one. I even tried out entire examples from the internet, but without success. And obviously, swapping socklen_t with size_t or int doesn't change anything.

I already checked for this:

  • address.sun_path contains the correct socket file path starting from the root directory
  • address.sun_path has the length of 61 characters
  • address.sun_family is set to AF_UNIX/AF_LOCAL
  • address.sun_family has the size of 2 bytes
  • no errors at creating and binding both sockets
  • server socket is in listening state
  • sizeof(address) returns 110 as it is supposed to be

Now I was wondering why the man page example was not working and whether there had been changes that were not updated at linux.die.net or www.kernel.org. My OS is Debian Squeeze if it's from relevant.

Any ideas what I am doing wrong? And how to solve it? If you need more code or have questions, then don't hesitate to ask me (though I don't need to state this probably, but this is my first post here >.<).

btw, sorry for my bad english

Update 2

Solved. I will post it in an extra answer below out of clarity.

解决方案

After figuring out that I was handling the sockets properly, I altered my code for connect() a little and now it works. I just added this line after the declaration of my variable:

memset(&address, 0, sizeof(struct sockaddr_un));

Does anyone know why I need to set the whole variable to 0 to make it work? Should I ask this in a new topic or can I ask this here?

这篇关于Unix域:connect():没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆