How can I send a file through socket?

W

Water Lin

I want to write a socket communication for my study in C++.

Now the socket works, I can communication between server and client.

If I want to send a file from client to server, what should I do?

I think I need to read the file first, then put the file content to socket buffer, then send them. Is this sequence correct?

For txt file, I think it will be OK. But what will happen if I send exe file? How can I read exe file?

I am just a little confused about sending a file......

Water Lin
http://blog.waterlin.org
 
C

coal

I want to write a socket communication for my study in C++.

Now the socket works, I can communication between server and client.

If I want to send a file from client to server, what should I do?

I think I need to read the file first, then put the file content to socket buffer, then send them. Is this sequence correct?

I worked on something similar recently. This page --
http://webEbenezer.net/build_integration.html -- has a link
to a file called File.cc. In CalculateMarshallingSize it reads
a file into a buffer and then uses a bzip2 function to compress
the file. There's a function called Send that sends the
compressed file data.

For txt file, I think it will be OK. But what will happen if I send exe file? How can I read exe file?

I've not tried it with binary files.
I am just a little confused about sending a file......



Brian Wood
Ebenezer Enterprises
www.webEbenezer.net
 
W

Water Lin

Water Lin wrote:

I also need to ask another question.

When I read the file, I need to send it exactly the same as the original
one.

But if I use <ifstream> to read the file, the >> will stop at a
blankspace or newline.

So, is it possible to read a whole file at one time without any change?
 
J

James Kanze

Water Lin wrote:
I also need to ask another question.
When I read the file, I need to send it exactly the same as
the original one.

Are you sure? There are cases where this is true, but most
protocols have specific rules, and the file doesn't
necessisarily conform.
But if I use <ifstream> to read the file, the >> will stop at
a blankspace or newline.

So don't use >>.

Formally, there's no way in C++ to read a file literally.
Practically, if you open the file in binary mode and imbue the
"C" locale, you should be OK. (In theory, binary mode can
append any number of 0's to the end. In practice, I don't think
that this is a problem with any modern system.) And of course,
you have to do unformatted reads.
So, is it possible to read a whole file at one time without
any change?

If you have a big enough buffer. The usual solution is to read
fixed sized blocks, processing each block before reading the
next. (A professional program will usually use a pool of
buffers and non-blocking reads, but that requires some system
dependent code.)
 
M

Marcel Müller

James said:
If you have a big enough buffer. The usual solution is to read
fixed sized blocks, processing each block before reading the
next. (A professional program will usually use a pool of
buffers and non-blocking reads, but that requires some system
dependent code.)

With a look to C++0x this should change, since a second thread may do
the job without the need for an asynchronous I/O API.


Marcel
 
J

James Kanze

With a look to C++0x this should change, since a second thread
may do the job without the need for an asynchronous I/O API.

That's basically what I was suggesting, no. The thread doing
the processing doesn't block for the read. Using threads is
probably the simplest way to do it, although I suspect using the
system levelo asynchronous IO might be slightly more efficient.
 
J

James Kanze

James Kanze <[email protected]> kirjutas:
Just for curiosity: if a program uses mmap() or equivalent to
map the file (or parts to it) into memory and use it from
there without asynchronous IO, would it make it less
professional, in your opinion?

It depends on the context. If it was a pure copy program,
probably, since that would limit the size of the file being
copied, and probably run slower as well (although that depends
on the OS). But very few people are writing pure copy
programs---they were generally written a long time ago. For a
lot of uses, mmap is a very good solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top