网络编程_UDP协议_聊天程序

发送端:(将数据源改为键盘录入)

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 UDPSendDemo {

    public static void main(String[] args) throws IOException {

        

        System.out.println("发送端启动.......");

        

        //1.udp的socket服务,使用DatagramSocket对象

        DatagramSocket ds = new DatagramSocket();

        

        //2.将要发送的数据封装到数据包中

        //使用DatagramPacket将数据封装到该对象包中

        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));

        

        String line = null;

        

        while((line = bufr.readLine())!=null){

            byte[] buf = line.getBytes();

            DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("127.0.0.1"), 10000);

            ds.send(dp);

            if("over".equals(line))

                break;

        }

        

        ds.close();

        

    }



}

 接收端:

import java.io.IOException;

import java.net.DatagramPacket;

import java.net.DatagramSocket;



public class UDPReceiveDemo {



    public static void main(String[] args) throws IOException {

        

        System.out.println("接收端启动.......");

            //1.建立udp socket服务

        DatagramSocket ds = new DatagramSocket(10000);

        while(true){

                //2.创建数据包

                byte[] buf = new byte[1024];

                DatagramPacket dp = new DatagramPacket(buf, buf.length);

                

                //3.使用接收方法将数据存储在数据包中

                ds.receive(dp);

                

                

                //4.通过数据包对象的方法,解析其中的数据,比如:地址,端口,数据内容

                String ip = dp.getAddress().getHostAddress();

                int port = dp.getPort();

                String text = new String(dp.getData(),0,dp.getLength());

                

                System.out.println(ip+":"+port+":"+text);

        }

        //5.关闭资源

        //ds.close();

    }



}

 

 

 

基于多线程的聊天程序:

Send.java

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;



public class Send implements Runnable {



    private DatagramSocket ds;

    

    public Send(DatagramSocket ds){

        this.ds = ds;

    }

    

    @Override

    public void run() {

        

        try {

            BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));

            String line = null;

            

            while((line=bufr.readLine())!=null){

                

                byte[] buf = line.getBytes();

                DatagramPacket dp = 

                        new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.255"),10001);

                ds.send(dp);

                

                if("886".equals(line))

                    break;

            }

            

            ds.close();

        } catch (Exception e) {



        }

    }



}

 

Rece.java

import java.net.DatagramPacket;

import java.net.DatagramSocket;



public class Rece implements Runnable {



    private DatagramSocket ds;



    public Rece(DatagramSocket ds) {

        this.ds = ds;

    }



    @Override

    public void run() {

        try {

            while (true) {



                // 2,创建数据包。

                byte[] buf = new byte[1024];

                DatagramPacket dp = new DatagramPacket(buf, buf.length);



                // 3,使用接收方法将数据存储到数据包中。

                ds.receive(dp);// 阻塞式的。



                // 4,通过数据包对象的方法,解析其中的数据,比如,地址,端口,数据内容。

                String ip = dp.getAddress().getHostAddress();

                int port = dp.getPort();

                String text = new String(dp.getData(), 0, dp.getLength());

                

                System.out.println(ip + "::" + text);

                if(text.equals("886")){

                    System.out.println(ip+"....退出聊天室");

                }



            }

        } catch (Exception e) {



        }



    }



}

ChatDemo.java

import java.io.IOException;

import java.net.DatagramSocket;

import java.net.SocketException;



public class ChatDemo {



    /**

     * @param args

     * @throws IOException 

     */

    public static void main(String[] args) throws IOException {



        

        DatagramSocket send = new DatagramSocket();

        

        DatagramSocket rece = new DatagramSocket(10001);

        new Thread(new Send(send)).start();

        new Thread(new Rece(rece)).start();

        

    }



}

 

 

 

 

你可能感兴趣的:(网络编程)