sending multiple images using socket python get sent as one to client

2024/9/20 12:32:24

I am capturing screenshots from the server, then sending it to the client, but the images get all sent as one big file to the client that keeps expanding in size. This only happens when i send from one machine to another (I am working on a local netwrok) but when running both client and server from my machine they work fine. Note: for the client on the other machine, I packaged it into an exe using pyinstaller, since this machine does not have python.

server code:

host="192.168.43.79"                # Set the server address to variable host
port=4446                   # Sets the variable port to 4446
import time
import pyautogui
from socket import *
import os
s=socket(AF_INET, SOCK_STREAM)s.bind((host,port))                 print("Listening for connections.. ")q,addr=s.accept()i = 0                      
while True:screenshot = pyautogui.screenshot()screenshot.save(str(i) + ".jpg")with open(str(i) + ".jpg", "rb") as f:data = f.read(4096)while data:q.send(data)data = f.read(4096)q.send(b"full")i += 1time.sleep(0.3)           

client code:


host="192.168.43.79"            # Set the server address to variable hostport=4446               # Sets the variable port to 4446from multiprocessing.reduction import recv_handle
from socket import *             # Imports socket modules=socket(AF_INET, SOCK_STREAM)      # Creates a socket
s.connect((host,port)) i = 0
while True:with open(str(i) + "s.jpg", "wb") as f:recv_data = s.recv(4096)            while recv_data:f.write(recv_data)recv_data = s.recv(4096)if(recv_data == b"full"):breaki += 1
Answer

There various wrong assumptions here which lead to the problem you see. The wrong assumptions are:

  • that send(data) will write all data
    It might send less. You need to check the return value or use sendall to be sure.
  • that a single send in the sender is matched by exactly a single recv in the recipient
    TCP is only an unstructured byte stream. send does not add message semantics, so a single send might lead to multiple recv, multiple send might lead to a single recv etc. Specifically send("data") followed by send("full") might be recv(4096) as "datafull", thus missing your code to detect end of image.

As for why does it work on the local machine but not on the remote - the chance in the latter case is higher that send get combined together and recv as one.

https://en.xdnf.cn/q/119222.html

Related Q&A

What are the different methods to retrieve elements in a pandas Series?

There are at least 4 ways to retrieve elements in a pandas Series: .iloc, .loc .ix and using directly the [] operator.Whats the difference between them ? How do they handle missing labels/out of range…

Speaker recognition - Bad Request error on microsoft oxford

I am using the python wrapper that has been given in the SDK section. Ive been trying to enroll a voice file for a created profile using the python API.I was able to create a profile and list all profi…

Remove list of phrases from string

I have an array of phrases: bannedWords = [hi, hi you, hello, and you]I want to take a sentence like "hi, how are tim and you doing" and get this:", how are tim doing"Exact case mat…

ImportError: No module named... (basics?) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 10 years ago.Improv…

How to pass python list address

I want to convert c++ code to python. I have created a python module using SWIG to access c++ classes.Now I want to pass the following c++ code to PythonC++#define LEVEL 3double thre[LEVEL] = { 1.0l, 1…

Python open says file doesnt exist when it does

I am trying to work out why my Python open call says a file doesnt exist when it does. If I enter the exact same file url in a browser the photo appears.The error message I get is:No such file or direc…

How to map one list and dictionary in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.Improve…

Sorting date inside of files (Python)

I have a txt file with names and dates like this name0 - 05/09/2020 name1 - 14/10/2020 name2 - 02/11/2020 How can I sort the text file by date? so that the file will end up like this name2 - 02/11/202…

Dicing in python

Code:-df = pd.DataFrame({col1:t, col2:wordList}) df.columns=[DNT,tweets] df.DNT = pd.to_datetime(df.DNT, errors=coerce) check=df[ (df.DNT < 09:20:00) & (df.DNT > 09:00:00) ]Dont know why this…

How to collect all specified hrefs?

In this test model I can collect the href value for the first (tr, class_=rowLive), Ive tried to create a loop to collect all the others href but it always gives IndentationError: expected an indented …