import java.net.*;
import java.io.*;
public class Example13_2{
   public static void main(String[] args)
{
     try{     
        DatagramSocket dSocket = new DatagramSocket();
        //创建长度为1024字节的数据缓冲区
        byte[] buffer = new byte[1024];
        //从键盘输入发送的信息
        System.out.print("输入发送的信息：");
        System.in.read(buffer);
        //根据数据缓冲区和数据报分组长度构建DatagramPacket对象
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("127.0.0.1"),8008);
         //发送数据报到服务端
         dSocket.send(packet);
         System.out.println("客户端发送数据报完毕!");
         //接收数据报
         dSocket.receive(packet);
         System.out.println("客户端接收返回的数据报!");                    
     }
catch(IOException e)
{
          System.out.println(e.toString());
     }
  }
}  

class UDPServer 
{
   public static void main(String[] args)
{
     try{
        DatagramSocket dSocket = new DatagramSocket(8008);
        //创建长度为1024字节的数据缓冲区
        byte[] buffer = new byte[1024];
        //根据数据缓冲区和数据报分组长度构建DatagramPacket对象
        DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
        //接收数据报
        dSocket.receive(packet);
        System.out.println("服务器收到数据报!");
        //将接收的数据报再发回客户端
        dSocket.send(packet);        
     }
catch(SocketException e)
     {
          System.out.println(e.toString());
     }
     catch(IOException e)
{
          System.out.println(e.toString());
     }
  }
}
