Python Forum
Stream via socket with multiple clients
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stream via socket with multiple clients
#1
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 server.py and two clients (client-1.py and cliente-2.py).

My code is:

server.py

import socket
import cv2
import pickle
import struct 

HOST=''
PORT=8485

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 = 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)
    cv2.imshow('ImageWindow',frame)
    cv2.waitKey(1)
client.py

import cv2
import socket
import struct
import pickle

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('0.0.0.0', 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 = cam.read()
    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:
        break

cam.release()
cv2.destroyAllWindows()
As I said above, for the client-2.py I only use another port (the one that I previously write on the server).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Microphone stream manipulation Talking2442 1 2,717 Nov-19-2023, 02:08 PM
Last Post: palumanic
  EEG stream data with mne and brainfolw PaulC 0 487 Aug-22-2023, 03:17 AM
Last Post: PaulC
  Check if clients are online with ips stored in json [SOLVED] AlphaInc 6 2,458 Jun-27-2022, 08:28 AM
Last Post: AlphaInc
  Decoding a serial stream AKGentile1963 7 8,518 Mar-20-2021, 08:07 PM
Last Post: deanhystad
  Duplex Named Pipe with Python Server and C# Clients raybowman 1 2,388 Dec-03-2020, 09:58 PM
Last Post: Gribouillis
  Best Video Quality And Stream Harshil 2 2,234 Aug-19-2020, 09:03 AM
Last Post: Harshil
  stream audio from pc to pc floatingshed 2 1,970 Sep-16-2019, 03:45 PM
Last Post: floatingshed
  pi camera stream is upside down delta1071 3 5,736 Sep-11-2019, 11:35 AM
Last Post: metulburr
  Saving and accessing data from different clients SheeppOSU 1 1,970 Jul-28-2019, 02:15 AM
Last Post: metulburr
  Need help to read a gzip stream... pythonchakri 5 4,141 Jun-07-2019, 02:33 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020