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(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)