Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8218759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:54:45+00:00 2026-06-07T12:54:45+00:00

I have 2 applications on the same system that I need to communicate back

  • 0

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.

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.

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:54:47+00:00Added an answer on June 7, 2026 at 12:54 pm

    You can use socketpair only where you create both processes, like so:

    1. 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
    2. call fork – now you have two processes
      1. 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
      2. 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
    3. you now have two processes, each has one file descriptor representing different ends of the same pipe. Note that both processes are running the same program, but they followed a different branch after calling fork. If parent calls write on its socket, child will be able to read that data from its socket, and vice-versa

    Here is a straight translation into code:

    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", 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.


    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:

    • 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two separate applications (both part of the same system) that share a
I have a Full and Lite applications that were built from the same code.
I have two Rails 3 applications that will share portions of the same database
I have two mdi applications, both of which retrieve their data from the same
I have an application that communicate with multiple hosts at the same time. In
I would like to have different applications under the same domain using Heroku. Because
I have two web applications in the same solution. They both use different membership/profile
Is it possible to have 2 applications write to the same log file using
In my production environment I have several Web applications installed in the same machine.
I have an application that has 2 beans with the same name, but which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.