Java教程

UDP

本文主要是介绍UDP,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.7UDP

发短信:不用连接,需要对方的地址

发送端:

package com.zishi.lesson3;
​
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
​
//不需要来凝结服务器
public class UdpClientDemo01 {
    public static void main(String[] args) throws Exception {
        //1.建立一个Socket
        DatagramSocket socket = new DatagramSocket();
​
        //2.建个包
        String msg = "你好啊,服务器";
        InetAddress localhost = InetAddress.getByName("localhost");     //发送给谁
        int port = 9090;
​
        //参数:数据,数据起始长度,发生给谁,
        DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);
​
        //3.发生包
        socket.send(packet);
​
        //4.关闭流
        socket.close();
    }
}

接收端:

package com.zishi.lesson3;
​
import java.net.DatagramPacket;
import java.net.DatagramSocket;
​
//还是要等待客户端的连接
public class UdpServerDemo01 {
    public static void main(String[] args) throws Exception{
        //开放端口
        DatagramSocket socket = new DatagramSocket(9090);
        //接收数据包
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
​
        socket.receive(packet);//阻塞接收
​
        System.out.println(packet.getAddress().getHostAddress());
        System.out.println(new String(packet.getData(),0,packet.getLength()));
​
​
        //关闭连接
        socket.close();
    }
}

UDP聊天实现:循环发送

发送端:

package com.zishi.chat;
​
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
​
public class UdpSenderDemo01 {
    public static void main(String[] args) throws Exception{
        DatagramSocket socket = new DatagramSocket(8888);
​
        //准备数据:控制台读取System.in
​
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
​
        while(true){
            String data = reader.readLine();
            byte[] datas = data.getBytes();
            DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 6666));
​
            socket.send(packet);
            if (data.equals("bye")){
                break;
            }
        }
​
        socket.close();
    }
}

接收端:

package com.zishi.chat;
​
import java.net.DatagramPacket;
import java.net.DatagramSocket;
​
public class UdpReceiveDemo01 {
    public static void main(String[] args) throws Exception{
        DatagramSocket socket = new DatagramSocket(6666);
​
        while (true){
​
            //准备接收baoguo
            byte[] container = new byte[1024];
            DatagramPacket packet = new DatagramPacket(container, 0, container.length);
            socket.receive(packet);//阻塞式接收
​
            //断开连接  bye
            byte[] data = packet.getData();
            String recevieData = new String(data, 0, data.length);
​
            System.out.println(recevieData);
​
            if (recevieData.equals("bye")){
                break;
            }
        }
    }
}

多线程聊天:

发送端:

package com.zishi.chat;
​
​
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
​
public class TalkSend implements Runnable{
​
    DatagramSocket socket = null;
    BufferedReader reader = null;
​
    private int formPort;
    private String toIP;
    private int toPort;
​
    public TalkSend(int formPort, String toIP, int toPort) {
        this.formPort = formPort;
        this.toIP = toIP;
        this.toPort = toPort;
​
​
        try {
            socket = new DatagramSocket(formPort);
            reader = new BufferedReader(new InputStreamReader(System.in));
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
​
    @Override
    public void run() {
​
        while(true){
            try {
                String data = reader.readLine();
                byte[] datas = data.getBytes();
                DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(this.toIP, this.toPort));
​
                socket.send(packet);
                if (data.equals("bye")){
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
​
        socket.close();
    }
}

接收端:

package com.zishi.chat;
​
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
​
public class TalkReceive implements Runnable{
​
    DatagramSocket socket = null;
    private int port;
    private String msgForm;
​
    public TalkReceive(int port,String msgForm) {
        this.port = port;
        this.msgForm = msgForm;
        try {
            socket = new DatagramSocket(port);
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
​
    @Override
    public void run() {
​
        while (true){
            try {
                //准备接收包裹
                byte[] container = new byte[1024];
                DatagramPacket packet = new DatagramPacket(container, 0, container.length);
                socket.receive(packet);//阻塞式接收
​
                //断开连接  bye
                byte[] data = packet.getData();
                String recevieData = new String(data, 0, data.length);
​
                System.out.println(msgForm+":"+recevieData);
​
                if (recevieData.equals("bye")){
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
​
        socket.close();
    }
}

学生:

package com.zishi.chat;
​
public class TalkStudent {
    public static void main(String[] args) {
        //开启两个线程
        new Thread(new TalkSend(7777,"localhost",9999)).start();
        new Thread(new TalkReceive(8888,"老师")).start();
    }
}

老师:

package com.zishi.chat;
​
public class TalkTeacher {
    public static void main(String[] args) {
        new Thread(new TalkSend(5556,"localhost",8888)).start();
        new Thread(new TalkReceive(9999,"学生")).start();
    }
}

1.8 URL

这篇关于UDP的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!