recvmsg() 不支持操作

发布于 2024-10-25 13:10:57 字数 1477 浏览 3 评论 0原文

我试图使用 sendmsg、recvmsg 函数在两个进程之间传递文件描述符。

我通过在 unix 域中创建套接字来实现这一点。

当我执行它时,我得到了recvmsg不支持操作的错误,它与sendmsg一起工作正常..

以下是代码..

    struct sockaddr_un serveraddr;
if((sfd=socket(AF_UNIX,SOCK_STREAM,0))<0)
{
    perror("socket() failed");
    unlink(SERVER_PATH);
    exit(0);
}
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sun_family = AF_UNIX;
strcpy(serveraddr.sun_path, SERVER_PATH);
if((rc = bind(sfd, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr)))<0)
{
    perror("bind() failed");
    unlink(SERVER_PATH);
    exit(0);
}

if((rc = listen(sfd, 10))<0)
{
    perror("listen() failed");
    exit(0);
}

printf("Ready for client connect().\n");

nsfd = accept(sfd, NULL, NULL);
if (nsfd < 0)
{
    perror("accept() failed");
    unlink(SERVER_PATH);
    exit(0);
}
/*rc = setsockopt(nsfd, SOL_SOCKET, SO_RCVLOWAT,(char *)&length, sizeof(length));
if (rc < 0)
{
    perror("setsockopt(SO_RCVLOWAT) failed");
    unlink(SERVER_PATH);
    exit(0);
}*/
recv(nsfd,buffer,200,0);
printf("the data recieved from client is %s\n",buffer);

struct iovec iov[1];
struct msghdr msg;

iov[0].iov_base = (char*)0;
iov[0].iov_len=0;
msg.msg_iov=iov;
msg.msg_iovlen=1;
msg.msg_name=(caddr_t)0;
msg.msg_control=(caddr_t)&fd;
msg.msg_controllen=sizeof(fd);
if(recvmsg(nsfd,&msg,0)<0)
{
    perror("recvmsg error:");
    unlink(SERVER_PATH);
    exit(0);
}

任何人请让我知道我能做些什么来解决该错误..

iam trying to pass the file descriptor between two processes using sendmsg, recvmsg functions..

Iam doing it by creating the socket in unix domain.

When i execute it iam getting the operation not supported error for recvmsg, it is working fine with sendmsg..

the following is the code..

    struct sockaddr_un serveraddr;
if((sfd=socket(AF_UNIX,SOCK_STREAM,0))<0)
{
    perror("socket() failed");
    unlink(SERVER_PATH);
    exit(0);
}
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sun_family = AF_UNIX;
strcpy(serveraddr.sun_path, SERVER_PATH);
if((rc = bind(sfd, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr)))<0)
{
    perror("bind() failed");
    unlink(SERVER_PATH);
    exit(0);
}

if((rc = listen(sfd, 10))<0)
{
    perror("listen() failed");
    exit(0);
}

printf("Ready for client connect().\n");

nsfd = accept(sfd, NULL, NULL);
if (nsfd < 0)
{
    perror("accept() failed");
    unlink(SERVER_PATH);
    exit(0);
}
/*rc = setsockopt(nsfd, SOL_SOCKET, SO_RCVLOWAT,(char *)&length, sizeof(length));
if (rc < 0)
{
    perror("setsockopt(SO_RCVLOWAT) failed");
    unlink(SERVER_PATH);
    exit(0);
}*/
recv(nsfd,buffer,200,0);
printf("the data recieved from client is %s\n",buffer);

struct iovec iov[1];
struct msghdr msg;

iov[0].iov_base = (char*)0;
iov[0].iov_len=0;
msg.msg_iov=iov;
msg.msg_iovlen=1;
msg.msg_name=(caddr_t)0;
msg.msg_control=(caddr_t)&fd;
msg.msg_controllen=sizeof(fd);
if(recvmsg(nsfd,&msg,0)<0)
{
    perror("recvmsg error:");
    unlink(SERVER_PATH);
    exit(0);
}

any one please let me know what can i do to solve the error..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2024-11-01 13:10:57

如果我没记错的话, fd 将指向一个在进程内唯一的整数,但在该进程之外毫无用处。您究竟想要实现什么目标?如果您尝试在两个进程之间重用文件句柄,您可能需要考虑创建命名管道。

有关命名管道的维基百科信息

编辑:正如 Ben 指出的那样,我错了。谢谢本。

If I'm not mistaken, fd will point to an integer that is unique within a process but quite useless outside of that process. What exactly are you trying to achieve? If you're trying to reuse a file handle between two processes, you may want to look into creating a named pipe.

Wikipedia info on Named Pipes

Edit: As Ben points out, I'm mistaken. Thanks Ben.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文