I wanted to create a software that can send video through socket programming. I can send 1 video at a time but when I want to send more than 2, it is stuck.
Below is the server code:
import socket
import os
IP = "127.0.0.1"
PORT = 4456
SIZE = 1024
FORMAT = "utf"
SERVER_FOLDER = "video_folder"
def main():
    print("[STARTING] Server is starting.\n")
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind((IP, PORT))
    server.listen()
    print("[LISTENING] Server is waiting for clients.")
    conn, addr = server.accept()
    print(f"[NEW CONNECTION] {addr} connected.\n")
    """ Receiving the folder_name """
    folder_name = conn.recv(SIZE).decode(FORMAT)
    """ Creating the folder """
    folder_path = os.path.join(SERVER_FOLDER, folder_name)
    if not os.path.exists(folder_path):
        os.makedirs(folder_path)
        conn.send(f"Folder ({folder_name}) created.".encode(FORMAT))
    else:
        conn.send(f"Folder ({folder_name}) already exists.".encode(FORMAT))
    """ Receiving files """
    while True:
        msg = conn.recv(SIZE).decode(FORMAT)
        """ Recv the file name """
        print(f"[CLIENT] Received the filename: {msg}.")
        file_path = os.path.join(folder_path, msg)
        file = open(file_path, "wb")
        conn.send(f"{file_path} filename received.".encode(FORMAT))
        while True:
            msg = conn.recv(SIZE)  # it stuck here once the msg become nothing need help
            if not msg:
                conn.send("The data is saved.".encode(FORMAT))
                break
            file.write(msg)
            print('.', end='', flush=True)
        if not msg: break
    file.close()
    print("\ndone.")
def test():
    while True:
        x = "World"
        print("morning")
        while True:
            print("Hello" + x)
            break
if __name__ == "__main__":
    main()
Below is the client code:
import socket
import os
IP = "127.0.0.1"
PORT = 4456
SIZE = 1024
FORMAT = "utf"
CLIENT_FOLDER = "C:/Users/wende/OneDrive/Desktop/client_folder"
def main():
    """ Staring a TCP socket. """
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect((IP, PORT))
    """ Folder path """
    path = os.path.join(CLIENT_FOLDER, "files")
    folder_name = path.split("/")[-1]
    """ Sending the folder name """
    msg = f"{folder_name}"
    print(f"[CLIENT] Sending folder name: {folder_name}")
    client.send(msg.encode(FORMAT))
    """ Receiving the reply from the server """
    msg = client.recv(SIZE).decode(FORMAT)
    print(f"[SERVER] {msg}\n")
    """ Sending files """
    files = sorted(os.listdir(path))
    for file_name in files:
        """ Send the file name """
        print(f"[CLIENT] Sending file name: {file_name}")
        client.send(file_name.encode(FORMAT))
        """ Recv the reply from the server """
        msg = client.recv(SIZE).decode(FORMAT)
        print(f"[SERVER] {msg}")
        """ Send the data """
        file = open(os.path.join(path, file_name), "rb")
        file_data = file.read()
        client.send(file_data)
        print("Sending File...")
        msg = client.recv(SIZE).decode(FORMAT)
        print(f"[SERVER] {msg}")
if __name__ == "__main__":
    main()
I have found out where the code is stuck, but I have no idea why it pauses there for no reason.
The problem I found is I keep loop the received at the server and once the received part is NULL then the loop will stop, but the problem I faced is once the received part is NULL, it cannot jump to the next if statement to check. I try transfer 1 video is no problem at all.
How can I solve this?
 
     
    