title: OSError: [Errno 57] Socket is not connected
body: <p>I'm trying to understand networking on python using the <code>socket</code> module, but I think the problem is that I don't really understand how a server or client actually functions.</p>
  
  <p><strong>What I am trying to do:</strong></p>
  
  <ul>
  <li>Send a dictionary from the client to the server by turning it to a string first using JSON</li>
  <li>Make the server send back a message to the client to confirm they are connected</li>
  </ul>
  
  <p>Server program</p>
  
  <pre><code>import socket
  import json
  
  HOST = '192.168.X.X'
  PORT = 50034
  
  
  with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
      s.bind((HOST, PORT))
      s.listen(1)
      conn, addr = s.accept()
  
      with conn:
          while True:
              data = conn.recv(1024)
              new_data = data.decode()
              new_data = json.loads(data)
              print(new_data["Name"],"has connected from",addr)
              message = ("Hello %s welcome to the chat server" % new_data["Name"]).encode()
              s.sendall(message)
              if not data:
                  conn.sendall(data)
  </code></pre>
  
  <p>Client program </p>
  
  <pre><code>import socket
  import json
  
  HOST = '192.168.X.X'
  PORT = 50034
  
  my_dict = {"Name":"Bob"}
  jsn = json.dumps(my_dict)
  with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
      s.connect((HOST,PORT))
      s.sendall((jsn).encode())
      data = s.recv(1024)
      print(data)
  </code></pre>
  
  <p>But I am getting the error:</p>
  
  <blockquote>
    <p>OSError: [Errno 57] Socket is not connected</p>
  </blockquote>
  
  <p>Why?</p>