User Datagram Protocol Programming : DatagramSocket « Network Protocol « Java






User Datagram Protocol Programming

 

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Main {
  final private static int DAYTIME_PORT = 13;
  public static void main(String args[]) throws IOException {
    String host = args[0];
    byte message[] = new byte[256];
    InetAddress address = InetAddress.getByName(host);
    System.out.println("Checking at: " + address);
    DatagramPacket packet = new DatagramPacket(message, message.length, address, DAYTIME_PORT);
    DatagramSocket socket = new DatagramSocket();
    socket.send(packet);
    packet = new DatagramPacket(message, message.length);
    socket.receive(packet);
    String time = new String(packet.getData());
    System.out.println(time);
    socket.close();
  }
}

   
  








Related examples in the same category

1.Use DatagramSocket to send out and receive DatagramPacket
2.DatagramSocket sends out DatagramPacket
3.DatagramSocket receives DatagramPacket
4.Datagram Sender
5.Sending a Datagram
6.Receiving a Datagram
7.Joining a Multicast Group
8.Receiving from a Multicast Group
9.Sending to a Multicast Group
10.Receive DatagramPacket
11.Send a Datagram
12.Read and write with DatagramPacket
13.Send back the response in a DatagramPacket