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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Sending and receiving information to the UDP Client in Java 
 

Here, you will provide send and receive information by the UDP client in Java.

 

Sending and receiving information to the UDP Client in Java

                         

Here, you will provide send and receive information by the UDP client in Java. UDP client sends information or messages to UDP server and it collects some messages from UDP server.

Description of program:

When you will run the program then appears a graphical layout on the screen. Which has destination IP (Internet protocol) address (localhost or 127.0.0.1), destination port number (8080), a text area and a 'Send' command button. All information or messages are written by client in a text area after that you clicked on the 'Send' button then messages to be send into the UDP server and it also sends a message to UDP client in the text area. Therefore UDP client can identify that your sending information are received or not by the UDP server.

Here is the code of program:

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

public class RecUDPClient{
  JFrame frame;
  JPanel panel;
  JTextField field1, field2;
  JTextArea area;
  JScrollPane pane;
  JLabel label;
  JButton button;
  public static void main(String[] args) {
    RecUDPClient u = new RecUDPClient();
  }
  public RecUDPClient(){
    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);
        //For send the message by the client
        String mess = area.getText();
        byte message[] = mess.getBytes();
        packet = new DatagramPacket(message, message.length, address, pnum);
        socket.send(packet);
        area.setText("");
        //For Received message
        packet = new DatagramPacket(message, message.length);
        socket.receive(packet);
        String recmessage = new String(packet.getData());
        area.append("Received from server: " + recmessage);
          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 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

please make clear about how to access the server and client programs in one program using udp protocal in java

Posted by sunayana on Wednesday, 09.17.08 @ 12:44pm | #80035

It is a good program for beginner

I have a question if You can answer me on my email address

Q1
i have to message the those computer connected from my Lan Card at a time ...suppose i have two Lan cards in my computer my code will take out the address of that LAN cards and send the message to the computer connected to it

Q1
i have to take out the IP address of LAN card using UDP automatically and it can bind that address for messaging

Posted by aamir on Wednesday, 12.27.06 @ 15:06pm | #2097

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.