⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fdpass.html

📁 SDK FAQ集
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"><html lang="en"><head><title>Winsock Programmer's FAQ: Passing Sockets Between Processes</title><link rel="Stylesheet" type="text/css" href="../faq.css"></head><body bgcolor="#ffffee" text="#000000" link="#491e00" vlink="#7d2e01" alink="#da7417"><!--  ---- Header Bar ----  --><table border="0" width="95%" bgcolor="#006000" cellpadding="5" cellspacing="3" align="center">	<tr>		<td align="left" bgcolor="#e0e0c0">			<font size="2" face=Verdana,Arial,Helvetica>				<b><a href="../examples/rawping.html">&lt;&lt;</a></b>			</font>		</td>		<td align="center">			<font face=Verdana,Arial,Helvetica color="#ffffee">				<p align=center class=bigger3><b>				Winsock Programmer's FAQ<br>				Example: How to Pass a Socket Handle				</b></p>			</font>			</td>		<td align="right" bgcolor="#e0e0c0">			<font size="2" face=Verdana,Arial,Helvetica>				<b><a href="../examples/ipaddr.html">&gt;&gt;</a></b>			</font>		</td>	</tr></table><!--  ---- Body Table ----  --><table width="95%" border="0" cellpadding="10">	<tr valign="top">		<td><p>This example is based on the example in the Microsoft documentationfor the <tt>_pipe()</tt> run-time library function. It was broughtto my attention by <a href="mailto:Laurent_Michel@brown.edu">ValeriePuffet-Michel</a>. I took the example and made it a bit more robust,partly to catch the Borland C++ bug described below.</p><p>Essentially, this example works on the Win32 rule that a childprocess can inherit the file handles that the parent opened. The programpasses the file handle IDs on the command line to the child that itspawns. Since file handles and socket descriptors are the same thingin Win32, this works just as well for sockets as it does for the pipehandles used in the example.</p><p>This example will not work under Borland C++ 5.0because of a run-time library bug, documented in <ahref="http://loki.borland.com/cpp/bugs/43101.htm">bug report43101</a>. The report does not give any workarounds for this problem,so you may be out of luck if you're trying to do this with BorlandC++. Still, it will compile now. B-)</p><hr noshade size=1 color=#404040><h4 class=lmargin><a href="../examples/src/fdpass.cpp">fdpass.cpp</a></h4><pre><font color="#444444">// Borland C++ 5.0: bcc32 fdpass.cpp</font><font color="#444444">// Visual C++ 5.0:  cl fdpass.cpp</font><font color="0000ff"><strong>#include <font color="#008000">&lt;stdlib.h&gt;</font></strong></font><font color="0000ff"><strong>#include <font color="#008000">&lt;stdio.h&gt;</font></strong></font><font color="0000ff"><strong>#include <font color="#008000">&lt;io.h&gt;</font></strong></font><font color="0000ff"><strong>#include <font color="#008000">&lt;fcntl.h&gt;</font></strong></font><font color="0000ff"><strong>#include <font color="#008000">&lt;process.h&gt;</font></strong></font><font color="0000ff"><strong>#include <font color="#008000">&lt;math.h&gt;</font></strong></font><strong>enum</strong> <font color="#2040a0">PIPES</font> <font color="4444FF"><strong>{</strong></font>    <font color="#2040a0">READ</font> <font color="4444FF">=</font> <font color="#FF0000">0</font>,    <font color="#2040a0">WRITE</font> <font color="4444FF">=</font> <font color="#FF0000">1</font><font color="4444FF"><strong>}</strong></font><font color="4444FF">;</font><font color="0000ff"><strong>#define NUMPROBLEM 8</strong></font><font color="0000ff"><strong>#ifdef _MSC_VER</strong></font><font color="0000ff"><strong>    #define CWAIT _cwait</strong></font><font color="0000ff"><strong>#else</strong></font><font color="0000ff"><strong>    #define CWAIT cwait</strong></font><font color="0000ff"><strong>#endif</strong></font><strong>int</strong> <font color="#2040a0">main</font><font color="4444FF">(</font><strong>int</strong> <font color="#2040a0">argc</font>, <strong>char</strong> <font color="4444FF">*</font><font color="#2040a0">argv</font><font color="4444FF">[</font><font color="4444FF">]</font><font color="4444FF">)</font><font color="4444FF"><strong>{</strong></font>    <strong>int</strong> <font color="#2040a0">hpipe</font><font color="4444FF">[</font><font color="#FF0000">2</font><font color="4444FF">]</font><font color="4444FF">;</font>    <strong>char</strong> <font color="#2040a0">hstr</font><font color="4444FF">[</font><font color="#FF0000">20</font><font color="4444FF">]</font><font color="4444FF">;</font>    <strong>int</strong> <font color="#2040a0">pid</font>, <font color="#2040a0">problem</font>, <font color="#2040a0">c</font><font color="4444FF">;</font>    <strong>int</strong> <font color="#2040a0">termstat</font><font color="4444FF">;</font>    <strong>if</strong> <font color="4444FF">(</font><font color="#2040a0">argc</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">1</font><font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font>        <font color="#444444">//// No arguments, so this must be the parent process</font>        <font color="#444444">// Open a set of pipes</font>        <strong>if</strong> <font color="4444FF">(</font><font color="#2040a0">_pipe</font><font color="4444FF">(</font><font color="#2040a0">hpipe</font>, <font color="#FF0000">256</font>, <font color="#2040a0">O_BINARY</font><font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="4444FF">-</font><font color="#FF0000">1</font><font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font>            <font color="#2040a0">perror</font><font color="4444FF">(</font><font color="#008000">&quot;pipe failed&quot;</font><font color="4444FF">)</font><font color="4444FF">;</font>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -