Sockets of UDP

Home  »  Java  »  Advance Java  »  Socket Programming  »  Sockets of UDP


As We know that UDP stands for user datagram Protocol Which is also Called as Connection Less Protocol which is used for Sending the data in the Form of Datagram's and this will never Create a Connection With Server before Transmitting data from Client to Server or vice versa. UDP Uses Datagram's and JAVA Provided various Constructor for Sending Datagram's From One Place to another.

  1. DatagramPacket(byte [] b, int l) The DatagramPacket Class is used for Creating a Packet For Sending and the First Argument of this Constructor Specify the information Which is to be Send and Second Argument Specify the Actual Length which is to be Send.

  2. DatagramSocket (int ) this is used for Making a Socket Which will be Used by Server and Client For Communication.

The Various Methods those are used In these Classes are :-
MethodsDescription
Send()Used by DatagramSocket To Send the Datagram's.
Receive()Used also by Datagram Sockets to Receive the datagram's.
Close()Used for Closing the Datagram Socket.


The following is a UDP/IP example, showing to retrieve incoming packets, display their contents and send notification back to the sender.

The following is a UDP/IP example to develop a simple client server application that uses UDP to send data and time information. In this example, the server transmits data and time at one-second intervals, regardless of whether the client is listening. this exemplifies the fundamental difference between TCP and UDP services: there is no concept of a "connection" in the UDP system. Hence transmission of data onto the network does not require any host to be listening.

There are two classes that make up the UDP-based Daytime syste: the server and the client. When started, both client and server continue to run indefinitely. Each time the client notices a new message; it prints it to standard output.

The server application is started by importing the required packages and declaring the classes as shown:



Next declare and define the main() method as shown:



The first step is to construct an InetAddress object that describes the remote host to which the date and time information is to be sent, achieved with the method call InetAddress.getByName(target). Once the address information has been obtained, the DatagramSocket object is created.



When a proper address and socket have been initialized, a DatagramPacket object must be prepared. The socket is then used to send the packet. The DatagramPacket object constructor takes four arguments. The first two arguments provide the data to be transmitted and the length of that data. The third argument specifies the destination address of this packet, and the final argument indicates the port number to which the packet is to be sent. After the DatagramPacket is prepared, the data is transmitted using the DatagramSocket class's send() method with and interval of 1 second.



This completes the server code. Running the server application sends the current data and time at an interval of 1 second. Each transmission is accompanied by the message "Sending" written to standard output. The code is shown below:


The Output of this application will be:



The next step is to design the client application. The client program is started by importing the packages, defining the main method and declaring the DatagramSocekt and DatagramPacket objects as shown:



Next a DatagramSocket object is created, bound to the port number 1313. Also a DatagramPacket object is crated to hold the received data. To support this reception, the DatagramPacket is provided with stroage for the received data (an array of 64 bytes).



Once the socket and packet object are set up, all that is required to receive the data is a call to the DatagramSocket class's receive() method. The receive() method takes a single argument of type DatagramPacket, to hold the received data.

After the receive() method returns, the DatagramPacket object will contain the received data and the address of the sending machine. The address can be extracted with the getAddress() method, which returns an InetAddress object. The toString() method of the InetAddress class returns a printable form of the address. The data sent can be extracted using the getData() method.


The complete client code is as shown:



Socket Programming


Sockets for TCP/IP Sockets of UDP



No comments:

Post a Comment