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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
UDP Client in Java 
 

In this section, you will know how to send any request or messages for UDP server by the UDP client.

 

UDP Client in Java

                         

In this section, you will know how to send any request or messages for UDP server by the UDP client. For this process you must require the destination IP address and destination port number. If you know both, then you send messages to UDP server. The sending process has been defined just  bellow:

Description of program:

Here, providing you a layout with the help of this program, which has destination IP address, destination port number, a text area and a "Send" command button. The IP address specify local host and destination port number takes a specific port number where you want to send the message. All messages written in the text area and finally click the send command button, messages are sent to the specific destination.

Description of code:

getByName(String socket):
This method identify the IP address of the host machine (localhost) which is entered in string of this method.

getBytes():
This method returns a sequence of bytes of the string.

DatagramPacket(byte[] message, int message_length, InetAddress address, int pnum):
This is the constructor of DatagramPacket class. The DatagramPacket class extends the Object and shows the datagam packet. It creates a datagram packet for sending messages on the specified host machine with the help of specified port number. The length of datagram packet depends upon messages size.

socket.send(DatagramPacket packet):
It can be used to send the datagarm packet to specified socket.

Here is the code of program:

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

public class UDPClient{
  JFrame frame;
  JPanel panel;
  JTextField field1, field2;
  JTextArea area;
  JScrollPane pane;
  JLabel label;
  JButton button;
  public static void main(String[] args) {
    UDPClient u = new UDPClient();
  }
  public UDPClient(){
    frame = new JFrame("Text Client");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setUndecorated(true);
    frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
    panel = new JPanel();
    panel.setLayout(null);
    label = new JLabel("Desination IP:");
    label.setBounds(102010030);
    panel.add(label);
    field1 = new JTextField(20);
    field1.setBounds(1252515020);
    panel.add(field1);
    label = new JLabel("Destination Port:");
    label.setBounds(105010030);
    panel.add(label);
    field2 = new JTextField(10);
    field2.setBounds(1255510020);
    panel.add(field2);
    area = new JTextArea();
    pane = new JScrollPane(area);
    pane.setBounds(10100300200);
    panel.add(pane);
    button = new JButton("Send");
    button.setBounds(2353107530);
    button.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent ae){
        new SendRequest();
      }
    });
    panel.add(button);
    frame.add(panel);
    frame.setSize(400400);
    frame.setVisible(true);
  }
  public class SendRequest{
    SendRequest(){
      try{
        DatagramSocket socket;
        DatagramPacket packet;
        InetAddress address;
        socket = new DatagramSocket();
        String dip = field1.getText();
        address = InetAddress.getByName(dip);
        String port = field2.getText();
        int pnum = Integer.parseInt(port);
        String mess = area.getText();
        byte message[] = mess.getBytes();
        packet = new DatagramPacket(message, message.length, address, pnum);
        socket.send(packet);
        area.setText("");
        socket.close();
      }
      catch(IOException io){}
    }
  }
}

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
Software Solutions
Least Viewed
Most Rated
Recently Viewed
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.