Stream via socket with multiple clients

The code that I have already works. What I do is create two servers and two clients to open two different codes with different ports, because when I use the same port I get an error. But, the problem is the problem is that two windows open and that makes me think that consumption is higher (or am I wrong?). What I am looking for is to consume as little as possible, that is, to have one and two clients ( and

My code is:

import socket
import cv2
import pickle
import struct 


print('Socket created')

print('Socket bind complete')
print('Socket now listening')


data = b""
payload_size = struct.calcsize(">L")
print("payload_size: {}".format(payload_size))
while True:
    while len(data) < payload_size:
        print("Recv: {}".format(len(data)))
        data += conn.recv(4096)

    print("Done Recv: {}".format(len(data)))
    packed_msg_size = data[:payload_size]
    data = data[payload_size:]
    msg_size = struct.unpack(">L", packed_msg_size)[0]
    print("msg_size: {}".format(msg_size))
    while len(data) < msg_size:
        data += conn.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)

import cv2
import socket
import struct
import pickle

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('', 8485))
connection = client_socket.makefile('wb')

cam = cv2.VideoCapture(0)

cam.set(3, 320)
cam.set(4, 240)

img_counter = 0

encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]

while True:
    ret, frame =
    result, frame = cv2.imencode('.jpg', frame, encode_param)
#    data = zlib.compress(pickle.dumps(frame, 0))
    data = pickle.dumps(frame, 0)
    size = len(data)

    print("{}: {}".format(img_counter, size))
    client_socket.sendall(struct.pack(">L", size) + data)
    img_counter += 1
    c = cv2.waitKey(1)
    if c == 27:


As I said above, for the I only use another port (the one that I previously write on the server).

socket programming is a general topic. you should ask that in a python forum, not in this forum, which is specific to OpenCV issues.

1 Like

Thanks, I will do it.

But, if there is anyone else who can help me, I will appreciate it.

imo, this is all wrong.
IF you want to write your own (you shouldnt !) you need to follow a common protocol (e.g. MJPEG)
the server should manage the camera, and it needs to accept more than 1 client, if you have multiple.


skip that effort. use ffmpeg to stream your cam to some rtsp url, and cv2.VideoCapture for the clients