Java by API/java.net/DatagramPacket

Материал из Java эксперт
Версия от 14:16, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

DatagramPacket: getAddress()

/*
 * Output:
 *  
 */
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class MainClass {
  private final static int BUFSIZE = 20;
  public static void main(String args[]) {
    try {
      int port = 80;
      DatagramSocket ds = new DatagramSocket(port);
      byte buffer[] = new byte[BUFSIZE];
      while (true) {
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
        // Receive data
        ds.receive(dp);
        // Display address from the datagram packet
        InetAddress ia = dp.getAddress();
        System.out.println(ia);
        // Display port from the datagram packet
        System.out.println(dp.getPort());
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}





DatagramPacket: getData()

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public static void main(String[] args) {
    try {
      byte[] buffer = new byte[1024];
      DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
      DatagramSocket socket = new DatagramSocket(5002);
      System.out.println("Waiting for a packet...");
      socket.receive(packet);
      System.out.println("Just received packet from " + packet.getSocketAddress());
      buffer = packet.getData();
      System.out.println(new String(buffer));
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}





DatagramPacket: getPort()

/*
 * Output:
 *  
 */
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class MainClass {
  private final static int BUFSIZE = 20;
  public static void main(String args[]) {
    try {
      int port = 80;
      DatagramSocket ds = new DatagramSocket(port);
      byte buffer[] = new byte[BUFSIZE];
      while (true) {
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
        // Receive data
        ds.receive(dp);
        // Display address from the datagram packet
        InetAddress ia = dp.getAddress();
        System.out.println(ia);
        // Display port from the datagram packet
        System.out.println(dp.getPort());
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}





DatagramPacket: getSocketAddress()

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public static void main(String[] args) {
    try {
      byte[] buffer = new byte[1024];
      DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
      DatagramSocket socket = new DatagramSocket(5002);
      System.out.println("Waiting for a packet...");
      socket.receive(packet);
      System.out.println("Just received packet from " + packet.getSocketAddress());
      buffer = packet.getData();
      System.out.println(new String(buffer));
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}





new DatagramPacket(byte[] buffer, int bufferLength)

/*
 * Output:
 *  
 */
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class MainClass {
  private final static int BUFSIZE = 20;
  public static void main(String args[]) {
    try {
      int port = 80;
      DatagramSocket ds = new DatagramSocket(port);
      while (true) {
        byte buffer[] = new byte[BUFSIZE];
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
        ds.receive(dp);
        String str = new String(dp.getData());
        System.out.println(str);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}





new DatagramPacket(byte[] buf, int length, InetAddress address, int port)

/*
 * Output:
 *  
 */
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class MainClass {
  public static void main(String args[]) {
    try {
      InetAddress ia = InetAddress.getByName("www.jexp.ru");
      DatagramSocket ds = new DatagramSocket();
      byte buffer[] = "hello".getBytes();
      DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);
      // Send the datagram packet
      ds.send(dp);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}