4C.
TCP socket – File Transfer
Aim: To implement File Transfer Protocol (FTP) for transferring a file from server to client
application.
1. Create a file which has to be read by the server. Open a notepad, write the file contents
and save it in the program folder sample.txt.
2. Import the required io and net packages.
3. Initialized the socket class for communicate with the server with port number 4000.
4. Client specifies the file name to the server (eg. Sample.txt)
5. Initialize the BufferReader class to read the filename from the terminal.
6. Initialize the PrintWriter class to write and send the file name to the server through the
socket.
7. Initialize BufferReader class to read the file contents from the server.
8. Repeatedly read the data till end of file equals to empty.
9. Close the socket stream, filestream and BufferReader classes
10. End of the program.
Algorithm – Server
1. Import the required io and net packages.
2. Initialized the SeverSocket class and accept the client connection.
3. Initialize the BufferReader class for reading the client request (ie file name)
4. Initialize the FileReader class for reading the file.
5. Initialize Buffer Reader class for reading the file contents line by line.
6. Repeatedly read the file content line by line and send to the client through the socket S.
7. Close the socket stream, BufferedReader.
Program – Client
Program – Server
Output
Server side
Client side