int
pipe(
int
fildes[
2]
)
{
int
tcp1,
tcp2;
sockaddr_in
name;
memset
(
&
name,
0,
sizeof
(
name)
)
;
name.
sin_family =
AF_INET
;
name.
sin_addr.
s_addr =
htonl
(
INADDR_LOOPBACK
)
;
int
namelen =
sizeof
(
name)
;
tcp1 =
tcp2 =
-
1;
int
tcp =
socket
(
AF_INET
,
SOCK_STREAM
,
0)
;
if
(
tcp =
=
-
1)
{
goto
clean;
}
if
(
bind
(
tcp,
(
sockaddr
*
)
&
name,
namelen)
=
=
-
1)
{
goto
clean;
}
if
(
listen
(
tcp,
5)
=
=
-
1)
{
goto
clean;
}
if
(
getsockname
(
tcp,
(
sockaddr
*
)
&
name,
&
namelen)
=
=
-
1)
{
goto
clean;
}
tcp1 =
socket
(
AF_INET
,
SOCK_STREAM
,
0)
;
if
(
tcp1 =
=
-
1)
{
goto
clean;
}
if
(
-
1 =
=
connect
(
tcp1,
(
sockaddr
*
)
&
name,
namelen)
)
{
goto
clean;
}
tcp2 =
accept
(
tcp,
(
sockaddr
*
)
&
name,
&
namelen)
;
if
(
tcp2 =
=
-
1)
{
goto
clean;
}
if
(
closesocket(
tcp)
=
=
-
1)
{
goto
clean;
}
fildes[
0]
=
tcp1;
fildes[
1]
=
tcp2;
return
0;
clean:
if
(
tcp !
=
-
1)
{
closesocket(
tcp)
;
}
if
(
tcp2 !
=
-
1)
{
closesocket(
tcp2)
;
}
if
(
tcp1 !
=
-
1)
{
closesocket(
tcp1)
;
}
return
-
1;
}
缺点很明显
1、效率低下。
2、占用了两个TCP端口。
3、accept的返回值未必就是tcp1连接过来的(多线程或者别的进程在干预), 所以最好通过发送数据进行确认。
4、由于不是匿名的, 所以可以在netstat里面看到。
优点只有一个, 可以使用select调用。
相关推荐
linux平台下实现进程间通信的一种:pipe。以字符串为例。
当没有硬件或中断处理程序时(如示例scull字符设备模拟程序)我们可以使用一个缓冲区和另一个进程写进程来产生数据并唤醒读取进程;类似的,阻塞在缓冲区write调用上的写进程也可以有另一读进程唤醒。这就是实现类...
ibm提供的一个小工具,用来备份数据流(一个向tsm读写数据流的工具)
有时我们需要重定向stdout,stderr,stdin。比如:将输出写入文件,又或者我们要将命令行程序输出结果显示到Windows对话框中。源码示范如何在Windows中实现,封装了几个简单易用的函数。
下述资源来源于 www.codeproject.com
多个进程利用pipe连接构成pipe ring, 利用这个环进行pipe sorting
BLOG_【故障处理】Oracle_lhr_TNS-12518、TNS-00517和 Linux Error:32:Broken pipe.pdfBLOG_【故障处理】Oracle_lhr_TNS-12518、TNS-00517和 Linux Error:32:Broken pipe.pdf
Linux下的无名管道pipe的设计.docx
1、应用UNIX的fork()等系统调用,编写一个c程序具有以下功能: a) 实现Shell的基本功能,包括有:打印提示符;...’并处理之) d)应用 dup(), pipe()系统调用具有输入输出重定向以及管道功能;
The PHY Interface for the PCI Express, SATA, and USB SuperSpeed Architectures (PIPE) is intended to enable the development of functionally equivalent PCI Express, SATA and USB SuperSpeed PHY's....
在PIPE中可以绘制Petri网,模拟Petri网的动态效果,从而让大家能直观地对Petri网有认识
操作系统实验四:Linux下进程管道通信 任务1:使用Pipe创建管道,创建一个子进程,子进程向父进程发送消息“I am your son!”,父进程接收到子进程的消息后,显示在屏幕上,并向子进程发送“I am your father!”。子...
本篇文章主要介绍了linux编程之pipe()函数详解,具有一定的参考价值,有需要的可以了解一下。
命名管道NamedPipe,c/s模型。模拟管道的建立,数据传输
用pipe coding for operation system
USB3.0 PIPE总线协议 针对usb物理接口芯片,所使用的pipe 总线的详细解释
pipe2.4
pipe flow expert 教程 2013版本
This simple example illustrates using the Pipe Control.This example also demonstrates the following...1) Modifying basic control properties at runtime