Note that there are some explanatory texts on larger screens.

plurals
  1. POShould/can a client use the same port to bind to multiple servers
    primarykey
    data
    text
    <p>Let's say I have a client who connects over TCP to 1000 different servers on a bunch of hosts. And then I'd like to start up 100 clients on the same host. I normally would run out of ephemeral ports (assigned by the os). My solution is for each client to bind to 1 port when connecting to all 1000 servers (thus only using 100 ports).</p> <p>My question is - should this be something I just do by default? In other words, in my custom connection layer I've built, should clients automatically reuse the same port to connect to multiple servers (and thus be more scalable), or is there a downside to this approach? I am using Linux.</p> <p>Update: turns out my solution relies on the availability of <code>SO_REUSEPORT</code>. The main use case for <code>SO_REUSEPORT</code> is for load-balancing listening sockets where multiple threads/processes are used to manage incoming connections, but I don't see any reason why it can't be used for TCP clients binding to the same port before connecting. <a href="https://lwn.net/Articles/542629/" rel="nofollow">Linux kernel 3.9 now supports this</a>. Using python 3.3.2 and Linux 3.11.0, the following works:</p> <pre><code>from socket import * s1 = socket(AF_INET, SOCK_STREAM) s1.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1) s1.bind(('localhost', 23456)) s2 = socket(AF_INET, SOCK_STREAM) s2.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1) s2.bind(('localhost', 23456)) </code></pre> <p>as @glglgl suggested would be nice, it fails as late as possible, and if you try to connect to the same remote <code>(host, port)</code>, you get an error message:</p> <pre><code>OSError: [Errno 99] Cannot assign requested address </code></pre> <p>I don't see any reason why this can't be used for scalability issues with lots of processes connecting to lots of other processes over TCP, and I would love to hear any comments as to why I <strong>shouldn't</strong> just build into my connection layer clients just re-using the same port by default.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload