Sends the specified text or file as a datagram to the specified port of the specified host : DatagramPacket « Network Protocol « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Network Protocol » DatagramPacketScreenshots 
Sends the specified text or file as a datagram to the specified port of the specified host

 
/*
 * Copyright (c) 2004 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 3nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose,
 * including teaching and use in open-source projects.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book, 
 * please visit http://www.davidflanagan.com/javaexamples3.
 */
//package je3.net;
import java.io.File;
import java.io.FileInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * This class sends the specified text or file as a datagram to the specified
 * port of the specified host.
 */
public class UDPSend {
  public static final String usage = "Usage: java UDPSend <hostname> <port> <msg>...\n"
      "   or: java UDPSend <hostname> <port> -f <file>";

  public static void main(String args[]) {
    try {
      // Check the number of arguments
      if (args.length < 3)
        throw new IllegalArgumentException("Wrong number of args");

      // Parse the arguments
      String host = args[0];
      int port = Integer.parseInt(args[1]);

      // Figure out the message to send.
      // If the third argument is -f, then send the contents of the file
      // specified as the fourth argument. Otherwise, concatenate the
      // third and all remaining arguments and send that.
      byte[] message;
      if (args[2].equals("-f")) {
        File f = new File(args[3]);
        int len = (intf.length()// figure out how big the file is
        message = new byte[len]// create a buffer big enough
        FileInputStream in = new FileInputStream(f);
        int bytes_read = 0, n;
        do // loop until we've read it all
          n = in.read(message, bytes_read, len - bytes_read);
          bytes_read += n;
        while ((bytes_read < len&& (n != -1));
      else // Otherwise, just combine all the remaining arguments.
        String msg = args[2];
        for (int i = 3; i < args.length; i++)
          msg += " " + args[i];
        // Convert the message to bytes using UTF-8 encoding
        message = msg.getBytes("UTF-8");
      }

      // Get the internet address of the specified host
      InetAddress address = InetAddress.getByName(host);

      // Initialize a datagram packet with data and address
      DatagramPacket packet = new DatagramPacket(message, message.length, address, port);

      // Create a datagram socket, send the packet through it, close it.
      DatagramSocket dsocket = new DatagramSocket();
      dsocket.send(packet);
      dsocket.close();
    catch (Exception e) {
      System.err.println(e);
      System.err.println(usage);
    }
  }
}

 
Related examples in the same category
1.Get data from DatagramPacket
2.Get address and port from DatagramPacket
3.Create DatagramPacket from byte array
4.Waits to receive datagrams sent the specified portWaits to receive datagrams sent the specified port
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.