The client code Assignment Help

Assignment Help: >> Datagram communication >> The client code

 The client code:

The code for the client is shown below:

import java.net. *;
import java.io. *;

public class DatagramClient
{
// number of bytes in a UDP packet
static final int UDP_PACKET_SIZE = 512;

// port number of UDP service
static final int PORT_NUMBER = 4000;
private DatagramSocket datagramSocket;

// constructor
public DatagramClient ( )
{

try
{

// create a socket 
datagramSocket = new DatagramSocket( ); 

}

catch (IOException e)

{
System.out.println("Trouble setting up datagram socket" + e);
}
}
// send a datagram containing a message
public void sendDatagramPacket (String message)
{
DatagramPacket packet; // UDP packet

// convert string to bytes
byte [] toBeSent = new byte [UDP_PACKET_SIZE ];
toBeSent = message.getBytes( );
try
{
/ * UDP packet contains the data, total length of data, destination IP address and destination port. */
packet = new DatagramPacket(toBeSent, toBeSent.length, InetAddress.getLocalHost( ), PORT_NUMBER);
datagramSocket.send(packet);
}
catch (IOException e)
{
System.out.println("Trouble sending datagram packet" + e);
}
}
}

The DatagramClient has a simple constructor, which sets up a datagram socket to be used for sending datagrams. Because UDP does not set up a connection (unlike TCP) this socket does not require any details of the destination. Only the individual packets contain the destination details, as we shall see later.

The client class has one method, sendDatagramPacket, which has a string argument specifying the text of the message to be sent. The packet data is a string containing the message text. The first part of the method formats the data appropriately and stores the string in byte format in the buffer toBeSent. We then create a datagram packet containing this data together with the packet header information required by the UDP protocol. This header information includes the destination IP address and port number as well as the number of bytes of data. The packet is then sent via the datagram socket to the specified IP address and port number. In this example, we are assuming the server is on the local host, as we specify the destination address using the static method InetAddress.getLocalHost. This is an alternative to specifying the loopback address "127.0.0.1".

We test this class using the main method of class TestDatagramClient, which is as follows:

public class TestDatagramClient
{
public static void main (String [] args)
{
// message to tell the server we are quitting
final String CLIENT_QUITTING = "Client quitting";

DatagramClient dc = new DatagramClient( );
// create and send some packets

dc.sendDatagramPacket("Wish you were here");

dc.sendDatagramPacket("Weather is lovely");

// send a packet to tell the server we are quitting
dc.sendDatagramPacket(CLIENT_QUITTING);
}
}

This tests the class by creating a DatagramClient object and sending several messages, terminating with the message CLIENT_QUITTING. This is a special message to indicate to the server that the client has finished sending datagrams. Because UDP is connectionless, there is no need to close anything before the client program terminates - we just exit.

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your  The client code homework and assignments? Live  The client code experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer  The client code homework help, java assignment help and  The client code projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd