title: Send webcam stream from server in Python using sockets
body: <p>I am trying to send a webcam stream from the <code>server.py</code> file to the client.py file using Python sockets. I have it working in the other direction, but want to flip it (the when a client connects to a server, the server will feed webcam data from the server to the client). However, trying to run such results in an error and will only show the first frame of the stream, then crash... What can I do to fix this?</p>
  
  <p><strong>server.py</strong></p>
  
  <pre><code>import cv2
  import io
  import socket
  import struct
  import time
  import pickle
  import zlib
  
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.bind((socket.gethostname(), 1234))
  s.listen(5)
  
  cam = cv2.VideoCapture(0)
  
  cam.set(3, 320);
  cam.set(4, 240);
  
  img_counter = 0
  
  encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 100]
  
  while True:
      client_socket, address = s.accept()
      print(f"Connection from {address} has been established!")
  
      ret, frame = cam.read()
      result, frame = cv2.imencode('.jpg', frame, encode_param)
  
      data = pickle.dumps(frame, 0)
      size = len(data)
  
      print("{}: {}".format(img_counter, size))
      client_socket.send(struct.pack("&gt;L", size) + data)
      print("ooga")
      img_counter += 1
  
  cam.release()
  </code></pre>
  
  <p><strong>client.py</strong></p>
  
  <pre><code>import socket
  import sys
  import cv2
  import pickle
  import numpy as np
  import struct ## new
  import zlib
  
  HOST=''
  PORT=8485
  
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.connect((socket.gethostname(),1234))
  
  data = b""
  payload_size = struct.calcsize("&gt;L")
  print("payload_size: {}".format(payload_size))
  while True:
      while len(data) &lt; payload_size:
          print("Recv: {}".format(len(data)))
          data += s.recv(4096)
  
      print("Done Recv: {}".format(len(data)))
      packed_msg_size = data[:payload_size]
      data = data[payload_size:]
      msg_size = struct.unpack("&gt;L", packed_msg_size)[0]
      print("msg_size: {}".format(msg_size))
      while len(data) &lt; msg_size:
          data += s.recv(4096)
      frame_data = data[:msg_size]
      data = data[msg_size:]
  
      frame=pickle.loads(frame_data, fix_imports=True, encoding="bytes")
      frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
      cv2.imshow('ImageWindow',frame)
      cv2.waitKey(1)
  
  </code></pre>