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 6170679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:04:55+00:00 2026-05-23T23:04:55+00:00

I’ve written client code that’s supposed to send some data through a socket and

  • 0

I’ve written client code that’s supposed to send some data through a socket and read back an answer from the remote server.

I would like to unit-test that code. The function’s signature is something along the lines of:

double call_remote(double[] args, int fd);

where fd is the file descriptor of the socket to the remote server.

Now the call_remote function will, after sending the data, block on reading the answer from the server. How can I stub such a remote server for unit-testing the code?

Ideally I would like something like:

int main() {
  int stub = /* initialize stub */
  double expected = 42.0;

  assert(expected == call_remote(/* args */, stub);

  return 0;
}

double stub_behavior(double[] args) {
  return 42.0;
}

I would like stub_behavior to be called and send the 42.0 value down the stubbed file descriptor.

Any easy way I can do that?

  • 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-05-23T23:04:55+00:00Added an answer on May 23, 2026 at 11:04 pm

    If this is a POSIX system, you can use fork() and socketpair():

    #define N_DOUBLES_EXPECTED 10
    double stub_behaviour(double []);
    
    int initialize_stub(void)
    {
        int sock[2];
        double data[N_DOUBLES_EXPECTED];
    
        socketpair(AF_UNIX, SOCK_STREAM, 0, sock);
    
        if (fork()) {
            /* Parent process */
            close(sock[0]);
            return sock[1];
        }
    
        /* Child process */
    
        close(sock[1]);
    
        /* read N_DOUBLES_EXPECTED in */
        read(sock[0], data, sizeof data);
    
        /* execute stub */
        data[0] = stub_behaviour(data);
    
        /* write one double back */
        write(sock[0], data, sizeof data[0]);
        close(sock[0]);
        _exit(0);
    }
    
    
    int main()
    {
      int stub = initialize_stub();
      double expected = 42.0;
    
      assert(expected == call_remote(/* args */, stub);
    
      return 0;
    }
    
    double stub_behavior(double args[])
    {
      return 42.0;
    }
    

    …of course, you will probably want to add some error checking, and alter the logic that reads the request.

    The file descriptor created by socketpair() is a normal socket, and thus socket calls like send() and recv() will work fine on it.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.