• C

udp file transfer


I am fairly new to this but I have managed to get a basic message passing client - server program working fine but I would now like to make it pass a file from client to the server.

I was wondering if any of you could give me some guideance on the structre and functions i need.

Who is Participating?
Hi devonflyers,

> I would now like to make it pass a file from client to the server.
Sounds like homework assignment so I am not giving you the code, only the guidelines ...

Code is essentially the same. In your current program, you would be packing your buffer and executing a send()/sendto().
The only difference now will be that you wil have to do this in a loop ... something like

open file
while (there is input to read)
        read next BUFFERSIZE bytes into buffer
        send (buffer)
end loop
close file

There are a couple of issues
1. How would receiver know that file transfer is complete?
You can either send the length of data in first packet that you send or you can use some sentinnel sequence of characters to indicate end of transfer

2. You sre using UDP which is by very nature unreliable. If one of the packets gets lost, receiver would never be able to reconstruct the file. Also, depending on the packet that gets lost, it may not be able to detect end of transfer or beginning of file transfer. Consider shifting to TCP if your problem definition permits that.

if you want to use udp for transfer, you must think that the packets might arrive out of order too, as udp doesn't guarantee the order of the packets, so you might want to create a special header inside your packet containing a sequence number (to know what number this packet has - this way you'll also know if you've lost some packets) and perhaps some flags, if you'd like to implement a protocol (e.g. flags for requesting retransmission of a lost packet).
If you are going to take so much pain, why not use time tested TCP instead :-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.