Receiving and sending a request to UDP server in Java

Here, you will know how to receive and send messages by UDP server.

Receiving and sending a request to UDP server in Java

Here, you will know how to receive and send messages by UDP server.

Receiving and sending a request to UDP server in Java

Receiving and sending a request to UDP server in Java

     

Here, you will know how to receive and send messages by UDP server. First of all, UDP server receives messages and sends some information to UDP client. The brief descriptions are available bellow:

Description of program: 

This program provides you a layout. Which contains two command buttons 'Start' and 'Stop' with a text area. Messages are received by the UDP server in a text area at the time of server is started. If server is start then shows a message "Server is started" in the text area and if stopped then shows a message like: "Server is stopped" and no any information or messages are received by server. Any information are collected by UDP server then it sends a message to UDP client.

Here is the code of program:

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

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

  public static void main(String[] args) {
  UDPSendServerRequest u = new UDPSendServerRequest();
  }
  public UDPSendServerRequest(){
  frame = new JFrame("Text Server");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setUndecorated(true);
  frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
  panel = new JPanel();
  panel.setLayout(null);
  area = new JTextArea();
  area.setEditable(false);
  button1 = new JButton("Start");
  button1.setBounds(210107540);
  button1.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent ae){
  new StartThread();
  }
  });
  panel.add(button1);
  button2 = new JButton("Stop");
  button2.setBounds(300107540);
  button2.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent ae){
  thread.interrupted();
  socket.close();
  area.append("Server is stopped\n");
  button1.setEnabled(true);
  button2.setEnabled(false);
  }
  });
  button2.setEnabled(false);
  panel.add(button2);
  pane = new JScrollPane(area);
  pane.setBounds(1060365250);
  panel.add(pane);
  frame.add(panel);
  frame.setSize(400400);
  frame.setVisible(true);
  }
  public class StartThread implements Runnable{
  StartThread(){
  thread = new Thread(this);
  thread.start();
  button1.setEnabled(false);
  button2.setEnabled(true);
  }
  public void run(){
  try{
  byte[] buffer = new byte[1024];
  int port = 8080;
  String message;
  try{
  socket = new DatagramSocket(port);
  while(true){
  try{
  area.append("Server is started\n");
  //Receive request from client
  DatagramPacket packet = 
 
new DatagramPacket(buffer, buffer.length );
  socket.receive(packet);
  InetAddress client = packet.getAddress();
  int client_port = packet.getPort();
  area.append(" Received "+new String(buffer)+" from "+client);
  // send information to the client
  message = "your request\n ";
  buffer = message.getBytes() ;
  packet = 
  new 
DatagramPacket(buffer, buffer.length,client,client_port);
  socket.send(packet);
  }
  catch(UnknownHostException ue){}
  }
  }
  catch(java.net.BindException b){}
  }
  catch (IOException e){
  System.err.println(e);
  }
  }
  }
}

Download this example.