ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Hi
im trying to send files over a socket datagram in C between 2 clients.
Can anyone tell me whats the best way to "read" the file i want to send, and split it if necessary, and then send send it?
thks
You can use the read() method to read a file in chunks of (up to a) fixed size. Call it repeatedly, and send each packet, until you hit the end of the file. See the read manpage.
Assuming that you want to do this reliably, you'll also need some sort of CRC checksum in each packet to identify if it is corrupted in transit or not ,and a sequence number (because packets can become shuffled over longer routes). Alternativly, use TCP which handles this detail itself.
On the other side, you will need to read the file in in blocks, sort them into sequence (note that they are likely to be more-or-less in sequence), check the checksums, acknowledge the valid packets (use CRC checksums again), and then out the headers.
You should also resend packets after a timeout (see seek() and tell()) if its acknowledgement doesn't arrive, or is corrupted.
To generate a CRC checksum, I don't know of a method off-hand, but you might try searching for something suitable on sourceforge.net
thks
i have one more question... when i use the read() function, from what ive seen, i read it to a buffer of type char ? does it work files other
that text only files? like jpeg's etc... or do i have to read to somekind of byte stream...?
thks again :P
Sending files through UDP seems to be a very strange idea. I'm almost sure you want to send the files reliably, so to do this (as rjlee pointed you) you should use some sort of CRC, sort parts of files, request resending of lost datagrams (btw, UDP datagrams can be lost even if UDP is used to communicate between 2 programs running on the same host), and so on, e.g. you have to implement TCP.
Try to find IMHO the best book on socket API "UNIX network programming" by Richard Stevens. Reading it will defenitely help you in writing your program.
Originally posted by raszagal thks
i have one more question... when i use the read() function, from what ive seen, i read it to a buffer of type char ? does it work files other
that text only files? like jpeg's etc... or do i have to read to somekind of byte stream...?
thks again :P
To read non-text files, you generally specify unsigned char, but char will work just as well so long as you don't do any bitwise operations on it (<< >> & | or ^). read() itself doesn't care what type of buffer you pass it; it can even be a struct or a union so long as it's big enough to hold all the data.
hi again...
im having some trouble when reading the file i want to send. Im trying do build a message containing
in the first position the char 'F' ( for file) followed by file id ( an int ) a pkt_id ( an int ) and finally a block of data frm the file
im trying to send
i have this code just to try out adding info to the message im sending but it isnt writing anything to a new file.
if you could take a look... that would be great thks
if ((fp = fopen( infile, "rb")) == NULL){
printf("Error opening file %s for input.\n", infile);
return FAIL;
}
if ((fp = fopen("teste.o", "wb")) == NULL){
printf("Error opening file for input.\n");
return FAIL;
}
Assuming that both files open correctly, The fp pointer will now point to a structure representing the output file, teste.o. when you subsequently try to read from fp, you will be reading from the teste.o file, which will fail because the file is write-only.
You would have trapped this error if you had used errno.
Code:
#include <errno.h>
at the start of the file, then do
Code:
errno=0;
immediately before any standard I/O functions, like read or write. Then you test if errno==0 after the I/O; if not, an error has occurred and the value of errno (or the string message strerror() from string.h) will tell you what the error was.
You shouldn't assume that any I/O will be successful, because that isn't under the program's control; this applies to networked I/O as well as file or device I/O.
The result ferror() can also be used in place of errno (and is cleaner but potentially slower than errno), but you must still use it after each and every I/O call; if you do an I/O that fails followed by one that suceeds, without resetting the error state in between, then the error condition it returns could be anything.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.