UDP Server in Java


UDP Server in Java

This section provides you the brief description and program of the UDP server only for received your messages or information.

This section provides you the brief description and program of the UDP server only for received your messages or information.

UDP Server in Java


Providing you information relating to all technicalities of UDP server. This section provides you the brief description and program of the UDP server only for received your messages or information.

The UDP server 
UDP denotes a computing and technical terms that stands for User Datagram Protocol. Usually functioning as a connectionless datagram services with no guarantee of delivering data in a particular sequence. Any source host that requires a reliable communication has options of using either TCP or a program based on a sequential services. Generally all UDP messages are sent with the current IP address through ports. It helps in delivering messages from the source host to the ultimate destination as a postcard; small but not with a guarantee of delivery but TCP works like a telephone by verifying the destination address. Usually UDP is used to deliver a small amount of data to a large number of recipient at one set period of time. So it low over heading and multitasking capacity stands it ahead than TCP.

The Datagram 
The datagram is an unite of packet or packet for transfer in the TCP/IP network. Each datagram packet has data with source and destination address.

Description of program:

When you will run the program then a layout will appear on the screen that contains two buttons 'Start' and 'Stop' with a text area, where you receive your messages. The messages can be received only on that time when the UDP server is started. Whenever the UDP server is stopped then you can't be sent any message by the UDP client. If UDP server is started then it display the the message "Server is started" in the text area otherwise it shows "Server is stopped" or no any message in the text area.

Description of code:

Thread is a constructor of Thread class. The Thread class extends the Object and implements the Runnable. It creates a new thread object. Where you require, the executing of program continuously you can be used thread.

This method starts the thread for executing the program. The run() method is called automatically when the start() method is execute.

The above method interrupts the thread that means breaking the thread for some time. If you want to start the thread again you can start it.

DatagramSocket(int port):
DatagramSocket is the constructor of DatagramSocket class. This class extends Object. It creates the datagram socket and takes only integer  port number. This socket binds the specified port number where the messages is send on the local host machine.

append(String str):
This method is used to insert or append given the string in it.

DatagramPacket(byte[] buffer, int buffer_length):
This constructor provided by the DatagramPacket class. This class extends the Object and imports the java.net.*. It constructs a DatagramPacket to the receiving packet length. It takes the buffer size and buffer length. Where the connectionless services are provided by the user then use it. 

receive(DatagramPacket packet):
The datagram packet received by this method from the specified socket.

This class extends the Object and implements the Serializable. It displays the IP address. 

Above method returns the IP address of the object of InetAddress class, where the datagram packet can be send or receive.

This method returns the port number of the host machine. Where the datagram packets can be send or receive.

This method closes the datagram socket that means you can not be received any messages.

Here is the code of program: 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class UDPServer{
  JFrame frame;
  JPanel panel;
  JButton button1,button2;
  JTextArea area;
  JScrollPane pane;
  Thread thread;
  DatagramSocket socket;

  public static void main(String[] args) {
  UDPServer u = new UDPServer();
  public UDPServer(){
  frame = new JFrame("Text Server");
  panel = new JPanel();
  area = new JTextArea();
  button1 = new JButton("Start");
  button1.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent ae){
  new StartThread();
  button2 = new JButton("Stop");
  button2.addActionListener(new ActionListener(){
  public void actionPerformed (ActionEvent ae){
  area.append("Server is stopped\n");
  pane = new JScrollPane(area);
  public class StartThread implements Runnable{
  thread = new Thread(this);
  public void run(){
  byte[] buffer = new byte[1024];
  int port = 8080;
  socket = new DatagramSocket(port);
  area.append("Server is started\n");
  //Receive request from client
  DatagramPacket packet = 
DatagramPacket(buffer, buffer.length );
  InetAddress client = packet.getAddress();
  int client_port = packet.getPort();
  area.append(" Received "
new String(buffer)+" from "+client);
  catch(UnknownHostException ue){}
  catch(java.net.BindException b){}
  catch (IOException e){

Download this example.