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

In this project you will create a pair of Ping Client/Server program built on UD

ID: 3679036 • Letter: I

Question

In this project you will create a pair of Ping Client/Server program built on UDP in java.

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 RTT when the corresponding ping message is returned.

Since 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.

Server code:

The Ping server code is given below.

class PingServer

{ public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(2000); //server will run on port #2000

byte[] receiveData = new byte[1024]; //Processing loop

while(true)

{

//create a random number generator for use in packet loss simulation

Random random = new Random();

//create a datagram packet to hold incoming UDP packet

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

// get the client message

serverSocket.receive(receivePacket);

String sentence = new String(receivePacket.getData(),0, receivePacket.getLength());

InetAddress IPAddress = receivePacket.getAddress(); //get client's IP

int port = receivePacket.getPort(); //get client's port #

//print out the clients's IP address, port number and the message

System.out.println("client's port # = " + port);

System.out.println("client'sIP = " +IPAddress);

System.out.println("client's message = " +sentence);

//capitalize the message from the client

String capitalizedSentence = sentence.toUpperCase();

//simulate the packet loss

int rand =random.nextInt(10);//a random number in the range of 0 to10

// if rand is less than 4 we consider the packet lost and do not reply

if (rand < 4) {

System .out.println("Reply not sent");

continue; }

//otherwise, the server responds

byte[] sendData = capitalizedSentence.getBytes();

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

serverSocket.send(sendPacket);

System.out.println("Reply to the client sent"); }//while

}//main

}

Specifically, your client program should do the following:

(1)Print out the current time and the ping attempt number. Send the ping message using UDP.

(2) Print the response message from server, if any (the server will capitalize the message sent by the client and send it back to the client). Print out the elapsed time in microseconds.

(3) Calculate and print the round trip time (RTT), in microseconds, of each packet, if server responses.

(4) Otherwise, print “Request timed out”.

Explanation / Answer

I can suggest this program in C.

programme:

#include <stdio.h>

#include <sys/types.h>

#include <stdio.h>

#include <sys/types.h>

#include <sys/socket.h> #include <sys/time.h> #include <sys/select.h> #include <netdb.h> #include <string.h> #include <stdlib.h> #include <errno.h> #include <time.h> #include <unistd.h> #define PING_PORT 12000 #define PING_PORT_STR "12000" #define PING_MESSAGES 10 #define TIMEOUT_SECS 1 /* Some interesting times measured when testing this code: * * Source machine running the client was located in Lisbon, Portugal. * * It is interesting to see how ping times increase with distance. * * Results for a virtual server from DigitalOcean running in San Francisco, CA, where * packets (probably) had to go through a transatlantic link (either that, or satellite). * * PING 104.***.***.***: 209.3490 ms * PING 104.***.***.***: 204.2170 ms * PING 104.***.***.***: 204.0840 ms * PING 104.***.***.***: 204.0750 ms * PING 104.***.***.***: 204.1260 ms * PING 104.***.***.***: 203.5260 ms * PING 104.***.***.***: 203.9290 ms * PING 104.***.***.***: 203.9460 ms * PING 104.***.***.***: 204.1560 ms * PING 104.***.***.***: 203.7490 ms * * Results for an OVH server running in Roubaix, France. * It is expected that the ping times are slightly lower, since the packets never leave Europe. * Also, this is a dedicated server rather than a VPS, but this is usually irrelevant. * * PING *.kimsufi.com: 44.2090 ms * PING *.kimsufi.com: 40.5510 ms * PING *.kimsufi.com: 39.9340 ms * PING *.kimsufi.com: 39.9480 ms * PING *.kimsufi.com: 40.1100 ms * PING *.kimsufi.com: 40.8410 ms * PING *.kimsufi.com: 40.0790 ms * PING *.kimsufi.com: 41.0890 ms * PING *.kimsufi.com: 40.8160 ms * PING *.kimsufi.com: 39.8980 ms * * Results for a server running in the same LAN as the client: * PING 192.168.1.91: 1.1860 ms * PING 192.168.1.91: 0.5220 ms * PING 192.168.1.91: 0.7420 ms * PING 192.168.1.91: 0.6920 ms * PING 192.168.1.91: 0.4170 ms * PING 192.168.1.91: 0.7390 ms * PING 192.168.1.91: 0.4060 ms * PING 192.168.1.91: 0.3660 ms * PING 192.168.1.91: 0.3530 ms * PING 192.168.1.91: 0.3630 ms * * Results for localhost: * PING localhost: 0.1810 ms * PING localhost: 0.1460 ms * PING localhost: 0.1470 ms * PING localhost: 0.1470 ms * PING localhost: 0.1470 ms * PING localhost: 0.1460 ms * PING localhost: 0.1180 ms * PING localhost: 0.0410 ms * PING localhost: 0.0840 ms * PING localhost: 0.0730 ms * */ void do_ping(const char *target, int sock, struct sockaddr *dest, socklen_t dest_sz) { static const char ping_request[] = "PING "; static const char ping_reply[] = "PONG "; static char sock_buff[1024]; int i; for (i = 0; i < PING_MESSAGES; i++) { printf("PING %s: ", target); fd_set fdset; FD_ZERO(&fdset); FD_SET(sock, &fdset); struct timeval timeout; timeout.tv_sec = TIMEOUT_SECS; timeout.tv_usec = 0; struct timeval before; gettimeofday(&before, NULL); if (sendto(sock, ping_request, sizeof(ping_request)-1, 0, dest, dest_sz) < 0) { printf("error - %s ", strerror(errno)); continue; } int res = select(sock+1, &fdset, NULL, NULL, &timeout); struct timeval after; gettimeofday(&after, NULL); if (res < 0) { printf("error - %s ", strerror(errno)); } else if (res == 0) { printf("TIMEOUT "); } else { ssize_t received; if ((received = recvfrom(sock, sock_buff, sizeof(sock_buff)-1, 0, NULL, NULL)) < 0) { printf("error - %s ", strerror(errno)); continue; } sock_buff[received] = ''; if (strcmp(sock_buff, ping_reply)) { printf("error - unexpected reply "); } else { double millisecs = (after.tv_sec-before.tv_sec)*1000+(after.tv_usec-before.tv_usec)/1000.0; printf("%.4f ms ", millisecs); } } } } int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "Usage: %s hostname/ip ", argv[0]); return 0; } struct addrinfo *res; struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_DGRAM; hints.ai_protocol = IPPROTO_UDP; int s; if ((s = getaddrinfo(argv[1], PING_PORT_STR, &hints, &res)) != 0) { fprintf(stderr, "Couldn't locate PING server: %s ", gai_strerror(s)); exit(EXIT_FAILURE); } if (res == NULL) { fprintf(stderr, "Couldn't find PING service on the target host. "); freeaddrinfo(res); exit(EXIT_FAILURE); } if (res->ai_next != NULL) { // Should never happen fprintf(stderr, "Oops! Multiple PING services found on the targest host?! "); freeaddrinfo(res); exit(EXIT_FAILURE); } int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sock < 0) { perror("Unable to create socket"); freeaddrinfo(res); exit(EXIT_FAILURE); } do_ping(argv[1], sock, res->ai_addr, res->ai_addrlen); freeaddrinfo(res); return 0; }
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