[Socket API] Create local server socket without specifying a set port number? - TCP-IP

This is a discussion on [Socket API] Create local server socket without specifying a set port number? - TCP-IP ; Hi all, I need to emulate the socketpair() function on a platform that doesn't provide this function natively. I intend to achieve this by creating a local server socket, then connecting to it via another socket and finally accepting the ...

+ Reply to Thread
Results 1 to 8 of 8

Thread: [Socket API] Create local server socket without specifying a set port number?

  1. [Socket API] Create local server socket without specifying a set port number?

    Hi all,

    I need to emulate the socketpair() function on a platform that doesn't
    provide this function natively. I intend to achieve this by creating a
    local server socket, then connecting to it via another socket and
    finally accepting the incoming connection attempt. This would give me a
    pair of connected sockets, just like the socketpair() function.
    Now the question I have is this. In order to be able to connect() to my
    server socket, I first need to bind() it to a set port number and then
    specify that port number in my connect() call. But since both the
    server socket and the connecting socket exist on the same computer (and
    even in the same process) it isn't really necessary to have some "well
    known" port number (and run the risk of that port number already being
    occupied by some other application). So .... is there are way to let
    the system assign an arbitrary port number to my server socket and then
    calling some function to find out about that port number so I can
    specify in my connect() call?
    Thanks in advance for any useful replies!

    Regards,

    Markus


  2. Re: [Socket API] Create local server socket without specifying aset port number?

    antred wrote:

    > Hi all,
    >
    > I need to emulate the socketpair() function on a platform that doesn't
    > provide this function natively. I intend to achieve this by creating a
    > local server socket, then connecting to it via another socket and
    > finally accepting the incoming connection attempt. This would give me a
    > pair of connected sockets, just like the socketpair() function.
    > Now the question I have is this. In order to be able to connect() to my
    > server socket, I first need to bind() it to a set port number and then
    > specify that port number in my connect() call. But since both the
    > server socket and the connecting socket exist on the same computer (and
    > even in the same process) it isn't really necessary to have some "well
    > known" port number (and run the risk of that port number already being
    > occupied by some other application). So .... is there are way to let
    > the system assign an arbitrary port number to my server socket and then
    > calling some function to find out about that port number so I can
    > specify in my connect() call?


    Bind to port '0' to get a system assigned port, then call getsockname() to find
    out the port number.

    --
    Phil Frisbie, Jr.
    Hawk Software
    http://www.hawksoft.com

  3. Re: Create local server socket without specifying a set port number?

    Excellent, just what I wanted. Thanks!


  4. Re: [Socket API] Create local server socket without specifying a set port number?

    In article <1146574102.483134.320630@i39g2000cwa.googlegroups. com>,
    "antred" wrote:

    > Hi all,
    >
    > I need to emulate the socketpair() function on a platform that doesn't
    > provide this function natively. I intend to achieve this by creating a
    > local server socket, then connecting to it via another socket and
    > finally accepting the incoming connection attempt. This would give me a
    > pair of connected sockets, just like the socketpair() function.
    > Now the question I have is this. In order to be able to connect() to my
    > server socket, I first need to bind() it to a set port number and then
    > specify that port number in my connect() call. But since both the
    > server socket and the connecting socket exist on the same computer (and
    > even in the same process) it isn't really necessary to have some "well
    > known" port number (and run the risk of that port number already being
    > occupied by some other application). So .... is there are way to let
    > the system assign an arbitrary port number to my server socket and then
    > calling some function to find out about that port number so I can
    > specify in my connect() call?
    > Thanks in advance for any useful replies!


    Why do you need port numbers at all? Shouldn't socketpair() use
    Unix-domain sockets rather than Internet sockets?

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  5. Re: Create local server socket without specifying a set port number?

    Well, like I said, I cannot use the actual socketpair() function since
    it's not available on Windows. Therefore I'm writing my own fake
    socketpair() function. If you know a better way of doing this than the
    one I mentioned I'm all ears. ;-)


  6. Re: Create local server socket without specifying a set port number?

    antred wrote:
    > Well, like I said, I cannot use the actual socketpair() function since
    > it's not available on Windows. Therefore I'm writing my own fake
    > socketpair() function. If you know a better way of doing this than the
    > one I mentioned I'm all ears. ;-)


    Cygwin uses TCP/IP sockets. If you're writing GPL compatible code then
    you could look at their code for ideas.


    Sam

  7. Re: Create local server socket without specifying a set port number?

    In article <1146647709.499208.283310@v46g2000cwv.googlegroups. com>,
    "antred" wrote:

    > Well, like I said, I cannot use the actual socketpair() function since
    > it's not available on Windows. Therefore I'm writing my own fake
    > socketpair() function. If you know a better way of doing this than the
    > one I mentioned I'm all ears. ;-)


    You said the platform doesn't have socketpair(), you never said it
    doesn't have Unix-domain sockets.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  8. Re: Create local server socket without specifying a set port number?

    In article ,
    Barry Margolin wrote:

    >> Well, like I said, I cannot use the actual socketpair() function since
    >> it's not available on Windows. Therefore I'm writing my own fake
    >> socketpair() function. If you know a better way of doing this than the
    >> one I mentioned I'm all ears. ;-)

    >
    >You said the platform doesn't have socketpair(), you never said it
    >doesn't have Unix-domain sockets.


    Why should the other person care about UNIX domain sockets? The
    question was about socketpair(), which can work with more protocol
    families than PF_UNIX (or PF_LOCAL in the new world order, except that
    isn't PF_UNIX the new order and AF_UNIX the original?).

    What do UNIX domain sockets offer, other than
    1. an obscure way to do something very similar to the pipe() system call
    on modern BSD flavors that have bidirectional pipes.
    2. names in the file system instead of IP addresses
    3. more throughput than loopback TCP or UDP on some systems, and less
    on other systems
    4. some hard to fix races in how applications handle deleting the file
    system nodes
    5. some access control or authorization mechanisms not available
    with TCP or UDP sockets, except on operating systems with some
    sort of MAC (Mandatory Access Control, not network Media Access
    Controller) on port numbers. Such systems are mostly sold to
    spooky government agencies because they're too much useless hassle
    in the real world.

    I count all except #2 and maybe #5 as problems instead of advantages.

    I think a socketpair() work-alike to what a nearby `man socketpair` says
    int socketpair(int d, int type, int protocol, int *sv);
    for d=PF_INET should need only a handful of lines. What am I missing?

    You might even handle d=PF_UNIX. I think I recall implementing a
    libc user-space socketpair() on top of some flavor of UNIX pipes
    for a commercial UNIX system a couple of decades ago.


    Vernon Schryver vjs@rhyolite.com

+ Reply to Thread