title: create loop to switch from one socket to another
body: <p>So I have successfully created a socket connection to one client and another, but I am having trouble getting them to switch from one another with loops, did some while true and if statements to make the program recognize when one system wants to switch based on user input but I don't think I'm doing it right. Can some one help me out with a code to switch back and forth</p>
  <p>The following is the code I'm attempting to implement.</p>
  <p>This is the code on my computer:</p>
  <pre><code>import socket,sys,os,time
  T2='yourturn'
  
  serverAddr = ('192.168.0.120', 20104)
  
  client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      #client.connect(serverAddr)
  sock = client
  client.connect(('192.168.0.120', 20104))
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  def redirectOut(port=20104, host='192.168.0.11'):
          &quot;&quot;&quot;
          connect caller's standard output stream to a socket for GUI to listen
          start caller after listener started, else connect fails before accept
          &quot;&quot;&quot;
          
          sock = client
                          # caller operates in client mode
          file = sock.makefile('w')                 # file interface: text, buffered
          sys.stdout = file 
                      # make prints go to sock.send
          return sock
          
  
  
  ########################################33333
  
  time.sleep(10)
  
  HOST = ''
  PORT2 = 20105
  
  
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  print('Socket created')
  
  try:
          l=s.bind((HOST, PORT2))
  except socket.error as msg:
          print('Bind failed. ')
          sys.exit()
  
  print('Socket bind complete')
  
  
  s.listen(10)
  print('Socket now listening')
  
  conn, addr = s.accept()
  print('Connected to ' + addr[0] + ':' + str(addr[1]))
  
  
  
  
  
  
  ####################################################
  
  time.sleep(10)
  
  
  
  
  
  while True:
      if T2!='yourturn':
          data = conn.recv(1024)
          line = data.decode    # convert to string (Python 3 only)
          T2=print(line)
      else :
          if T2=='myturn':
              break
          else:
              redirectOut()
              T2=print(input())
  </code></pre>
  <p>this is the code on my begalbone black:</p>
  <pre><code>import socket
  import sys
  import os, time
  
  
  
  HOST = ''
  PORT2 = 20104
  T='yourturn'
  
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  print('Socket created')
  
  try:
          l=s.bind((HOST, PORT2))
  except socket.error as msg:
          print('Bind failed. ')
          sys.exit()
          
  
  
  print('Socket bind complete')
  
  
  s.listen(10)
  print('Socket now listening')
  
  conn, addr = s.accept()
  print('Connected to ' + addr[0] + ':' + str(addr[1]))
  
  
  #################################################################
  time.sleep(15)
  serverAddr = ('192.168.0.11', 20105)
  
  client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  sock = client
  
  try:
    sock.connect(('192.168.0.11', 20105))
    fsr = 'P9_40'
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  except TimeoutError:
    os.system('reboot')
  
  
  def redirectOut(port=20105, host='192.168.0.120'):
      &quot;&quot;&quot;
      connect caller's standard output stream to a socket for GUI to listen
      start caller after listener started, else connect fails before accept
      &quot;&quot;&quot;
      sock = client
                      # caller operates in client mode
      file = sock.makefile('w')                 # file interface: text, buffered
      sys.stdout = file                         # make prints go to sock.send
      return sock 
  
  
  
  
  ##############################################################
  
  time.sleep(10)
  
  
  
  while True:
      
        if T!='myturn':
          data = conn.recv(1024)
          line = data.decode('UTF-8')    # convert to string (Python 3 only)
          T=print(line)
        else:
          redirectOut()
          if T=='yourturn':
              break
          else:
              T=print(input())
  </code></pre>
  <p>So tried this while loop: but its still hanging up, I think I'm close:</p>
  <pre><code>T2='yourturn'
  while True:
   #for line in 'Python':
      print(T2)
      time.sleep(10)
      if T2=='myturn':
          data = conn.recv(1024)
          line = data.decode 
          print(&quot;myturn print&quot;)
          l=print(line)
          if l==&quot;yourturn&quot;:
              continue
      if T2==&quot;yourturn&quot; or T2!='myturn':
          print(T2)
          print('myturn send')
          redirectOut()# convert to string (Python 3 only)
          k=input()
          if k==&quot;myturn&quot;:
              T2='myturn'
              continue
  </code></pre>
  <p>Tried the folowing but reciving machine is hanging up when myturn is input:</p>
  <p>sending:</p>
  <pre><code>time.sleep(3)
  T2='yourturn'
  print('here')
  while True:
   #for line in 'Python':
      #print(T2)
      #time.sleep(10)
      if T2=='myturn':
          data = conn.recv(1024)
          line = data.decode('UTF-8')
          #print(&quot;myturn print&quot;)
          l=print(line)
          if l==&quot;yourturn&quot;:
              T2='yourturn'
              continue
      if T2==&quot;yourturn&quot; and T2!='myturn':
          #print(T2)
          #print('myturn send')
          redirectOut()# convert to string (Python 3 only)
          k=input()
          print(k)
      if k==&quot;myturn&quot;:
          T2=print('myturn')
          T2='myturn'
          print('there')
          continue
  </code></pre>
  <p>receiving:</p>
  <pre><code>time.sleep(3)
  T='yourturn'
  while True:
   #for line in 'Python':
      #print(T)
  
      if T=='yourturn':
          data = conn.recv(1024)
          line = data.decode('UTF-8')
          #print(&quot;myturn print&quot;)
          l=print(line)
          if l=='myturn':
            T='myturn'
            continue
          if l=='exit':
            client.close()
            break
      if T=='myturn' and T!='yourturn':
          #print('myturn send')
          redirectOut()# convert to string (Python 3 only)
          k=input()
          print(k)
          if k=='yourturn':
            T=print('yourturn')
            continue
  </code></pre>
  <p>EDIT SOLUTION:</p>
  <p>I finally figured it out, removed my reditectOut function and opened the port with windows firewall to implement this code</p>
  <p>my computer:</p>
  <pre><code>T2='yourturn'
  while True:
      if T2=='myturn':
          data = conn.recv(1024)
          line = data.decode('UTF-8')
          print(line)
          l=line
          if l==&quot;yourturn&quot;:
              T2='yourturn'
              continue
      if T2==&quot;yourturn&quot; and T2!='myturn':
          k=input()
          client.sendto(k.encode('utf-8'),('192.168.0.120', 20104))
      if k==&quot;myturn&quot;:
          T2='myturn'
          continue
  </code></pre>
  <p>Beagle bone black:</p>
  <pre><code>time.sleep(3)
  T='yourturn'
  while True:
      if T=='yourturn':
          data = conn.recv(1024)
          line = data.decode('UTF-8')
          print(line)
          l=line
          if l=='myturn':
            T='myturn'
            continue
      if T=='myturn' and T!='yourturn':
          k=input()
          client.sendto(k.encode('utf-8'),('192.168.0.11', 20105))
          if k=='yourturn':
            T='yourturn'
            continue
  </code></pre>