2

everything should be alright, but received file is always being damaged the data is matched without any difference remove the hash tag from print(data) if you want to see binary and compering by yourself .................................................................... ....................................................................

server.py

import socket, threading, os
from time import sleep


host, port = '127.0.0.1', 442


class transfer :
    mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    mysocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    def __init__(self):
        self.mysocket.bind((host, port))
        print(' Server is ready ..')
        self.mysocket.listen(5)
        conn, addr = self.mysocket.accept()

        file_name = 'test.webm'
        size = os.path.getsize(file_name)
        print(' file size : {}'.format(str(size)))

        send_thread = threading.Thread(target = self.send_file, args=(file_name, size, conn, addr, ))
        send_thread.start()

    def send_file(self, file_name, size, conn, addr):
        with open(file_name, 'rb') as file:
            data = file.read(1024)
            conn.send(data)
            while data != bytes(''.encode()):
                #print(data)
                data = file.read(1024)
                conn.send(data)

            print(' File sent successfully.')


Transfer = transfer()

client.py

import socket, sys, threading

from time import sleep

host, port = '127.0.0.1', 442


class recv_data :
    mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    mysocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    mysocket.connect((host, port))

    def __init__(self):
        data = self.mysocket.recv(1024)
        f = open('newfile.webm', 'wb')
        while data != bytes(''.encode()):
            #print(data)
            data = self.mysocket.recv(1024)
            f.write(data)


re = recv_data()
7
  • I would replace the while data != bytes(''.encode()): with while len(data) > 0: Commented Feb 15, 2018 at 17:24
  • nice edit but i need to solve the problem Commented Feb 15, 2018 at 19:08
  • Just while data: works as well. Commented Feb 15, 2018 at 20:29
  • socket.send does not guarantee all bytes are sent. You have to check the return value. You can also try .sendall(). Commented Feb 15, 2018 at 20:30
  • so many ways to limit the loop but its not the problem sendall() didn't do much Commented Feb 15, 2018 at 20:49

1 Answer 1

2

In the client here:

def __init__(self):
    data = self.mysocket.recv(1024)
    f = open('newfile.webm', 'wb')
    while data != bytes(''.encode()):
        #print(data)
        data = self.mysocket.recv(1024)
        f.write(data)

the program never writes the result of the first recv into the file. That data is thrown away and replaced by the result of the second recv, which becomes the first data written into the file.

To fix, move the f.write above the second recv call.

Sign up to request clarification or add additional context in comments.

1 Comment

what a logic ^_^ i didn't notice that , its working now thanx alot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.