title: Send Video over TCP using OpenCV and sockets in Raspberry Pi
body: <p>I have been trying to send live video frame from my client (Raspberry Pi) to a server hosted on Laptop. Both these devices are connected to the same network.</p>
  
  <p>Server.py</p>
  
  <pre><code>import socket
  import sys
  import cv2
  import pickle
  import numpy as np
  import struct
  
  HOST = '192.168.1.3'
  PORT = 8083
  
  s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  print 'Socket created'
  
  s.bind((HOST, PORT))
  print 'Socket bind complete'
  s.listen(10)
  print 'Socket now listening'
  
  conn, addr = s.accept()
  
  data = ""
  payload_size = struct.calcsize("L")
  
  while True:
      while len(data) &lt; payload_size:
          data += conn.recv(4096)
      packed_msg_size = data[:payload_size]
  
      data = data[payload_size:]
      msg_size = struct.unpack("L", packed_msg_size)[0]
  
      while len(data) &lt; msg_size:
          data += conn.recv(4096)
      frame_data = data[:msg_size]
      data = data[msg_size:]
  
      frame=pickle.loads(frame_data)
      print frame.size
      # cv2.imshow('frame', frame)
      # cv2.waitKey(10)
  </code></pre>
  
  <p>Client.py</p>
  
  <pre><code>import cv2
  import numpy as np
  import socket
  import sys
  import pickle
  import struct
  
  cap = cv2.VideoCapture(0)
  clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  clientsocket.connect(('192.168.1.3', 8081))
  
  while True:
      ret,frame = cap.read()
      data = pickle.dumps(frame)
      clientsocket.sendall(struct.pack("L", len(data)) + data)
  </code></pre>
  
  <p><strong>My server on laptop is not receiving any data</strong>. But when I run this client and server on same devices (e.g. server and client on laptop) then it is working properly.</p>
  
  <p>I am able to send data from raspberry to laptop (tested for echo application).</p>
  
  <p>Can anyone help me with this ?</p>