Hi guys,
Do anyone here know how to send file via socket in unix? I need to use the client program to send file to server program. Any idea? If possible please provide me with some examples. thanks
Hi guys,
Do anyone here know how to send file via socket in unix? I need to use the client program to send file to server program. Any idea? If possible please provide me with some examples. thanks
this is a question for the networking forum
http://beej.us/guide/bgnet/
http://cboard.cprogramming.com/forumdisplay.php?f=28
http://cboard.cprogramming.com/showthread.php?t=41926
enjoy the reading.
"Hence to fight and conquer in all your battles is not supreme excellence;
supreme excellence consists in breaking the enemy's resistance without fighting."
Art of War Sun Tzu
What library are you using for Sockets, Berkley?
You may want to try this library. Should be possible, if not, I suppose fstream could just read the entire file into a string, and send it that way.
Moved to Networking/Device Communication board.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
yup Berkley.. fstream could just read the entire file into a string? I'm sorry but still very new with socket programming. I read the link that send a String "hello world " from http://beej.us/guide/bgnet/ but I was wondering how can I transfer a file? stead of just a string that print out hello world ? And also how can the server reply to the client ? if i'm going to transfer file to the server.
In the end, it's all just an array of bytes which you're sending from one machine to another.
You just have to make the first part of the message mean
- here is a file
- this is it's name
- this is how long it is
Then you send
- this is the file data.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Yup, something like:
Then just send the string as per usual.... As for server/client responses, have you thought about enums?Code:using namespace std; fstream f("Myfile", ios::in); string theFile=""; char buff[1024] while(!f.eof()) { f.get(buff, 1023); theFile += buff; buff=""; } f.close();
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
vart
can you expand on this please.check the return value of the input function
i'm new to networking code and i'm not sure what input function?
do you mean to do error (exception) checking on the
Code:f.get(buff, 1023);
"Hence to fight and conquer in all your battles is not supreme excellence;
supreme excellence consists in breaking the enemy's resistance without fighting."
Art of War Sun Tzu
Basically the f.get returns success or failure upon reading in the buffer from the file. So it is smarter to read as your break statement in the loop then check the eof of the filestream because that isn't set until you read past the end of the file so you can incur double reading of the last item or the stream could have failed for other reasons besides EOF.
Woop?
f.eof() returns true when the EOF has been reached. As your loop is setup, this could happen:
1) f.get() reads data
2) f.feof() returns false
3) f.get() reads an EOF, and fails.
4) The remainder of the loop proceeds to corrupt the string.
Also, note:
Since buff is a char array, this will only work for text files. This fails on binary files since std::string treats the pointer as a null terminated string.Code:theFile += buff; buff="";
The second line is just wrong because you're assigning a string literal to an array. (Shouldn't compile...)
There's no need to read the whole file in at once. (Indeed, doing so restricts the program to small files - what if I want to send a CD ISO?) Choose a buffer size, read as much as possible from the file to that buffer, and send it. Rinse, lather, repeat. 4KB, 16KB, etc buffers are probably fine. Remember: check for the amount of characters that were read from the file (fstream's gcount() ) and sent on the network (the return value of send() ) - although you may request a read or a send of some value, you might not get it. (Especially on the last read of the file, it'll probably be less.)
Also: pay attention to Salem's post on what must also be sent. Pay attention to byte ordering (network vs. host), and be sure the other end knows how much data you're sending, or how it could figure it out.
long time; /* know C? */
Unprecedented performance: Nothing ever ran this slow before.
Any sufficiently advanced bug is indistinguishable from a feature.
Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
The best way to accelerate an IBM is at 9.8 m/s/s.
recursion (re - cur' - zhun) n. 1. (see recursion)