title: How to make 2 clients connect each other directly, after having both connected a meeting-point server?
body: <p>I'm writing a toy meeting-point/relay server listening on port 5555 for two clients "A" and "B". </p>
  
  <p>It works like this: every byte received by the server from the firstly-connected client A will be sent to the secondly-connected client B, <strong>even if A and B don't know their respective IP</strong>:</p>
  
  <pre><code>A -----------&gt; server &lt;----------- B     # they both connect the server first
  A --"hello"--&gt; server                    # A sends a message to server
                 server --"hello"--&gt; B     # the server sends the message to B
  </code></pre>
  
  <p>This code is currently working:</p>
  
  <pre><code># server.py
  import socket, time
  from threading import Thread
  socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  socket.bind(('', 5555))
  socket.listen(5)
  buf = ''
  i = 0
  
  def handler(client, i):
      global buf
      print 'Hello!', client, i 
      if i == 0:  # client A, who sends data to server
          while True:
              req = client.recv(1000)
              buf = str(req).strip()  # removes end of line 
              print 'Received from Client A: %s' % buf
      elif i == 1:  # client B, who receives data sent to server by client A
          while True:
              if buf != '':
                  client.send(buf)
                  buf = ''
              time.sleep(0.1)
  
  while True:  # very simple concurrency: accept new clients and create a Thread for each one
      client, address = socket.accept()
      print "{} connected".format(address)
      Thread(target=handler, args=(client, i)).start()
      i += 1
  </code></pre>
  
  <p>and you can test it by launching it on a server, and do two netcat connections to it: <code>nc &lt;SERVER_IP&gt; 5555</code>. </p>
  
  <p><strong>How can I then pass the information to the clients A and B that they can talk directly to each other without making the bytes transit via the server?</strong></p>
  
  <p>There are 2 cases:</p>
  
  <ul>
  <li><p>General case, i.e. even if A and B are not in the same local network</p></li>
  <li><p>Particular case where these two clients are in the same local network (example: using the same home router), this will be displayed on the server when the 2 clients will connect to the server on port 5555:</p>
  
  <pre><code>('203.0.113.0', 50340) connected  # client A, router translated port to 50340
  ('203.0.113.0', 52750) connected  # same public IP, client B, router translated port to 52750
  </code></pre></li>
  </ul>
  
  <hr>
  
  <p>Remark: a previous unsuccesful attempt here: <a href="https://unix.stackexchange.com/questions/421471/udp-or-tcp-hole-punching-to-connect-two-peers-each-one-behind-a-router">UDP or TCP hole punching to connect two peers (each one behind a router)
  </a> and <a href="https://unix.stackexchange.com/questions/422945/udp-hole-punching-with-a-third-party">UDP hole punching with a third party</a></p>