title: Python: Errno 57 when calling Server function in GUI
body: <p><strong>Question</strong></p>
  
  <p>Why am I receiving <code>error: [Errno 57] Socket is not connected</code>, even though I have initialized the socket? The full code is <a href="http://pastebin.com/gEXBMFMi" rel="nofollow">on paste bin</a>, but feel free to check out what I have provided here.</p>
  
  <p><strong>Background</strong></p>
  
  <p>I am creating a basic GUI for a socket server. I am having issues in making the two parts of the script (GUI and Server) communicate. The server part is written functionally, while the Tkinter GUI is in OOP. The GUI calls parts of the server to run. The server is flawless, but whenever I call the function <code>send_msg</code>(which uses the socket to send a message) I am given the error.</p>
  
  <pre><code>[Errno 57] Socket is not connected
  </code></pre>
  
  <p><strong>Code</strong></p>
  
  <p>In the <code>GUI</code> <code>__init__</code> method, I call the <code>start_up()</code> function. The function opens the socket and binds it to the respective address. Here is the function:</p>
  
  <pre><code>def start_up():
      HOST = socket.gethostname()
      PORT = 2468
  
      print "HOST: %s" % (HOST)
      print "PORT: %s" % (PORT)
      print "\n"
      print "-------------------------"
      print "\n"
      global s
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM )
      s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
      print "Socket Created"
  
      try:
          s.bind((HOST, PORT))
      except socket.error, msg:
          print "Bind failed. Error Code : " + str(msg[0]) + " Message " + str(msg[1])
          sys.exit()
      print "Socket Bind Complete"
      """
      So by specifying 10, it means that if 10
      connections are already waiting to be processed,
      then the 11th connection request shall be rejected.
      This will be more clear after checking socket_accept.
      """
      s.listen(10)
      print "Socket now listening"
  </code></pre>
  
  <p>Whenever I press the <code>self.send</code> button, it calls the <code>class_send_msg</code> method, which in turn calls the <code>send_msg(s)</code> function. Here is the <code>send_msg</code> code:</p>
  
  <pre><code>def send_msg(s):
      #Send some data to the remote server
      message = app.message.get("0.0", END)
  
  
       #set the whole string
      s.sendall(message)
      myHistory = open("history.txt", "a+")
      myHistory.write(message)
  </code></pre>
  
  <p>And lastly, the console messages that I receive (with traceback):</p>
  
  <pre><code>HOST: ****.local
  PORT: 2468
  
  
  -------------------------
  
  
  Socket Created
  Socket Bind Complete
  Socket now listening
  Exception in Tkinter callback
  Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1410, in __call__
      return self.func(*args)
    File "/Volumes/****'S USB/Python Programs/Sockets/IM Project/Server/Server GUI InDev Class.py", line 160, in class_send_msg
      send_msg(s)
    File "/Volumes/****'s USB/Python Programs/Sockets/IM Project/Server/Server GUI InDev Class.py", line 97, in send_msg
      s.sendall(message)
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth
      return getattr(self._sock,name)(*args)
  error: [Errno 57] Socket is not connected
  </code></pre>