织梦CMS - 轻松建站从此开始!

罗索

Passing Sockets Between Processes

罗索客 发布于 2006-02-24 15:17 点击:次 
http://tangentsoft.net/wskfaq/examples/fdpass.html This example is based on the example in the Microsoft documentation for the _pipe() run-time library function. It was brought to my attention by Valerie Puffet-Michel (Laurent_Michel at brown dot ed
TAG:

http://tangentsoft.net/wskfaq/examples/fdpass.html


This example is based on the example in the Microsoft documentation for the _pipe() run-time library function. It was brought to my attention by Valerie Puffet-Michel (Laurent_Michel at brown dot edu). I took the example and made a few small modifications to it.

Essentially, this example works on the Win32 rule that a child process can inherit the file handles that the parent opened. The program passes the file handle IDs on the command line to the child that it spawns. Since file handles and socket descriptors are the same thing in Win32, this works just as well for sockets as it does for the pipe handles used in the example.


--------------------------------------------------------------------------------

fdpass.cpp
// Borland C++ 5.0: bcc32 fdpass.cpp
// Visual C++ 5.0:  cl fdpass.cpp
//
// This sample program is hereby placed in the public domain.

#include <stdlib.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <process.h>
#include <math.h>

enum PIPES {
    READ = 0,
    WRITE = 1
};

#define NUMPROBLEM 8

#ifdef _MSC_VER
    #define CWAIT _cwait
#else
    #define CWAIT cwait
#endif

int main(int argc, char *argv[])
{
    int hpipe[2];
    char hstr[20];
    int pid, problem, c;
    int termstat;

    if (argc == 1) {
        //// No arguments, so this must be the parent process
        // Open a set of pipes
        if (_pipe(hpipe, 256, O_BINARY) == -1) {
            perror("pipe failed");
            exit(1);
        }

        // Convert read side of pipe to string and pass as an argument
        // to the child process.
        itoa(hpipe[READ], hstr, 10);
        if ((pid = spawnl(P_NOWAIT, argv[0], argv[0], hstr, NULL)) == -1) {
            perror("Spawn failed");
        }

        // Put problem in write pipe; it will appear in child's read pipe.
        for (problem = 1000; problem <= NUMPROBLEM * 1000; problem += 1000) {
            if (write(hpipe[WRITE], (char *) &problem, sizeof(int)) == -1) {
                perror("parent write failed");
            }
            else {
                printf("Son, what is the square root of %d?\\n", problem);
            }
        }

        // Wait until spawned program is done processing.
        CWAIT(&termstat, pid, WAIT_CHILD);
        if (termstat & 0x0) {
            perror("Child failed");
        }

        close(hpipe[READ]);
        close(hpipe[WRITE]);
    }
    else {
        //// There is a command line argument, so we must be a child process
        // Convert argument to integer handle value.
        hpipe[READ] = atoi(argv[1]);

        // Read problem from pipe and calculate solution.
        for (c = 0; c < NUMPROBLEM; c++) {
            if (read(hpipe[READ], (char *) &problem, sizeof(int)) == -1) {
                perror("child read failed");
            }
            else {
                printf("Dad, the square root of %d is %3.2f.\\n", problem,
                       sqrt((double) problem));
            }
        }
    }

    return 0;
}

(iwgh)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/200602/496.html]
本文出处: 作者:iwgh
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容