- 论坛徽章:
- 0
|
原帖由 wcw 于 2009-3-12 09:59 发表 ![]()
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int fd ...
后来我用unix-socket解决了,不过上面的用ioctl的方法还是不知为什么不行,apue里说的是2中都可以的,下面是尝试成功的:
#include <sys/socket.h>
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <signal.h>
#include <sys/wait.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <stropts.h>
#define CONTROLLEN sizeof (struct cmsghdr) + sizeof (int)
int fd[2];
void child()
{
int file;
if ((file = open("1", O_RDONLY)) < 0) {
perror("open file failed");
exit(-2);
}
printf("fd = %d\n", file);
char tmpbuf[CONTROLLEN];
struct cmsghdr *cmptr = (struct cmsghdr *) tmpbuf;
struct iovec iov[1];
struct msghdr msg;
char buf[1];
iov[0].iov_base = buf;
iov[0].iov_len = 1;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_control = cmptr;
msg.msg_controllen = CONTROLLEN;
cmptr->cmsg_level = SOL_SOCKET;
cmptr->cmsg_type = SCM_RIGHTS;
cmptr->cmsg_len = CONTROLLEN;
*(int *)CMSG_DATA (cmptr) = file;
if (sendmsg(fd[1], &msg, 0) != 1) {
perror("sendmsg failed");
exit(-1);
}
exit(0);
}
int main()
{
//int socketpair(int domain, int type, int protocol, int sockfd[2]);
if (socketpair(AF_UNIX/* or AF_LOCAL*/, SOCK_STREAM, 0, fd) != 0) {
perror("socketpair failed");
return(-1);
}
/*if (pipe(fd) < 0) {
perror("pipe failed");
return -2;
}*/
pid_t pid;
if ((pid = fork()) == 0 ){
child();
}
char tmpbuf[CONTROLLEN];
struct cmsghdr *cmptr = (struct cmsghdr *) tmpbuf;
struct iovec iov[1];
struct msghdr msg;
char buf[1];
iov[0].iov_base = buf;
iov[0].iov_len = sizeof (buf);
msg.msg_iov = iov;
msg.msg_iovlen = 1;
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_control = cmptr;
msg.msg_controllen = CONTROLLEN;
wait(NULL);
if (recvmsg(fd[0], &msg, 0) <= 0) {
perror("recvmsg failed");
exit(-2);
}
int recvfd = *(int *) CMSG_DATA (cmptr);
printf("recvfd = %d\n", recvfd);
char tmp[256];
u_int count;
if ((count = read(recvfd, tmp, sizeof(tmp))) < 0) {
perror("read failed\n");
return -2;
}
tmp[count] = 0;
printf("have read: %s\n", tmp);
return 0;
}
[ 本帖最后由 wcw 于 2009-3-21 18:05 编辑 ] |
|