Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Project description: The TCP/IP stack has five layers, namely application, trans

ID: 3808502 • Letter: P

Question

Project description: The TCP/IP stack has five layers, namely application, transport, network, link, and physical. In Phase 1, each student implemented the standard user datagram protocol (UDP) sockets. The intention was to transfer a file (say JPEG) between a UDP client process and a UDP server process. In Phase 2, each of you implemented reliable data transfer service over the reliable UDP connection developed in Phase 1. In Phase 3, each ofyou has to implement the RDT 2.2 protocol described in Section 3.4.1, page 218-227 of the course textbook. The finite state machines (FSM)of the sender and received are given below: rdt send (data) sndpkt make pkt. (0,data, checksum) udt send (sndpkt) rat rev (rcvpkt) && (corrupt (rcvpkt) II isACK (rcvpkt, 1)) Wait for udt send (sndpkt) Wait for call 0 from ACK 0 above rdt rcv (rcvpkt) && not corrupt (rcvpkt) && not corrupt (rcvpkt) && isACK (rcvpkt, 1) && isACK (rcvpkt, 0) Wait for Wait for call 1 from ACK 1 above rdt_rcv (rcvpkt) && (corrupt (rcvpkt) II SACK (rcvpkt., 0)) rdt send (data) udt-send (sndpkt) sndpkt make pkt (1, data, checksum) udt send (sndpkt) Figure 3.13 rdt2.2 sender

Explanation / Answer

SERVER SIDE PROGRAM

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class fileServer {

public final static int SOCKET_PORT = 13265;
public final static String FILE_TO_SEND = "c:/temp/source.pdf";

public static void main (String [] args ) throws IOException {
FileInputStream filein = null;
BufferedInputStream bfIn = null;
OutputStream Ots = null;
ServerSocket soc = null;
Socket sock = null;
try {
soc = new ServerSocket(SOCKET_PORT);
while (true) {
System.out.println("Waiting...");
try {
sock = soc.accept();
System.out.println("Accepted connection : " + sock);
// send file
File MY_FILE = new File (FILE_TO_SEND);
byte [] Byte_array = new byte [(int)MY_FILE.length()];
filein = new FileInputStream(MY_FILE);
bfIn = new BufferedInputStream(filein);
bfIn.read(Byte_array,0,Byte_array.length);
Ots = sock.getOutputStream();
System.out.println("Sending " + FILE_TO_SEND + "(" + Byte_array.length + " bytes)");
Ots.write(Byte_array,0,Byte_array.length);
Ots.flush();
System.out.println("Done.");
}
finally {
if (bfIn != null) bfIn.clOtse();
if (Ots != null) Ots.clOtse();
if (sock!=null) sock.clOtse();
}
}
}
finally {
if (soc != null) soc.clOtse();
}
}
}

CLIENT SIDE PROGRAM

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class fileClient {

public final static int SOCKET_PORT = 13265;
public final static String SERVER = "127.0.0.1";
public final static String
FILE_TO_RECEIVED = "c:/temp/source-downloaded.pdf";

public final static int Size_of_file= 622386; /

public static void main (String [] args ) throws IOException {
int ByteRead;
int present = 0;
FileOutputStream Fileos = null;
BufferedOutputStream BufferOS = null;
Socket sock = null;
try {
sock = new Socket(SERVER, SOCKET_PORT);
System.out.println("Connecting...");

byte [] Byte_array = new byte [Size_of_file]
InputStream is = sock.getInputStream();
Fileos = new FileOutputStream(FILE_TO_RECEIVED);
BufferOS = new BufferedOutputStream(Fileos);
ByteRead = is.read(Byte_array,0,Byte_array.length);
present = ByteRead;

do {
ByteRead =
is.read(Byte_array, present, (Byte_array.length-present));
if(ByteRead >= 0) present += ByteRead;
} while(ByteRead > -1);

BufferOS.write(Byte_array, 0 , present);
BufferOS.flush();
System.out.println("File " + FILE_TO_RECEIVED
+ " downloaded (" + present + " bytes read)");
}
finally {
if (Fileos != null) Fileos.close();
if (BufferOS != null) BufferOS.close();
if (sock != null) sock.close();
}
}

}