working with files (splitting and combining) under UNIX C
Hi guys, I am new to the forums, so I will get straight to the point. I have to make a chat client/server and file transfer over udp program. I got the chat to work, but I am having some difficulties working with the file transfer as I am not too experienced working with files.
I need to split a file into chunks and then send them over the network, and recombine the chunks back into the file. I am using the gcc C in Unix. I found the following commands, but I am not sure if they will work.
long int ftell() and int fseek ().
any help would be great. Thanks
[588 byte] By [
nark] at [2007-11-18 19:28:12]

# 1 Re: working with files (splitting and combining) under UNIX C
Why do U want to transfer data over UDP here? U cannot send a file like a whole entity, using one packet (because ip packets has a fixed size). U have to send it piece by piece. UDP doesn't care about data loosing, if some peace of file is lost (that's a pretty common case with UDP), a reciever wont get the completed filr. In that case, U should build your own high level to preserve such kind of loosing. I would suggest U to use TCP for file transferring, that will take less codding from U.
Remember that read()/write()/send()/recv() operations can send/get less data than U have asked. (Read man pages for them).
U have no need to use ftell(), lseek() - just read the file piece by piece.
Some pseudo-code for send file operation. sock - opened socket (use sendmsg() for udp) :
int bytes, sb, pos;
char buf[16384];
int fd = open(file, O_RDONLY);
if (fd == -1) {
perror("open");
return -1;
}
while ((bytes = read(fd, buf, sizeof(buf)) > 0) { /* read until the end of file*/
pos = 0;
while (bytes > 0) {
/* need a cicle here, because send() can place less data that we ask */
sb = send(sock, buf + pos, bytes);
bytes -= sb;
pos += sb;
}
}
close(fd);
# 2 Re: working with files (splitting and combining) under UNIX C
ahh thanks. Yeah, well the gsis thought it would be funny to have us implement file transfer over UDP. We basicly have to implement window size and reliability, however, congestion control we dont need to worry about. What about recombining the pieces back into one file?
nark at 2007-11-9 0:33:42 >

# 3 Re: working with files (splitting and combining) under UNIX C
Well, that's your turn. The reciever side is not much more difficult that this one. If this's your homework or smth like this, then put your hands on the keabord and write smth ;)
# 4 Re: working with files (splitting and combining) under UNIX C
Originally posted by dimm_coder
Well, that's your turn. The reciever side is not much more difficult that this one. If this's your homework or smth like this, then put your hands on the keabord and write smth ;)
;p thanks.
nark at 2007-11-9 0:35:35 >

# 5 Re: working with files (splitting and combining) under UNIX C
although the task is not too challenging when considering to just send the file chunks via UDP, assuring the file's integrity is a challenge. Since UDP is not reliable in the sense that 2 packets sent are delivered to the receiver in the same order and that packets may be lost you will have to add your own reliability such as a sequence number in front of each packet. How would you deal with lost packets? ...
# 6 Re: working with files (splitting and combining) under UNIX C
Originally posted by Richard.J
although the task is not too challenging when considering to just send the file chunks via UDP, assuring the file's integrity is a challenge. Since UDP is not reliable in the sense that 2 packets sent are delivered to the receiver in the same order and that packets may be lost you will have to add your own reliability such as a sequence number in front of each packet. How would you deal with lost packets? ...
well thats one of the major points of the project. Most likely resend them. I was thinking of having a window size of maybe 100 packets. Then if say packets 5,6, 8, 9 are lost. The other end can send one ack with the packets that were lost.
also, another question. I have to open the file for reading, so I need to use the "r" mode. However, I can do either "rt" or "rb" so what is the difference between a text file (rt) and a binary file (rb)?
nark at 2007-11-9 0:37:38 >

# 7 Re: working with files (splitting and combining) under UNIX C
Originally posted by nark
well thats one of the major points of the project. Most likely resend them. I was thinking of having a window size of maybe 100 packets. Then if say packets 5,6, 8, 9 are lost. The other end can send one ack with the packets that were lost.
That forces U to add an additional logical level to your applications. That's possible of course, but can be a horrible thing under some circumstanecs (and can affect the application's performance too).
also, another question. I have to open the file for reading, so I need to use the "r" mode. However, I can do either "rt" or "rb" so what is the difference between a text file (rt) and a binary file (rb)?
according to "man fread"
The mode string can also include the letter ``b'' either as a last
character or as a character between the characters in any of the two-
character strings described above. This is strictly for compatibility
with ANSI X3.159-1989 (``ANSI C'') and has no effect; the ``b'' is
ignored on all POSIX conforming systems, including Linux. (Other sys-
tems may treat text files and binary files differently, and adding the
``b'' may be a good idea if you do I/O to a binary file and expect that
your program may be ported to non-Unix environments.)
Is it clear for U?
# 8 Re: working with files (splitting and combining) under UNIX C
In a TCP environment, you would negotiate a window size. Each side then has to observe not to exceed this window when sending packets. The receiving side must then acknowledge the received packets and the sender can send more packets. The details are really painful to implement.