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

Socket Programming Assignment 2: UDP Client In this assignment, you will impleme

ID: 3763463 • Letter: S

Question

Socket Programming Assignment 2: UDP Client
In this assignment, you will implement a UDP client to send and receive datagram packets, setting a
proper socket timeout.
The standard ping program available in modern operating systems uses Internet Control Message
Protocol (ICMP) to ping a remote host. ICMP's echo-request/echo-reply options allow a client
machine to send a packet of data to a remote machine, and have the remote machine return the data
back to the client unchanged (an action referred to as echoing). Among other uses, the ping protocol
allows hosts to determine round-trip times to other machines.

Explanation / Answer

for this we need to create two programs

1. server.java

2. client.java

server.java - the code will be:

import java.io.*;
import java.net.*;
class server
{
public static void main(String args[]) throws Exception
   {   
           DatagramSocket serverSocket = new DatagramSocket(7886); //port to be used for udp server
           byte[] recdata = new byte[512];   
           byte[] sendData = new byte[512];   
           while(true) //keeping on running server   
       {   
           DatagramPacket recpacket = new DatagramPacket(recdata, recdata.length); // used to create packet for udp transfer
           serverSocket.receive(recpacket);   
           String sentence = new String( recpacket.getData());
           System.out.println("Recieved packet from: " + sentence);
           InetAddress IPAddress = recpacket.getAddress();
           int my_port = recpacket.getmy_port();   
           String capitalizedSentence = sentence.toUpperCase();
           sendData = capitalizedSentence.getBytes();
           DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, my_port);   
serverSocket.send(sendPacket);
       }
   }
}

client.java

import java.io.*;
import java.net.*;
class client
{
   public static void main(String args[]) throws Exception   
   { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); //reading the stream from server.   
           DatagramSocket clientSocket = new DatagramSocket();   
           InetAddress ipaddress = InetAddress.getByName("localhost");
           byte[] sendData = new byte[512];
           byte[] recdata = new byte[512];   
           String sentence = inFromUser.readLine();   
           sendData = sentence.getBytes();   
           DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ipaddress, 7886); //using same port 7886 for communication
           clientSocket.send(sendPacket);   
           DatagramPacket recpacket = new DatagramPacket(recdata, recdata.length); //creating datapacket based on the length of the text
           clientSocket.receive(recpacket);
           String message = new String(recpacket.getData()); //getting message from the server
           System.out.println("Message from Server:" + message);   
           clientSocket.close(); //closing the socket
   }
}

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