0

I tried to create a client server script for sending and receiving some files. The scenario is that initially Server sends automatically a csv file to client, client process the file and sends back an answer file.

When I start receiving the file I can see all the contents of the file but is not doing a break to continue to the next function for sending back the answer file Is server part problem or client or both?

Client

#GET THE WORK FILE
with open('received_file.csv', 'wb') as f:
    print ('file opened')
    while True:
        data = s.recv(BUFFER_SIZE)
        print('receiving data...')
        print('data=%s', (data))
        #data =''
        if not data:
            print('Successfully get the file')
            break

        f.write(data)
    f.close()

Only if I place data=' ' it goes next but shouldn't server sends an empty data packet? It wouldn't work if a received file is over buffer size correct?

Server

while True:
    print('Starting is ',starting)
    l = f.read(BUFFER_SIZE)
    while (l):
        self.sock.send(l)
        print('Sending..') 
        l = f.read(BUFFER_SIZE)
        if not l:
            f.close()
            starting = 0
            del filenames[0]
            print('Sending in over..')

            self.getfile()
Nic Wanavit
  • 2,363
  • 5
  • 19
  • 31
Evridiki
  • 323
  • 3
  • 16
  • You are assuming that `recv` will return with no data if all data are read. This is not the case - it will just wait for more data since it does not know when the data will end. Possible duplicate of [How does the python socket.recv() method know that the end of the message has been reached?](https://stackoverflow.com/questions/41382127/how-does-the-python-socket-recv-method-know-that-the-end-of-the-message-has-be) – Steffen Ullrich May 05 '19 at 10:18
  • So if I place at the end of received data a NULL or something and check for that? – Evridiki May 05 '19 at 10:27
  • How exactly you figure out the end of the message is up to your specific application. You might have some end-of-message marker and check for it (which is impossible for binary data unless this marker is somehow escaped or encoded in the data) or you might add some prefix to the message which contains the length of the following payload. Use what fits your use case best - I myself prefer a fixed size length prefix since it is simpler to implement (no need to scan all received data for end-of-data marker), works with binary data and also since this way the length of the message is known early. – Steffen Ullrich May 05 '19 at 10:36
  • Can you give me an example of a fixed size length prefix? – Evridiki May 05 '19 at 11:56
  • 1
    `struct.pack("H",len(data)) + data` prefixes the data by a 2 byte length – Steffen Ullrich May 05 '19 at 16:43

1 Answers1

0

I think the problem is that the break command is exiting the if statement instead of the while loop that you intend to break.

Here is a possible solution

Client

with open('received_file.csv', 'wb') as f:
        print ('file opened')
        while_loop_status=True
        while while_loop_status:
            data = s.recv(BUFFER_SIZE)
            print('receiving data...')
            print('data=%s', (data))
            if not data:
                print('Successfully get the file')
                while_loop_status=False
            f.write(data)
        f.close()

Here you create a variable that determines whether to continue the while loop called "while_loop_status" then you change it from True to False when you need to break the loop.

poon gilbert
  • 368
  • 3
  • 5
Nic Wanavit
  • 2,363
  • 5
  • 19
  • 31