title: How connect my Kivy client to a server (TCP, Sockets)
body: <p>So as part of my project (2D Multiplayer Card Game), I've figured out how to host and run a server script online. My plan is to have two separate kivy clients connect to the server (which will just be a script with commands). </p>
  
  <p>However I'm somewhat confused about the order of operations because I <em>think</em> the client connection is potentially in conflict with the message loop so I'm wondering if someone could basically tell me what I should be doing:</p>
  
  <p>I'm going to be using this as my serverscript:</p>
  
  <pre><code>import socket
  
  serversocket = socket.socket()
  
  host = 'INSERTIPHERE'
  port = PORTHERE
  
  
  serversocket.bind(('', port))
  
  serversocket.listen(1)
  
  while True:
      clientsocket,addr = serversocket.accept()
      print("got a connection from %s" % str(addr))
  
      msg = 'Thank you for connecting' + "\r\n"
      clientsocket.send(msg.encode('ascii'))
      clientsocket.close()
  </code></pre>
  
  <p>This is my client connection function</p>
  
  <pre><code>def Main():
      host = 'INSERTIPHERE'
      port = PORTHERE
  
     mySocket = socket.socket()
     mySocket.connect((host, port))
  
     message = input(' -&gt; ')
  
     while message != 'q':
             mySocket.send(message.encode())
             data = mySocket.recv(1024).decode()
             print('Received from server: ' + data)
             message = input(' -&gt; ')
  
    mySocket.close()
  </code></pre>
  
  <p>Note: I understand that the server and client aren't perfectly aligned in functions but provided I can at least a connection confirmation for now, I can work from there.</p>
  
  <p>I'm basically wondering how do I put this code into a simple kivy app like this:</p>
  
  <pre><code>from kivy.app import App
  from kivy.uix.boxlayout import BoxLayout
  
  class BoxWidget(BoxLayout):
      pass
  
  class BoxApp(App):
  
       def build(self):
          return BoxWidget()
  
  if __name__ == '__main__':
       BoxApp().run()
  </code></pre>
  
  <p>My best guess is that you want to:</p>
  
  <ul>
  <li>Establish the connection before opening the client</li>
  <li>Passing the server connection to the primary widget (in this case the Box Widget) as you're running an instance of the client (ie BoxApp(server).run()?)</li>
  <li>Use that connection in a message loop function of the BoxWidget</li>
  </ul>
  
  <p>I also understand that Kivy has built in solutions with Twisted but I'm having trouble with the python 2-3 differences. </p>
  
  <p>Thank you for reading. </p>
  
  <p>Just to clarify: All I want to do right now is open a blank window and also have a confirmation message sent to the command line (or failing that a label in the window).</p>