Re: Listen to multiple ports

Discussion in 'C Programming' started by Ersek, Laszlo, Jun 24, 2010.

  1. On Thu, 24 Jun 2010, Christian wrote:

    > Is there a way, within a single process, to listen to several TCP ports ?


    socket()-bind()-listen() twice, then put them in an fd_set and wait with
    select() till readability is signalled on either. (You can use poll()
    equivalently.) Then accept() on each signalled.

    lacos
    Ersek, Laszlo, Jun 24, 2010
    #1
    1. Advertising

  2. Ersek, Laszlo

    Nobody Guest

    On Fri, 25 Jun 2010 07:56:50 +0200, Christian wrote:

    > One more thing though, regarding performance : wouldn't it be better to run
    > 2 separate processes ?


    No. Once a connection is accepted, it will be handed off to a worker
    thread or process. After initialisation, the main process does almost
    nothing but accept() connections, and there's no point accepting
    more connections than you can actually process.

    There's no reason to limit each instance to a single socket (nothing stops
    you from running multiple instances, although you then need to manage each
    instance separately).

    This allows you to have a single instance handling both HTTP and HTTPS,
    IPv4 and IPv6, port 80 and 8000 (it's not uncommon to offer the same
    service on a non-standard port as a convenience for users whose ISPs try
    to get "clever" with traffic to port 80). You only need one configuration
    file, one set of log files, start/stop one process, etc.
    Nobody, Jun 25, 2010
    #2
    1. Advertising

  3. Ersek, Laszlo

    Jorgen Grahn Guest

    On Fri, 2010-06-25, Christian wrote:
    >> On Fri, 25 Jun 2010 07:56:50 +0200, Christian wrote:
    >>
    >>> One more thing though, regarding performance : wouldn't it be better to
    >>> run
    >>> 2 separate processes ?

    >>
    >> No. Once a connection is accepted, it will be handed off to a worker
    >> thread or process. After initialisation, the main process does almost
    >> nothing but accept() connections, and there's no point accepting
    >> more connections than you can actually process.
    >>
    >> There's no reason to limit each instance to a single socket (nothing stops
    >> you from running multiple instances, although you then need to manage each
    >> instance separately).
    >>
    >> This allows you to have a single instance handling both HTTP and HTTPS,
    >> IPv4 and IPv6, port 80 and 8000 (it's not uncommon to offer the same
    >> service on a non-standard port as a convenience for users whose ISPs try
    >> to get "clever" with traffic to port 80). You only need one configuration
    >> file, one set of log files, start/stop one process, etc.
    >>

    >
    > I was thinking of the kinda overload generated by select()-ing and
    > FD_ISSET()-ing, that wouldn't be needed with 2 separate processes. But I
    > agree with your arguments in favor of a single instance.


    It's true that waking up from a select() and then calling read() is
    more work than just hanging on a read() ... and also that select()
    scales badly to many file descriptors. If you are allowed to be
    Linux-specific, check out epoll(7) as an alternative.

    Anyway, this is a huge topic and offtopic here. Try comp.protocols.tcp-ip.
    But I note you should also get a good book about Unix network
    programming, like W R Stevens' books. There are plenty of silly
    mistakes one can make -- mistakes that don't show up in testing, but
    maybe rarely, over long links or during network congestion.

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Jun 25, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Kipp
    Replies:
    1
    Views:
    1,150
    Jim Gibson
    Feb 12, 2004
  2. Ryan Stewart
    Replies:
    1
    Views:
    816
    Tor Iver Wilhelmsen
    Jan 17, 2004
  3. Donny
    Replies:
    13
    Views:
    657
    Donny
    Aug 5, 2004
  4. billiejoex

    pcapy listen on multiple devices

    billiejoex, Sep 12, 2005, in forum: Python
    Replies:
    1
    Views:
    435
    Jorgen Grahn
    Sep 17, 2005
  5. mreister
    Replies:
    1
    Views:
    3,118
    mreister
    May 25, 2010
Loading...

Share This Page