C/Unix 中的 Socketpair() [英] Socketpair() in C/Unix
问题描述
我在同一个系统上有 2 个应用程序,我需要来回通信.根据我的研究,我相信这称为进程间通信,使用 socketpair() 是解决我的问题的最佳方法.
I have 2 applications on the same system that I need to communicate back and forth. From my research I believe this is called Inter Process Communication and the use of socketpair() is the best method for my problem.
我正在努力(字面意思)尝试开始在 C 中使用 socketpair() 创建套接字.据我所知,套接字是一个非常复杂的话题,我作为一个 C 新手程序员肯定无济于事.
I am tearing my hair out (literally) trying to get started with creating sockets with socketpair() in C. From what I understand, sockets are a very complex topic and me being a novice C programmer is surely not helping the situation.
过去 48 小时我用谷歌搜索,阅读教程等,但我仍然无法理解.我理解这个概念,但代码太混乱了.我读过这篇文章几次:http://beej.us/guide/bgnet/html/single/bgnet.html,但还不够简单.
I googled for the last 48 hours, read tutorials, etc, but I still can't get it. I understand the concept, but the code is just too confusing. I've read this article a few times: http://beej.us/guide/bgnet/html/single/bgnet.html, but it's not simple enough.
有人可以提供一些示例(非常简单,五年级学生都能理解)或给我指点一个好的教程?
Can someone provide some example (so simple a 5th grader could understand) or point me to a good tutorial?
推荐答案
您只能在创建两个进程的地方使用 socketpair
,如下所示:
You can use socketpair
only where you create both processes, like so:
- call
socketpair
- 现在你有两个套接字文件描述符(一个管道的两端)- 指定一端为父,另一端为子.哪个都无所谓,只要做出选择,然后坚持下去
- call
socketpair
- now you have two socket file descriptors (two ends of a single pipe)- nominate one end to be the parent and one to be the child end. It doesn't matter which, just make a choice and stick to it later
- 如果
fork
返回零,你就是孩子.关闭父文件描述符,保留子描述符,并将其用作这个进程的管道末端 - 如果
fork
返回非零值,则您是父级.关闭 child 文件描述符,保留 parent 并将其用作管道的末端
- if
fork
returned zero, you are the child. Close the parent file descriptor, keep the child descriptor, and use it as this process's end of the pipe - if
fork
returned non-zero, you are the parent. Close the child file descriptor, keep the parent one and use it as your end of the pipe
fork
后它们遵循不同的分支.如果父对其套接字调用write
,子将能够从它的套接字读取该数据,反之亦然-相反
fork
. If parent calls write
on its socket, child will be able to read that data from its socket, and vice-versa这是直接翻译成代码:
void child(int socket) {
const char hello[] = "hello parent, I am child";
write(socket, hello, sizeof(hello)); /* NB. this includes nul */
/* go forth and do childish things with this end of the pipe */
}
void parent(int socket) {
/* do parental things with this end, like reading the child's message */
char buf[1024];
int n = read(socket, buf, sizeof(buf));
printf("parent received '%.*s'
", n, buf);
}
void socketfork() {
int fd[2];
static const int parentsocket = 0;
static const int childsocket = 1;
pid_t pid;
/* 1. call socketpair ... */
socketpair(PF_LOCAL, SOCK_STREAM, 0, fd);
/* 2. call fork ... */
pid = fork();
if (pid == 0) { /* 2.1 if fork returned zero, you are the child */
close(fd[parentsocket]); /* Close the parent file descriptor */
child(fd[childsocket]);
} else { /* 2.2 ... you are the parent */
close(fd[childsocket]); /* Close the child file descriptor */
parent(fd[parentsocket]);
}
exit(0); /* do everything in the parent and child functions */
}
请注意,这只是示例代码:我省略了所有错误检查和合理的流协议.
Please note that this is just sample code: I've left out all error-checking and a sensible stream protocol.
如果您希望两个独立的程序进行通信(例如,您有一个名为 client 的可执行文件,另一个名为 server),您可以不要使用这种机制.相反,您可能会:
If you want two separate programs to communicate (eg. you have an executable called client, and one called server), you can't use this mechanism. Instead, you might:
- 使用 UNIX 套接字(其中一台主机上的 IPC 管道由文件名标识 - 这仅适用于 客户端 和 服务器 在同一台机器上运行)
- 或使用 TCP/IP 套接字(其中 IP 地址和端口标识管道,并且客户端和服务器可以在不同的机器上)
- use UNIX sockets (where an IPC pipe on one host is identified by a filename - this only works if client and server run on the same machine)
- or use TCP/IP sockets (where an IP address and port identify the pipe, and the client and server can be on different machines)
如果您不是特别需要套接字,并且您很高兴要求客户端和服务器在同一台机器上运行,您还可以使用共享内存或消息队列.
If you don't specifically need sockets, and you're happy to require that client and server run on the same machine, you can also use shared memory, or message queues.
这篇关于C/Unix 中的 Socketpair()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!