기본 콘텐츠로 건너뛰기

socketpair

socketpair는 IPC 가운데 하나로 보통 부모 자식 간의 대화를 전달할 때 쓴다고 한다. 얘도 pipe처럼 두개 file-descriptor를 던져주는데, pipe와 달리 두 file-descriptor가 모두 읽기/쓰기가 가능하다. 아래는 예제이다.
#include <sys/types.h>
#include <sys/socket.h>
#include <iostream>
#include <errno.h>
#include <sys/wait.h>
using namespace std;

int
main(int,char**)
{
    int s[2];
    if ( -1 == socketpair(AF_UNIX, SOCK_STREAM, AF_LOCAL,s) )
    {
        cout << strerror(errno) << endl;
        return 1;
    }

    char buf[1024];
    if ( fork() )
    {
        strcpy(buf, "Hello, world!");
        send(s[0], buf, strlen(buf)+1, 0);
        int res;
        wait(&res);
    }
    else
    {
        recv(s[1], buf, sizeof(buf), 0);
        cout << buf << endl;
    }

    return 0;
}


어디선가 pipe와 다른 점은 부모자식이 아닌 프로세스 간 통신이 가능하다는데, 부모자식이 아니면 어떻게 소켓을 넘겨주는지 궁금하다.

댓글

이 블로그의 인기 게시물

Winget 해시 무시하기

가끔씩 Winget 에서 패키지를 다운로드 했을 때, "설치 관리자 해시가 일치하지 않습니다." 오류가 뜰 때가 있다. 보안 이슈가 있지만, 그냥 무시하고 싶을 때, 아래 순서로 무시해준다. 관리자 권한 winget settings --enable InstallerHashOverride 설치 winget install --ignore-security-hash --id NirSoft.NirCmd