udp点对点传输,udp只支持点对点通信
本文实例为大家分享了爪哇岛使用用户数据报协议(用户数据报协议)实现点对点通信的具体代码,供大家参考,具体内容如下
整体的结构思路:
斯图(男子名)和茶是收发消息的两端;
斯图(男子名)和茶类都是通过启动两个线程完成通信的,这两个线程分别是发消息线程和收消息线程;
斯图(男子名)类中启动的两个线程分别写在两个类里,茶也是这样做。
Stu端及其收发消息代码:
导入Java。io。缓冲阅读器;导入Java。io。io异常;导入Java。io。inputstreamreader导入Java。网。数据报包;导入Java。网。数据报套接字;导入Java。网。inet地址;导入Java。网。套接字异常;导入Java。网。unknownhostexception公共类stu { public static void main(String[]args)抛出io异常{新线程(新StuSendThread( 127。0 .0 .1 ,8888))。start();新螺纹(新结构螺纹(8889))。start();} }导入Java。io。io异常;导入Java。网。数据报包;导入Java。网。数据报套接字;导入Java。网。套接字异常;公共类结构通用螺纹实现runnable { private int port public StuReceiverThread(int port){ this。port=端口;} @覆盖公共void run() { //创建接收方关闭套接字;尝试{ socket=new datagram socket(port);while(true) { //准备容器字节[]缓冲区=新字节[1024];数据报包d包=新数据报包(buf,buf。长度);//阻塞式接受数据试试{插座。接收(d包);} catch(io异常e){ e . printstacktrace();} //分析数据System.out.println(新字符串(buf));如果(新字符串(buf).equals( exit ){ break;} } //释放资源插座。close();} catch(套接字异常E1){ E1。printstacktrace();} } }导入Java。io。缓冲阅读器;导入Java。io。io异常;导入Java。io。inputstreamreader导入Java。网。数据报包;导入Java。网。数据报套接字;导入Java。网。inet地址;导入Java。网。套接字异常;公共类StuSendThread实现Runnable {私有数据报套接字套接字;私人BufferedReader br字符串IP;(同Internationalorganizations)国际组织端口;公共StuSendThread(字符串互联网协议(Internet Protocol)
, 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(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持盛行IT。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。