1

I am trying to code a simple share screen script. For some reason it take a lot of time for the server to receive the message. And I dont know what is the problem. If you have a solution, or a way to speed the process up it be very helpful. Any way I am appreciate your help.

server:

import socket
import pickle
import select
import numpy
import cv2

def receive_msg(socket, HEADERSIZE):#receive message
    try:
        readySockets, _, _ = select.select([socket], [], [], 0.02)
        if readySockets:
            msgLen = socket.recv(HEADERSIZE)#receive the header/the message length
                
            msgLen = int(msgLen)#convert fron bytes to int
            msg = socket.recv(msgLen)#resive the size of the message
                
            while len(msg) < msgLen:#if dont receive the full message / if the size of the message smaller than the size that the message sepose to be 
                msg += socket.recv(msgLen - len(msg))#add to the message the part that missing (the full size of the message - the size of the message that the program received)
                        
            msg = pickle.loads(msg)#extract message
        else:
            msg = False
    except: 
        msg = False
        
    return msg#return the complite massage

HEADERSIZE = 10

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), 1243))
s.listen(5)
    
clientsocket, address = s.accept()

while True:
    # now our endpoint knows about the OTHER endpoint.
    msg = receive_msg(clientsocket, HEADERSIZE)
    if msg is not False:
        image = cv2.cvtColor(msg, cv2.COLOR_RGB2BGR)#numpy array to open cv image
        cv2.imshow("image", image)#show image
        
        cv2.waitKey(1)

client:

import socket
import numpy as np
import pyautogui
import pickle

def get_screen():
    img = pyautogui.screenshot()# take a screenshot
    
    img = np.array(img)#from image to array
    return img

def send_msg(socket, msg, HEADERSIZE):#send a message
        msg = pickle.dumps(msg)#comprass the msg
        #give the mag a header / signature of the size of the message 
        msg = bytes(str(len(msg)) + (" " * (HEADERSIZE - len(str(len(msg))))), 'utf-8') + msg

        socket.send(msg)#send the msg

HEADERSIZ = 10

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((socket.gethostname(), 1243))

while True:
    send_msg(s, get_screen(), HEADERSIZ)#send a image of the screen to the server
  • How long *does* it take? How large is your screen? How much bandwidth is there from client to server? – MisterMiyagi Aug 24 '20 at 17:00
  • There is a delay of 5.4 seconds. The screen is HD. And every message is 6220964 bytes. – SDSD.dsfsfd Aug 24 '20 at 17:02
  • Is that the latency (time between send/recv) or delay (time between multiple sends)? – MisterMiyagi Aug 24 '20 at 17:07
  • is the time between multiple sends – SDSD.dsfsfd Aug 24 '20 at 17:09
  • 1
    Okay, so did you validate that the delay is actually from *transmitting* the image, instead of of capturing, converting, packing, and pushing the image to the socket? – MisterMiyagi Aug 24 '20 at 17:11
  • How are you measuring the time delay? With a stopwatch? You should add some timestamps between all the different operations to establish how much time each operation takes. e.g. before and after screenshot, before and after numpy conversion, before and after pickling, before the send call, before the receive, at each piece of the message, before and after the unpickling. Each operation takes time,as @MisterMiyagi said. – RufusVS Aug 24 '20 at 18:10

0 Answers0