这篇文章主要为大家详细介绍了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实现点对点通信
基础教程推荐
猜你喜欢
- Java并发编程进阶之线程控制篇 2023-03-07
- Java文件管理操作的知识点整理 2023-05-19
- java实现多人聊天系统 2023-05-19
- Java实现查找文件和替换文件内容 2023-04-06
- ConditionalOnProperty配置swagger不生效问题及解决 2023-01-02
- Java数据结构之对象比较详解 2023-03-07
- springboot自定义starter方法及注解实例 2023-03-31
- Java实现线程插队的示例代码 2022-09-03
- JDK数组阻塞队列源码深入分析总结 2023-04-18
- java基础知识之FileInputStream流的使用 2023-08-11