Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using the codes PacketSendDemo and PacketReceiveDemo: 1. Compile and test the provided codes using a localhost (test the functionality of the PacketSendDemo to www.google.com). 2.

Using the codes PacketSendDemo and PacketReceiveDemo: 1. Compile and test the provided codes using a localhost (test the functionality of the PacketSendDemo to www.google.com). 2. Modify the program to allow communication through port 5000 instead of port number 2000. 3. Modify the programs to allow the user to provide an IP address instead of a hostname. 4. Create a GUI that would a) allow the user to provide the required parameters, that is (either the IP address or the hostname) and the port number. b) send the output to some object in the GUI or another window or to a file if you choose to. You will be submitting your three programs separately. PacketReceiveDemo import java.net.*; import java.io.*; public class PacketReceiveDemo { public static void main (String args[]) { try { System.out.println ("Binding to local port 2000"); // Create a datagram socket, bound to the specific port 2000 DatagramSocket socket = new DatagramSocket(2000); System.out.println ("Bound to local port " + socket.getLocalPort()); // Create a datagram packet, containing a maximum buffer of 256 bytes DatagramPacket packet = new DatagramPacket( new byte[256], 256 ); // Receive a packet - remember by default this is a blocking operation socket.receive(packet); System.out.println ("Packet received!"); // Display packet information InetAddress remote_addr = packet.getAddress(); System.out.println ("Sent by : " + remote_addr.getHostAddress() ); System.out.println ("Send from: " + packet.getPort()); // Display packet contents, by reading from byte array ByteArrayInputStream bin = new ByteArrayInputStream (packet.getData()); // Display only up to the length of the original UDP packet for (int i=0; i < packet.getLength(); i++) { int data = bin.read(); if (data == -1) break; else System.out.print ( (char) data) ; } socket.close(); } catch (IOException ioe) { System.err.println ("Error - " + ioe); } } } PacketSendDemo import java.net.*; import java.io.*; // This will create a text and send it to // the hostname using port 2000 public class PacketSendDemo { public static void main (String args[]) { int argc = args.length; if (argc != 1) { System.out.println ("Syntax :"); System.out.println ("java PacketSendDemo hostname"); return; } String hostname = args[0]; try { System.out.println ("Binding to a local port"); // Create a datagram socket, bound to any available local port DatagramSocket socket = new DatagramSocket(); System.out.println ("Bound to local port " + socket.getLocalPort()); // Create a message to send using UDP packet ByteArrayOutputStream bout = new ByteArrayOutputStream(); PrintStream pout = new PrintStream (bout); pout.print ( "Greetings from this client!!"); // Get the contents of the message as an array of bytes byte[] barray = bout.toByteArray(); DatagramPacket packet = new DatagramPacket(barray, barray.length); System.out.println ("Looking up the hostname " + hostname ); // Lookup the hostname and resolve the IP address InetAddress remote_addr = InetAddress.getByName(hostname); System.out.println (" Hostname IP is " + remote_addr.getHostAddress()); packet.setAddress (remote_addr); packet.setPort (2000); // Send packet socket.send(packet); System.out.println ("Packet sent"); } catch (UnknownHostException uhe) { System.err.println ("Can't find the host " + hostname); } catch (IOException ioe) { System.err.println (" Error - " + ioe ); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions