<div dir="ltr"><br><div>The ephemeral ports you mention are used ONLY if  you do not do BIND call on socket for outgoing connection.</div><div>If before doing CONNECT you do BIND,  the local port number will be one specified in BIND.</div><div>Following small program demonstrates it.</div><div><br></div><div>#!/usr/bin/python</div><div><div>import socket</div><div>import threading</div><div><br></div><div><br></div><div>READER_ADDR = ("127.0.0.1", 23008)</div><div>WRITER1_ADDR = ("127.0.0.1", 23001)</div><div>WRITER2_ADDR = ("127.0.0.1", 23002)</div><div><br></div><div><br></div><div>LISTEN_SOCK = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)</div><div>LISTEN_SOCK.bind(READER_ADDR)</div><div>LISTEN_SOCK.listen(3)</div><div><br></div><div>DONE = False</div><div><br></div><div>class wait_conn(threading.Thread):</div><div>    def run(self):</div><div>        while not DONE:</div><div>            s, a = LISTEN_SOCK.accept()</div><div>            print("Incomming connection from: ",  s.getpeername())</div><div>            s.close()</div><div><br></div><div><br></div><div>t = wait_conn()</div><div>t.start()</div><div><br></div><div>w1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)</div><div>w1.bind(WRITER1_ADDR)</div><div>w1.connect(READER_ADDR)</div><div>w1.close()</div><div><br></div><div><br></div><div><br></div><div>w2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)</div><div>w2.bind(WRITER2_ADDR)</div><div>w2.connect(READER_ADDR)</div><div>w2.close()</div><div><br></div><div>DONE = True</div><div><br></div><div>t.join(1.0)</div><div>LISTEN_SOCK.close()</div><div><br></div></div><div> </div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">2018-06-20 15:19 GMT+02:00 Daniel-Constantin Mierla <span dir="ltr"><<a href="mailto:notifications@github.com" target="_blank">notifications@github.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p>By default, Kamailio is reusing the tcp connection when possible.</p>
<p>Again, about the local port for tcp connections, read more about tcp protocol and how a connection is identified (some references about ephemeral ports are also there) -- practically it is the tcp stack implementation/operating system kernel that does the selection of the local port, impossible to ensure it from user space.</p>

<p style="font-size:small;color:#666">—<br>You are receiving this because you commented.<span class=""><br>Reply to this email directly, <a href="https://github.com/kamailio/kamailio/issues/1532#issuecomment-398745383" target="_blank">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AF36ZSFtK7ZUdmBxJE8DCetWIJGGXa_Lks5t-kvggaJpZM4UAV3q" target="_blank">mute the thread</a>.<img src="https://github.com/notifications/beacon/AF36ZTI3yZbgsdO5UPqAUn3Qj4Oj-rBPks5t-kvggaJpZM4UAV3q.gif" height="1" width="1" alt=""></span></p>


<br>______________________________<wbr>_________________<br>
Kamailio (SER) - Development Mailing List<br>
<a href="mailto:sr-dev@lists.kamailio.org">sr-dev@lists.kamailio.org</a><br>
<a href="https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-dev" rel="noreferrer" target="_blank">https://lists.kamailio.org/<wbr>cgi-bin/mailman/listinfo/sr-<wbr>dev</a><br>
<br></blockquote></div><br></div>