Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
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. 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.

                         

» View all related tutorials
Related Tags: java c com server layout time button io graph screen command this disable pear cast tar app cas buttons start

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.