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

Java Programing: Write a client (sender) and server (receiver) java application

ID: 3592364 • Letter: J

Question

Java Programing:

Write a client (sender) and server (receiver) java application and used Stop-and-Wait-with-ARQ using the provided java classes (State.java, Packet.java, StopAndWaitClient.java, StopAndWaitServer.java, NoisyDatagramSocket.java)
Make sure to have your client read from the keyboard (until EOF {ctrD or ctr-Z}) and send char character (one at a time) to the server. And use -NoisyDatagramSocket.setSoTimeout( TimeOutValue ); instead of a timer

--------

Provided Documents:

You will be provided with a NoisyDatagramSocket class that extends the official DatagramSocket class provided by Java, (used to send a DatagramPacket over UDP). It is recommended that you take time to familiarize yourself with the

main classes provided by Java for UDP support and their functionality.

Our NoisyDatagramSocket class has some additional functionality. It can simulate a noisy network. It allows the programmers to set the

            MTU (packet size and fragmentation),

            packet drop rate (unreliable networks)

            and add a random time delays to packets (out-of-order reception).

Parameters regulating the noisy network or packet transfers are located in a file named

            net.properties.txt

This file needs to be in the same directory as the .class files. And it can be modified/changed to test different networking conditions or scenarios (packet loss, packet delay, max packet size, etc)

net.properties.txt

##################################################

# net properties file

#

# named: net.properties.txt

# must be in the same directory as NoisyDatagramSocket.class

# and any other .class files

#

#################################################

#== packet drop rate =============================

#

# n=0 --- Do not drop any packets

# n=10 --- Drop 10% of all packets

# n=50 --- Drop 50% of all packets

# n=4,6,7 --- Drop select packets e.g. packet.droprate = 4,6,7

packet.droprate = 3

#== packet delay rate ============================

#

# packet delay in milliseconds

# min==max --- in order delivery

packet.delay.minimum = 10

packet.delay.maximum = 20

#== packet maximum transmission size =============

# 1500 normal for ethernet

# -1 unlimited (Integer.MAX_VALUE)

packet.mtu = 1500

#################################################

The following two pieces of code illustrate how to use UDP facilities in Java. A sender program is used to send a “Hello World” message to a receiver over UDP. The NoisyDatagramSocket class (extends DatagramSocket) gives you additional control over any message transfers as specified in the net.properties.txt file.

Note that these two programs will work if all instances of NoisyDatagramSocket are replaced by DatagramSocket.

Server

The following code creates a server that listens on a port and simply outputs the data it receives.

import java.net.DatagramPacket;

import java.net.InetAddress;

// uncomment the line below to use offical DatagramSocket

// import java.net.DatagramSocket;

public class udpreceiver {

   static final int PORT = 2102;

   public static void main(String[] args) {

      try {

          byte [] buffer = new byte[11];

         NoisyDatagramSocket socket = new NoisyDatagramSocket(PORT); // Note NOT using DatagramSocket

          DatagramPacket packet = new DatagramPacket(buffer,buffer.length);

          socket.receive(packet);

          InetAddress client = packet.getAddress();

          System.out.println(" Received'"+new String(buffer)+"' from " +packet.getAddress().getHostAddress());

      }

      catch(Exception e){ e.printStackTrace(); }

   }

}

Client

The following code creates a client, capable of transmitting a simple message to the server.

import java.net.DatagramPacket;

import java.net.InetAddress;

// uncomment the line below to use offical DatagramSocket

//import java.net.DatagramSocket;

public class udpsender {

   static final String SERVER = "localhost";

   static final int PORT = 2102;

   public static void main(String[] args) {

      try {

         byte [] buffer = ("Hello World").getBytes();

         NoisyDatagramSocket socket = new NoisyDatagramSocket(); // Note NOT using DatagramSocket

         socket.send(new DatagramPacket(buffer, buffer.length, InetAddress.getByName(SERVER), PORT));

      }

      catch(Exception e){ e.printStackTrace(); }

   }

}

--------------

NoisyDatagramSocket:

Explanation / Answer

import java.io.*;

import java.net.*;

import java.nio.*;

class UDPStopAndWaitClient{

private static final int BUFFER_SIZE = 1024;

private static final int PORT = 6789;

private static final String HOSTNAME = "localhost";

private static final int BASE_SEQUENCE_NUMBER = 42;

public static void main(String args[]) throws Exception{

// Create a socket

DatagramSocket socket = new DatagramSocket();

socket.setSoTimeout( 1000 );

// The message we're going to send converted to bytes

Integer sequenceNumber = BASE_SEQUENCE_NUMBER;

for (int counter = 0; counter < 10; counter++) {

boolean timedOut = true;

while( timedOut ){

sequenceNumber++;

// Create a byte array for sending and receiving data

byte[] sendData = new byte[ BUFFER_SIZE ];

byte[] receiveData = new byte[ BUFFER_SIZE ];

// Get the IP address of the server

InetAddress IPAddress = InetAddress.getByName( HOSTNAME );

System.out.println( "Sending Packet (Sequence Number " + sequenceNumber + ")" );

// Get byte data for message

sendData = ByteBuffer.allocate(4).putInt( sequenceNumber ).array();

try{

// Send the UDP Packet to the server

DatagramPacket packet = new DatagramPacket(sendData, sendData.length, IPAddress, 6789);

socket.send( packet );

// Receive the server's packet

DatagramPacket received = new DatagramPacket(receiveData, receiveData.length);

socket.receive( received );

// Get the message from the server's packet

int returnMessage = ByteBuffer.wrap( received.getData( ) ).getInt();

System.out.println( "FROM SERVER:" + returnMessage );

// If we receive an ack, stop the while loop

timedOut = false;

} catch( SocketTimeoutException exception ){

// If we don't get an ack, prepare to resend sequence number

System.out.println( "Timeout (Sequence Number " + sequenceNumber + ")" );

sequenceNumber--;

}

}

}

socket.close();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote