温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java如何使用UDP实现点对点通信

发布时间:2022-06-15 16:02:15 来源:亿速云 阅读:146 作者:iii 栏目:开发技术

本文小编为大家详细介绍“java如何使用UDP实现点对点通信”,内容详细,步骤清晰,细节处理妥当,希望这篇“java如何使用UDP实现点对点通信”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

整体的结构思路:

Stu和Tea是收发消息的两端;

Stu和Tea类都是通过启动两个线程完成通信的,这两个线程分别是发消息线程和收消息线程;

Stu类中启动的两个线程分别写在两个类里,Tea也是这样做。

Stu端及其收发消息代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
 
public class Stu {
 
    public static void main(String[] args) throws IOException {
        new Thread(new StuSendThread("127.0.0.1", 8888)).start();
        new Thread(new StuReceiverThread(8889)).start();
    }
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
 
 
public class StuReceiverThread implements Runnable{
 
    private int port;
 
    public StuReceiverThread(int port) {
        this.port = port;
    }
    
    @Override
    public void run() {
        // 创建接收方
        DatagramSocket socket;
        try {
            socket = new DatagramSocket(port);
            while(true) {
                // 准备容器
                byte[] buf = new byte[1024];
                DatagramPacket dPacket = new DatagramPacket(buf, buf.length);
                // 阻塞式接受数据
                try {
                    socket.receive(dPacket);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                // 分析数据
                System.out.println(new String(buf));
                if (new String(buf).equals("exit")) {
                    break;
                }
            }
            // 释放资源
            socket.close();
        } catch (SocketException e1) {
            e1.printStackTrace();
        }        
    }    
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
 
public class StuSendThread implements Runnable{
 
    private DatagramSocket socket;
    private BufferedReader br;
    String IP;
    int port;
    
    public StuSendThread(String IP, int port) throws SocketException {
        socket = new DatagramSocket();
        br = new BufferedReader(new InputStreamReader(System.in));
        this.IP = IP;
        this.port = port;
    }
    
    @Override
    public void run() {        
        while(true) {
            // 准备数据
            br = new BufferedReader(new InputStreamReader(System.in));
            String msg;
            try {
                msg = br.readLine();
                byte[] data = msg.getBytes();
                // 封装数据
                DatagramPacket dPacket = new DatagramPacket(data, data.length, InetAddress.getByName(IP), port);
                // 发送数据
                socket.send(dPacket);
                if(msg.equals("exit")) {
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
        
        // 关闭资源
        try {
            br.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
 
}

Tea端及其收发消息代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
 
public class TeaSendThread implements Runnable{
    private DatagramSocket socket;
    private BufferedReader br;
    String IP;
    int port;
 
    public TeaSendThread(String IP, int port) throws SocketException {
        socket = new DatagramSocket();
        br = new BufferedReader(new InputStreamReader(System.in));
        this.IP = IP;
        this.port = port;
    }
 
    @Override
    public void run() {
        while (true) {
            // 准备数据
            br = new BufferedReader(new InputStreamReader(System.in));
            String msg;
            try {
                msg = br.readLine();
                byte[] data = msg.getBytes();
                // 封装数据
                DatagramPacket dPacket = new DatagramPacket(data, data.length, InetAddress.getByName(IP), port);
                // 发送数据
                socket.send(dPacket);
                if (msg.equals("exit")) {
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
 
        }
 
        // 关闭资源
        try {
            br.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
    }
 
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
 
public class TeaReceiverTread implements Runnable{
    private int port;
 
    public TeaReceiverTread(int port) {
        this.port = port;
    }
    
    @Override
    public void run() {
        // 创建接收方
        DatagramSocket socket;
        try {
            socket = new DatagramSocket(port);
            while(true) {
                // 准备容器
                byte[] buf = new byte[1024];
                DatagramPacket dPacket = new DatagramPacket(buf, buf.length);
                // 阻塞式接受数据
                try {
                    socket.receive(dPacket);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                // 分析数据
                System.out.println(new String(buf));
                if (new String(buf).equals("exit")) {
                    break;
                }
            }
            // 释放资源
            socket.close();
        } catch (SocketException e1) {
            e1.printStackTrace();
        }        
    }    
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
 
public class TeaSendThread implements Runnable{
    private DatagramSocket socket;
    private BufferedReader br;
    String IP;
    int port;
 
    public TeaSendThread(String IP, int port) throws SocketException {
        socket = new DatagramSocket();
        br = new BufferedReader(new InputStreamReader(System.in));
        this.IP = IP;
        this.port = port;
    }
 
    @Override
    public void run() {
        while (true) {
            // 准备数据
            br = new BufferedReader(new InputStreamReader(System.in));
            String msg;
            try {
                msg = br.readLine();
                byte[] data = msg.getBytes();
                // 封装数据
                DatagramPacket dPacket = new DatagramPacket(data, data.length, InetAddress.getByName(IP), port);
                // 发送数据
                socket.send(dPacket);
                if (msg.equals("exit")) {
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
 
        }
 
        // 关闭资源
        try {
            br.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
    }
 
}

读到这里,这篇“java如何使用UDP实现点对点通信”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI