Java File Transfer Protocol: Build a simple File Transfer Service that consists
ID: 3686203 • Letter: J
Question
Java File Transfer Protocol:
Build a simple File Transfer Service that consists of a client and server.
The server exports a set of files from the computer on which it runs to be downloaded on the client computer.
That is: a client requests a file from the server and the server responds to the client by sending the file.
Client will request one file at a time. Server will send the contents of the file followed by end-offile and then exits.
Client must save the file on the local computer with the same file name.
Server records all file download activities in a log file. You should build your MFTP on UDP ports using TCP.
The message from the client and the response from the server are transmitted as the content of UDP datagrams.
Each message is identified by a message code as follows:
message code meaning:
1 Request a file to be read from the server
2 The message contains data being transferred
3 The message acknowledges a data message
4 Reports an error [error message: File not found]
Format of the messages being transmitted between the client and the server are:
Read message:
1 File Name Zero Data message:
2 sequence number Data Bytes ........... Acknowledgement message:
3 sequence number Error message:
4 "File Not Found" zero
Explanation / Answer
Please find below the Server.java and Client.java program which can be used to send files:
Server.java:
package filesendtest;
import java.io.*;
import java.net.*;
class TCPServer {
private final String fileToSend = "D:\testing.jpg";
public static void main(String args[]) {
while (true) {
ServerSocket welcomeSocket = null;
Socket connectionSocket = null;
BufferedOutputStream outToClient = null;
try {
welcomeSocket = new ServerSocket(3248);
connectionSocket = welcomeSocket.accept();
outToClient = new BufferedOutputStream(connectionSocket.getOutputStream());
} catch (IOException ex) {
// Do exception handling
}
if (outToClient != null) {
File myFile = new File( fileToSend );
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(myFile);
} catch (FileNotFoundException ex) {
// Do exception handling
}
BufferedInputStream bis = new BufferedInputStream(fis);
try {
bis.read(mybytearray, 0, mybytearray.length);
outToClient.write(mybytearray, 0, mybytearray.length);
outToClient.flush();
outToClient.close();
connectionSocket.close();
// File sent, exit the main method
return;
} catch (IOException ex) {
// Do exception handling
}
}
}
}
}
Client.java:
package filesendtest;
import java.io.*;
import java.io.ByteArrayOutputStream;
import java.net.*;
class TCPClient {
private final String serverIP = "127.0.0.1";
private final int serverPort = 3248;
private final String fileOutput = "D:\testingoutput.jpg";
public static void main(String args[]) {
byte[] aByte = new byte[1];
int bytesRead;
Socket clientSocket = null;
InputStream is = null;
try {
clientSocket = new Socket( serverIP , serverPort );
is = clientSocket.getInputStream();
} catch (IOException ex) {
// Do exception handling
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (is != null) {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream( fileOutput );
bos = new BufferedOutputStream(fos);
bytesRead = is.read(aByte, 0, aByte.length);
do {
baos.write(aByte);
bytesRead = is.read(aByte);
} while (bytesRead != -1);
bos.write(baos.toByteArray());
bos.flush();
bos.close();
clientSocket.close();
} catch (IOException ex) {
// Do exception handling
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.