java如何使用UDP实现点对点通信
短信预约 -IT技能 免费直播动态提醒
本文小编为大家详细介绍“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实现点对点通信”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注编程网行业资讯频道。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341