package javas.socket;

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

/**
 * UDP
 * DatagramSocketServer
 *
 * @author sunny
 * @version 1.0.0
 * @since 2015-07-21
 */
@SuppressWarnings("ALL")
public class DatagramSocketServer {

    public static final Integer PORT = 2015;

    public static void main(String[] args) {
        try {
            //启动服务端
            DatagramSocket socket = new DatagramSocket(PORT);

            //接收数据
            DatagramPacket datagramPacket = new DatagramPacket(new byte[1024], 1024);
            while (true) {
                socket.receive(datagramPacket);
                String message = new String(datagramPacket.getData(), "UTF-8");
                System.out.println("server receive data is " + message);
            }
        } catch (SocketException e) {
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

}
