Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File transfer over sockets

Status
Not open for further replies.

thepain009

Technical User
Oct 22, 2005
42
US
Hey guys,

I was wondering if someone could point me in the right direction with my project:

I'm to write a simple client and server programs that will use sockets. The client will be able to sent a GET <file name> command to the server and the server will send back the file (which does not have to be ASCII characters).

I have basic knowledge of sockets, so I can send text messages back and forth, but I can't figure out how to transfer files.

Any help would be greatly appreciated!
 
Thank you for your response,

I would like to write this program by myself, so I don't want to use the library.

Also, the paper on sockets that you linked me to doesn't have any examples that I'm looking for.

Let me describe my understanding of what I should do so far, so that someone could tell me if I'm on the right track:

1. At first, I need to establish a socket between the server and client
2. Set server socket to listen
3. Client will open a local file (.bmp lets say) using fread() and save the whole thing into buffer
4. Client then will write() the buffer into the socket
5. Server will accept() that buffer and using fwrite() it will recreate the bmp

Is this the right flow of action? Do I need to handle splitting the buffer into smaller pieces or will sockets handle that themselves?
 
> Also, the paper on sockets that you linked me to doesn't have any examples that I'm looking for.
That's not the job of the RFCs. They tell you what to send (and what to expect in return), not how.

How is in "beej"

> Do I need to handle splitting the buffer into smaller pieces or will sockets handle that themselves?
A bit of both really.
You can send as much as you like, but you always have to deal with it not being able to send it all. send() returns how many bytes were sent. If that's less than the total, then try again with the remainder of the message until it's all gone.

Likewise at recv(), you can supply a large buffer, but you'll need to do some reassembly of the message.

Other than that, your approach looks OK so far.

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top