recvmsg() 不支持操作
我试图使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我没记错的话, 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.