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

Need help! I have my requirements, but professor has not explained how to do it

ID: 3600694 • Letter: N

Question

Need help! I have my requirements, but professor has not explained how to do it and I really have no clue where to start

Assignment Requirements

Write a Java program for Assignment 2 on page 181 of the text – you will develop the pinger for both
UDP and TCP. Modify the UDP/TCP client and server code already posted as required for this program.
(18 points)
The following are additional requirements:
1. The client should send a ping consisting of exactly 10,000 (ten thousand) bytes for each ping. These
bytes should consist of alternating ones and zeros (that is the binary pattern 1010101….0).
2. The server should check if the ping consists of 10,000 bytes of alternating bit pattern – else should
print an error. If the pattern is correctly received, the server should echo the ping with its pong of the
same 10,000 bytes.
3. The client should check if the pong received from the server matches the alternating bit pattern - else
should print an error; if no error, then the client should record the RTT for these 10,000 bytes in ms.
4. One trial consists of 10 pings and pongs and your program should calculate the average RTT of these
ten pings/pongs and print them. Time unit for the average should be ms.
5. You should perform this trial three times - that is the client program must be run three times; do not
code the three trials in the same program.
6. This pinger program should be developed separately for both UDP and TCP. TCP should use nonpersistent
connections for each ping/pong pair.
7. You should first run the server on the localhost and perform Steps 4 and 5 above; you should then run
the server on another host in the LAN (give its IP address in the output) and again perform Steps 4
and 5 above.
8. Give the response as shown in the example output below in a separate .docx file.

UDP:

TCP:

RTT Average for First Trial

9. The client should print an error if the socket times out as required in the textbook assignment. For
this, use the following socket timeout method for a DatagramSocket object:

setSoTimeout(timeout_in_milliseconds) throws SocketTimeoutException


10. Upload your .java files (two for the clients and two for the servers) and your output file (that is, five
files in all) to Canvas before the due date/time.
11. Name your java files as [first name initial][last name]UDPClient.java, [first name initial][last
name]TCPClient.java, [first name initial][last name]UDPServer.java, [first name initial][last
name]TCPServer.java . Make sure your Java class names inside the files match the file names. Your
output file name should be [first name initial][last name].docx. For example, if your first name is
John and the last name is Smith, your files should be named: JSmithUDPClient.java,
JSmithTCPClient.java, JSmithUDPServer.java, JSmithTCPServer.java, and JSmith.docx. Your class
names in the java files will be JSmithUDPClient, JSmithTCPClient, JSmithUDPServer, and
JSmithTCPServer respectively.
12. Do not upload your java file as a txt file. Do not upload zip or rar files.

Directions from Book:

In this programming assignment, you will write a client ping program in Java.
Your client will send a simple ping message to a server, receive a corresponding
pong message back from the server, and determine the delay between when the
client sent the ping message and received the pong message. This delay is called the
Round Trip Time (RTT). The functionality provided by the client and server issimilar to the functionality provided by standard ping program available in modern
operating systems. However, standard ping programs use the Internet Control Message
Protocol (ICMP) (which we will study in Chapter 4). Here we will create a
nonstandard (but simple!) UDP-based ping program.
Your ping program is to send 10 ping messages to the target server over UDP.
For each message, your client is to determine and print the RTT when the corresponding
pong message is returned. Because UDP is an unreliable protocol, a packet sent
by the client or server may be lost. For this reason, the client cannot wait indefinitely
for a reply to a ping message. You should have the client wait up to one second for a
reply from the server; if no reply is received, the client should assume that the
packet was lost and print a message accordingly.
In this assignment, you will be given the complete code for the server (available
in the companion Web site). Your job is to write the client code, which will be
very similar to the server code. It is recommended that you first study carefully the
server code. You can then write your client code, liberally cutting and pasting lines
from the server code.

Code for TCP Client:

import java.net.*;

import java.io.*;

import javax.swing.JOptionPane;

public class TCPClient

{

public static void main(String[] args)

{

String serverName = "localhost";

//String serverName = "192.168.1.135";

int port = 9999;

try

{

System.out.println("Connecting to " + serverName + " on port " + port);

Socket clientSocket = new Socket(serverName, port); //create socket for connecting to server

System.out.println("Just connected to " + clientSocket.getRemoteSocketAddress());

OutputStream outToServer = clientSocket.getOutputStream(); //stream of bytes

DataOutputStream out = new DataOutputStream(outToServer);

String outText = JOptionPane.showInputDialog("Enter Client Message: ");

System.out.println("TCP Client says: " + outText);

out.writeUTF(outText);

InputStream inFromServer = clientSocket.getInputStream(); //stream of bytes

DataInputStream in = new DataInputStream(inFromServer);

System.out.println("TCP Server says: " + in.readUTF());

clientSocket.close();

}

catch (IOException e)

{

e.printStackTrace();

}

}

}

Code for TCP Server:

import java.net.*;

import java.io.*;

public class TCPServer

{

public static void main(String[] args)

{

ServerSocket serverSocket;

try

{

serverSocket = new ServerSocket(9999); //creates a socket and binds it to port 9999

//serverSocket = new ServerSocket(0); //creates a socket and binds it to next available port

while (true)

{

System.out.println("TCP Server waiting for client on port " + serverSocket.getLocalPort() + "...");

Socket connectionSocket = serverSocket.accept(); //listens for connection and

// creates a connection socket for communication

System.out.println("Just connected server port # " + connectionSocket.getLocalSocketAddress() + " to client port # " + connectionSocket.getRemoteSocketAddress());

DataInputStream in = new DataInputStream(connectionSocket.getInputStream()); //get incoming data in bytes from connection socket

String outText = in.readUTF();

System.out.println("RECEIVED: from IPAddress " +

connectionSocket.getInetAddress() + " and from port " + connectionSocket.getPort() + " the data: " + outText);

outText = outText.toUpperCase();

DataOutputStream out = new DataOutputStream(connectionSocket.getOutputStream()); //setup a stream for outgoing bytes of data

out.writeUTF(outText);

connectionSocket.close(); //close connection socket after this exchange

System.out.println();

}

}

catch (IOException e)

{

e.printStackTrace();

}

}

}

UDP Client Code:

import java.net.*;

import java.io.*;

import javax.swing.JOptionPane;

public class UDPClient

{

public static void main(String[] args)

{

String serverName = "localhost";

//String serverName = "192.168.1.152";

int port = 10999;

try

{

DatagramSocket clientSocket = new DatagramSocket();

InetAddress IPAddress = InetAddress.getByName(serverName);

byte[] sendData = new byte[1024];

String sentence = JOptionPane.showInputDialog("Enter Client Message: ");

//String sentence = "Hello";

sendData = sentence.getBytes();

System.out.println("UDP Client says: " + sentence);

DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);

clientSocket.send(sendPacket);

byte[] receiveData = new byte[1024];

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

clientSocket.receive(receivePacket);

String receivedSentence = new String(receivePacket.getData());

System.out.println("UDP Server says: " + receivedSentence);

clientSocket.close();

}

catch (IOException e)

{

e.printStackTrace();

}

}

}

UDP Server Code:

import java.net.*;

import java.io.*;

import java.util.Arrays;

public class UDPServer

{

public static void main(String[] args)

{

try

{

DatagramSocket serverSocket = new DatagramSocket(10999); //creates a datagram socket and binds it to port 10999

byte[] receiveData = new byte[1024];

byte[] sendData = new byte[1024];

while (true)

{

System.out.println("UDP Server waiting for client on port " + serverSocket.getLocalPort() + "...");

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

serverSocket.receive(receivePacket);

InetAddress IPAddress = receivePacket.getAddress();

int port = receivePacket.getPort();

String receivedSentence = new String(receivePacket.getData());

System.out.println("RECEIVED: from IPAddress " +

IPAddress + " and from port " + port + " the data: " + receivedSentence);

sendData = receivedSentence.toUpperCase().getBytes();

DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);

serverSocket.send(sendPacket);

Arrays.fill(receiveData, (byte)0);

Arrays.fill(sendData, (byte)0);

System.out.println();

}

}

catch (IOException e)

{

e.printStackTrace();

}

}

}

Server Location RTT Average for First Trial RTT Average for
Second Trial RTT Average for
Third Trial localhost 0.19 ms 0.19 ms 0.18 ms Remote Host
IP Address:
129.114.250.22 0.75 ms 0.66 ms 0.68 ms

Explanation / Answer

Yes, you can use the same port number for both TCP and UDP. Many protocols already do this, for example DNS works on udp/53 and tcp/53.

Technically the port pools for each protocol are completely independent, but for higher level protocols that can use either TCP or UDP it's convention that they default to the same port number.

When writing your server, bear in mind that the sequence of events for a TCP socket is much harder than for a UDP socket, since as well as the normal socket and bind calls you also have to listen and accept.

Furthermore that accept call will return a new socket and it's that socket that you'll then have to also poll for receive events. Your server should be prepared to continue accepting connections on the original socket whilst simultaneously servicing multiple clients each of which will be triggering receive events on their own sockets.

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