Your project is to write a test program that will measure the time to transfer 1
ID: 3596372 • Letter: Y
Question
Your project is to write a test program that will measure the time to transfer 1 GB of data under various conditions and prepare a report that charts the results.
Transfer rate, bandwidth speed -- they dont really matter.
Message size: ranging from 1 byte to 65,536 bytes, incrementing in powers of two (1, 2, 4, 8, 16, …)...
You many use Java, C, C++, Go, or Python for this assignment.
Specifications
Your program should allow you to adjust three parameters for testing:
Transport: TCP or UDP Message size: ranging from 1 byte to 65,536 bytes, incrementing in powers of two (1, 2, 4, 8, 16, …) Acknowledgements: pure streaming or stop-and-wait All of these should be specified on the command line so you can easily modify them within a shell script. Your program will support two modes of communication between client and server: pure streaming and a stop-and-wait protocol.
Client
Your client will accept command-line parameters that include: Hostname (or IP address in decimal dot notation) of server Port number of server Transport protocol: TCP or UDP Acknowledgement protocol: streaming or stop-and-wait Message size
The server must be started first, of course, and will receive messages from the client. Prior to starting the data transfer, the client sends a message to the server with a message that, at a minimum, identifies the number of bytes that it will transfer. See the server section for possible extensions. It then waits for an acknowledgement from the server. This acknowledgement can be a single byte. Prior to transferring the data, you will note the start time. After the last message was sent, you will note the end time to compute the overall elapsed time.
Basic server
Your server will be given the following parameters on the command line: Port number for requests Transport protocol: TCP or UDP Acknowledgement protocol: streaming or stop-and-wait The server will start up, create a socket, and read messages from the client. The first message will contain the size. This can be assumed to be a fixed number of bytes (e.g., four bytes). This message is acknowledged (e.g., send back a single byte containing a 1 or whatever). Subsequent bytes read will contain the data.
Output
Server
At the end of each server session (when the maximum number of bytes has been read or a TCP connection has been closed) the server will print: Acknowledgement protocol used Number of messages read Number of bytes read
Client
At the end of execution, the client will print Number of messages sent Number of bytes sent Total transmit time
Explanation / Answer
/*
file name : client.java
Use Java 8 to run the below code.
I have used java.time which is the part of java 8(jdk 1.8)
first run the Server.java ,then enter the port number
After that run client.java, then enter ip address and port number
if you are using same machine for server and client then use ip as a 127.0.0.1 and port number will be same for both server and client,
Mostly number greater than 5000 will be good for port number.
If you are running different machine to run the client and server, in that case while running client.java it prompt you
to enter the ip address, in that place give the ip address of server.
And port number is always same
*/
import java.net.*;
import java.io.*;
import java.util.*;
//Java 8 has a cleaner solution - Instant and Duration
import java.time.Duration;
import java.time.Instant;
//your code
public class client
{
// initialize socket and input output streams
private Socket socket = null;
private DataInputStream input = null;
private DataOutputStream out = null;
// constructor to put ip address and port
public client(String address, int port)
{
// establish a connection
try
{
socket = new Socket(address, port);
System.out.println("Connected");
// takes input from terminal
input = new DataInputStream(System.in);
// sends output to the socket
out = new DataOutputStream(socket.getOutputStream());
}
catch(UnknownHostException u)
{
System.out.println(u);
}
catch(IOException i)
{
System.out.println(i);
}
// string to read message from input
String line = "";
// keep reading until "stop" is input
int totalBytes=0;
Instant start = Instant.now();
while (!line.equals("stop"))
{
try
{
line = input.readLine();
final byte[] utf32Bytes = line.getBytes("UTF-32");
totalBytes+=(utf32Bytes.length);
out.writeUTF(line);
}
catch(IOException i)
{
System.out.println(i);
}
}
System.out.println("Total bytes reveived :"+totalBytes);
Instant end = Instant.now();
Duration timeElapsed = Duration.between(start, end);
System.out.println("Time taken: "+ timeElapsed.toMillis() +" milliseconds");
// close the connection
try
{
input.close();
out.close();
socket.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String args[])
{
// same port numbeer used at server code
System.out.println("Enter the IP Address then press enter aftr that enter Port Number:");
Scanner sc = new Scanner(System.in);
String ip = sc.nextLine();
int port = sc.nextInt();
client client = new client(ip,port);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* A Java program for a
filename : Server.java
Use Java 8 to run the below code.
I have used java.time which is the part of java 8(jdk 1.8)
other wise comment the line number 13,14,43,61,62,63
*/
import java.net.*;
import java.io.*;
import java.util.*;
//Java 8 has a cleaner solution - Instant and Duration
//import java.time.Duration;
//import java.time.Instant;
public class Server
{
//initialize socket and input stream
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream in = null;
// constructor with port
public Server(int port)
{
// starts server and waits for a connection
try
{
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for a client ...");
socket = server.accept();
System.out.println("Client accepted");
// takes input from the client socket
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
String line = "";
int totalBytes=0;
// reads message from client until "stop" is sent
Instant start = Instant.now();
while (!line.equals("stop"))
{
try
{
line = in.readUTF();
final byte[] utf32Bytes = line.getBytes("UTF-32");
totalBytes+=(utf32Bytes.length);
System.out.println(line);
}
catch(IOException i)
{
System.out.println(i);
}
}
System.out.println("Total bytes reveived :"+totalBytes);
Instant end = Instant.now();
Duration timeElapsed = Duration.between(start, end);
System.out.println("Time taken: "+ timeElapsed.toMillis() +" milliseconds");
System.out.println("Closing connection");
// close connection
socket.close();
in.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String args[])
{
System.out.println("Enter the Server Port number :");
Scanner sc = new Scanner(System.in);
int port = sc.nextInt();
Server server = new Server(port);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.