Send out UDP pockets : UDP « Network Protocol « Java






Send out UDP pockets

   

import java.io.*;
import java.net.*;

public class UDPSend {
  public static void main(String args[]) {
    try {
      String host = "www.java2s.com";
      int port = 90;

      byte[] message = "Java Source and Support".getBytes();

      // 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);
    }
  }
}

           
         
    
    
  








Related examples in the same category

1.Udp Echo Server
2.DatagramSocket Server
3.DatagramSocket Client
4.Receive UDP pocketsReceive UDP pockets
5.Experiment with UDP sockets
6.Using Datagrams to Get the Date
7.An echo server using UDP socketsAn echo server using UDP sockets
8.Connect to a daytime server using the UDP protocol
9.Handles TCP and UDP connections and provides exception handling and error logging
10.UDP OutputStream
11.UDP InputStream
12.Performs broadcast and multicast peer detection.