Help would be greatly appreciated! PLEASE make sure it runs in command prompt/te
ID: 3823649 • Letter: H
Question
Help would be greatly appreciated! PLEASE make sure it runs in command prompt/terminal!
: -> Java Implementation of a reliable data transfer protocol on top of UDP sockets using Go-Back-N protocol with window size equal to 4 packets.
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 the end-of-transmission packet 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. Build the MFTP on UDP ports. Use sequence number on data blocks to enforce reliability.
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 packet type as follows:
Packet type| Explanation
0 | ACK packet(i.e., acknowledgement packet).
1 | Request a file to be read from the server i.e., this packet carries file name to be downloaded.
2 | Data packet, i.e, contains data being transferred
3 | Reports an error [error message: File not found]
4 | EOT packet, i.e., it is an end-of-transmission (EOT) packet
Format of the UDP packet being transmitted between the client and the server is:
Type of the packet Sequence number Length (data size) Data (<= 16 bytes), depending on the packet type.
The EOT packet is in the same format as a regular data packet, except that its type field is set to 4 and its length is set to zero.
The server can close its connection and exit only after it has received ACKs for all data packets it has sent.
The length field specifies the number of characters carried in the data field. It should be in the range of 0 to 15.
For ACK packets, length should be set to zero.
Implementation:
Both client and server will run from the command line as follows: >client >server The is the name of the file to be downloaded from the server.
The RN is used by the server to emulate lost packets as,
If RN = 0 all packets are received by the receiver,
If RN = 1 first packet of the current winodw is lost,
If RN = 2 all packets of the current window are lost.
Output: In order to observe the performance of the sliding window protocol and to check whether the implementation is working properly, collect statistics. Collect statistics for both the client and server. After transferring a file print out the following suggested statistics
1. the total number of data packets transmitted
2. the total number of retransmissions
3. the total number of acknowledgments sent
4. the total number of acknowledgments received
5. the total number of duplicate packets received
6. the total amount of data sent.
7. A log file of actions, recording all activities between the client and server.
Supplied Code:
A packet class:
public class Packet
{
/* Packet Constructor:
* t = type
* i = seqno
*j = size of the packet
*/ public
Packet(int t, int i, int j, byte abyte[]) {
type = t;
seqno = i;
size = j;
data = new byte[size];
data = abyte; }
public byte[] getData() {
return data; }
public int getSeqNo() {
return seqno; }
public int getSize() {
return size; }
public boolean getType() {
return type; }
public String toString() {
return "type: " + type + "seq: " + seqno + " size: " + size + " data: " + data; }
private int type;
private int seqno;
private int size;
private byte data[];
}
Explanation / Answer
1. MFTPServer class:
-----------------------------------
package mftpservice;
/**
* Server class
*
*/
import java.io.*;
public class MFTPServer {
//main function
public static void main(String[] args) throws IOException {
new MFTPServerProcess().start();
}
}
------------------------------------------------------------
2. MFTPServerProcess class:
--------------------------------------------------
package mftpservice;
/**
* MFTPServerProcess class
*/
import java.io.*;
import java.net.*;
public class MFTPServerProcess extends Thread {
protected DatagramSocket mftpServerSocket = null;
protected BufferedReader in = null;
protected boolean moreData = true; // flag to check whether more data to
// transfer
public MFTPServerProcess() throws IOException {
this("MFTP Server Process");
}
public MFTPServerProcess(String name) throws IOException {
super(name);
mftpServerSocket = new DatagramSocket(4445);
}
public void run() {
// receive request from client
byte[] buffer = new byte[512];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
try {
mftpServerSocket.receive(packet);
} catch (IOException e1) {
e1.printStackTrace();
}
String filename = new String(packet.getData(), 0, packet.getLength());
try {
in = new BufferedReader(new FileReader(filename));
} catch (FileNotFoundException e) {
System.err.println("Could not open file. Serving time instead.");
}
// loop for sending data to client
while (moreData) {
try {
// figure out response
String data = null;
if (in == null)
data = "";
else {
data = getAndSendFileData();
}
//Packet pkt = new Packet(1,1,data.length(),data.getBytes());
//buffer = pkt.toString().getBytes();
buffer = data.getBytes();
// send the response to the client at "address" and "port"
InetAddress address = packet.getAddress();
int port = packet.getPort();
packet = new DatagramPacket(buffer, buffer.length, address, port);
mftpServerSocket.send(packet);
} catch (IOException e) {
e.printStackTrace();
moreData = false;
}
}
mftpServerSocket.close();
}
protected String getAndSendFileData() {
String data = null;
try {
if ((data = in.readLine()) == null) {
in.close();
moreData = false;
data = "";
}
} catch (IOException e) {
data = "IOException occurred in server.";
}
return data;
}
}
-----------------------------------------------
3. MFTPClient class:
-----------------------------------------
package mftpservice;
/**
* MFTPClient class
*/
import java.io.*;
import java.net.*;
public class MFTPClient {
private DatagramSocket mftpClientSocket;
private DatagramPacket packet;
private byte[] buffer;
private String hostname;
private String filename; // name of file requested from server
private BufferedWriter writer;
/**
* Constructor
*/
public MFTPClient() {
// create a datagram socket
try {
mftpClientSocket = new DatagramSocket();
mftpClientSocket.setSoTimeout(1000);
buffer = new byte[512];
hostname = "localhost";
filename = "";
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// send request to server
public void sendRequest(InetAddress address, int port) {
// server address
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, 4445);
// send request to server
try {
mftpClientSocket.send(packet);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// receive data from server
public void receiveData() throws IOException {
writer = new BufferedWriter(new FileWriter(new File("data.txt")));
boolean moreData = true; // flag whether more data to receive
while (moreData) {
try {
// get response from server
packet = new DatagramPacket(buffer, buffer.length);
mftpClientSocket.receive(packet);
// display response received from server
String receivedData = new String(packet.getData(), 0, packet.getLength());
writer.write(receivedData + " ");
System.out.println("Data: " + receivedData);
} catch (SocketTimeoutException exception) {
System.out.println("Timeout...");
System.out.println("Exiting...");
System.out.println("Exited!!!");
writer.close();
mftpClientSocket.close();
break;
}
}
}
/**
* @return the buffer
*/
public byte[] getBuffer() {
return buffer;
}
/**
* @param buffer
* the buffer to set
*/
public void setBuffer(byte[] buffer) {
this.buffer = buffer;
}
/**
* @return the hostname
*/
public String getHostname() {
return hostname;
}
/**
* @return the filename
*/
public String getFilename() {
return filename;
}
/**
* @param filename
* the filename to set
*/
public void setFilename(String filename) {
this.filename = filename;
}
// main function
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.out.println("Usage: java MFTPClient <filename>");
return;
}
MFTPClient mftpClient = new MFTPClient();
// send request
// byte[] buffer = new byte[256];
// String hostname = "localhost";
mftpClient.setFilename(args[0]); // name of file requested from server
mftpClient.setBuffer(mftpClient.getFilename().getBytes());
InetAddress address = InetAddress.getByName(mftpClient.getHostname());
int port = 4445;
mftpClient.sendRequest(address, port);
mftpClient.receiveData();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.