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

  • SEARCH
  • Home
  • 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 7003239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:02:29+00:00 2026-05-27T21:02:29+00:00

Using socket programming APIs (e.g., socket(), connect(), accept() …), how can I know if

  • 0

Using socket programming APIs (e.g., socket(), connect(), accept() …), how can I know if a TCP connection is between two processes on the same machine? Say, I have the socket file descriptor, and the remote ip. Can I simply inspect if the remote ip is 127.0.0.1?

  • 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-27T21:02:30+00:00Added an answer on May 27, 2026 at 9:02 pm

    Here is the approach I have used. The idea is to attempt to bind a listener to that IP address and use the failure/success codes to decide whether the address is local.

    I am not claiming this is particularly efficient, but it should be fairly reliable, and for my application it was appropriate.

    #include <sys/socket.h>
    #include <errno.h>
    /* ...probably need some other headers I am forgetting... */
    
    int
    is_local(const struct sockaddr *addr, socklen_t addr_len)
    {
        const char *func = "is_local()";
        int result = 0;
    
        int tmp = socket(addr->sa_family, SOCK_STREAM, 0);
        if (tmp < 0) {
            printf("%s: socket(%d,SOCK_STREAM,0) failed, errno %d\n",
                   func, addr->sa_family);
    
            goto out;
        }
    
        /* If bind() succeeds, or if it fails because the address is in
           use, then the address must be local to this system. */
        if (bind(tmp, addr, addr_len) < 0) {
            if (errno == EADDRINUSE)
                result = 1;
            else if (errno == EADDRNOTAVAIL)
                ; /* do nothing; address is remote */
            else
                printf("%s: bind() unexpected error %d\n", func, errno);
        }
        else {
            result = 1;
        }
    
        close(tmp);
    
     out:
        return result;
    }
    

    You call it like this:

    struct sockaddr_storage client_addr;
    socklen_t client_addr_len = sizeof(client_addr);
    int fd = accept(listener, &client_addr, &client_addr_len);
    
    if (is_local((struct sockaddr *)&client_addr, client_addr_len))
        /* peer is local */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a project using socket programming. If I connect a single client to
I am using TCP/IP socket programming. I have a floating point value stored in
I know basic socket programming. I have a code to send strings using sockets
How do we connect to a database server using any programming language using socket
I'm using socket programing APIs in C. In a TCP client-side program, I use
Using C / C++ socket programming, and the read(socket, buffer, BUFSIZE) method. What exactly
I am currently doing some socket programming using C/C++. To be able to use
I am still learning socket programming (using Perl) but I have both options (
I'm writing a proxy using .NET and C#. I haven't done much Socket programming,
I am using socket to send data from local machine to remote in TCP,

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.