package pack;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/*public class Demo {
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        //method1();
    }
    public static void method1() throws Exception {
        InetAddress i = InetAddress.getLocalHost();
        System.out.println(i.toString());
        InetAddress ia = InetAddress.getByName("www.baidu.com");
        System.out.println(ia.getHostAddress());
    }
}
发送接收数据
class UdpSend {
    public static void main(String[] args) throws Exception {
        //1.创建udp服务,通过DatagramSocket对象
        DatagramSocket ds = new DatagramSocket();
        //2.确定数据,封装成包
        byte[] buf = "udp send".getBytes();
        DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.2"),10000);
        //3.通过socket服务,将已有的数据包发送出去
        ds.send(dp);
        ds.close();
    }
}
class UdpReceive {
    public static void main(String[] args) throws Exception {
        //1.创建udp socket,建立端点,指定端口
        DatagramSocket ds = new DatagramSocket(10000);
        while(true) {
            //2.定义数据包,用于存储数据
            byte[] buf = new byte[1024];
            DatagramPacket dp = new DatagramPacket(buf,buf.length);
            //3.通过服务的receive接收数据
            ds.receive(dp);
            //4.通过数据包对象获取ip地址与数据
            String ip = dp.getAddress().getHostAddress();
            String data = new String(dp.getData(),0,dp.getLength());
            int port = dp.getPort();
            System.out.println(ip+":"+data+":"+port);
            ds.close();
        }
    }
}
 键盘录入发送数据
class UdpSend2 {
    public static void main(String[] args) throws Exception {
        DatagramSocket ds = new DatagramSocket();
        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
        String line = bufr.readLine();
        while(line!=null) {
            if("over".equals(line)) {
                break;
            }
            byte[] buf = line.getBytes();
            DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.10"),10001);
            ds.send(dp);
        }
    }
}
class UdpRece2 {
    public static void main(String[] args) throws Exception {
        DatagramSocket ds = new DatagramSocket(100001);
        while(true) {
            byte[] buf =new byte[1024];
            DatagramPacket dp = new DatagramPacket(buf,buf.length);
            ds.receive(dp);
            String ip = dp.getAddress().getHostAddress();
            String data = new String(dp.getData(),0,dp.getLength());
            System.out.println(ip+":"+data);
        }
    }
}
*/
/*聊天程序*/
public class Demo {
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        DatagramSocket sendSocket = new DatagramSocket();
        DatagramSocket receSocket = new DatagramSocket(10001);
        new Thread(new Send(sendSocket)).start();
        new Thread(new Rece(receSocket)).start();
    }
}
class Send implements Runnable {
    private DatagramSocket ds;
    Send(DatagramSocket ds) {
        this.ds = ds;
    }
    public void run() {
        try {
            BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
            String line = bufr.readLine();
            while(line!=null) {
                if("over".equals(line))
                    break;
            byte[] buf = line.getBytes();
            DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.10"),10001);
            ds.send(dp);
            }
        }
        catch(Exception e) {
            System.out.println("error");
        }
    }
}
class Rece implements Runnable {
    private DatagramSocket ds;
    Rece(DatagramSocket ds) {
        this.ds = ds;
    }
    public void run() {
        try {
            while(true) { 
            byte[] buf =new byte[1024];
            DatagramPacket dp = new DatagramPacket(buf,buf.length);
            ds.receive(dp);
            String ip = dp.getAddress().getHostAddress();
            String data = new String(dp.getData(),0,dp.getLength());
            System.out.println(ip+":"+data);
            }
        }
        catch(Exception e) {
            System.out.println("error");
        }
    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/sjtu_chenchen/article/details/47177579