java - Transferring files from Client to Server using TCP -
hello i'm doing school project , need make tcp server/client transfer files client server using tcp protocol.
i know how make tcp server , client sending messages , objects.
what need is:
- the user selects file predefined directory
- then can type send-file.ext send file server server needs to
- get file client
- save file in predefined directory
how go this? if fetch file users hard drive, how make file bytes , send server. how server know file , save file original name?
thanx
the first thing need define protocol. example...
- each connection server should represent single file.
- after connection occurs, client should pass file name first line.
- the client should pass size of file (in bytes) second line.
- the client should send contents of file.
- finally, connection should shut down.
now, have coding client. can read contents of file using fileinputstream. then, send meta data plus contents on socket using outputbuffer on socket.
finally, server. whenever client connects, know first 2 lines going be. so, read in first line , create new file based on file name. then, read in second line. finally, read x number of bytes socket , write bytes file, x size in bytes second line.
by having second line, know when done socket. plus, in case socket dies or blocks - whatever reason - know wrong transfer , can abort.
Comments
Post a Comment