Python 官方文档:入门教程 => 点击学习
本文实例为大家分享了Java实现UDP多线程在线咨询,供大家参考,具体内容如下 1.发送的线程 import java.io.BufferedReader; import jav
本文实例为大家分享了Java实现UDP多线程在线咨询,供大家参考,具体内容如下
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
//发送线程
public class TalkSend implements Runnable {
DatagramSocket socket = null;
BufferedReader reader = null;
private int fORMPort; //自身端口
private String toIP; //发送到的IP
private int toPort; //发送到的端口
public TalkSend(int formPort, String toIP, int toPort) {
this.formPort = formPort;
this.toIP = toIP;
this.toPort = toPort;
try {
socket = new DatagramSocket(formPort);
reader = new BufferedReader(new InputStreamReader(System.in)); //获取控制台输入
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while(true) {
try {
String data = reader.readLine(); //获取到的数据
byte[] datas = data.getBytes(); //转换为需要的字节数组
//将数据打包成包
DatagramPacket packet = new DatagramPacket(datas,0, datas.length,new InetSocketAddress(this.toIP,this.toPort));
socket.send(packet); //发送包
//退出指令
if(packet.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
//接收线程
public class TalkReceive implements Runnable {
DatagramSocket socket = null; //与发送端的socket为同一个,实现通信
private int port;
private String msgFrom; //标志信息来源
public TalkReceive(int port, String msgFrom) {
this.port = port;
this.msgFrom= msgFrom;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
socket.receive(packet);//接收数据
byte[] data = packet.getData();
String receiveData = new String(data, 0, data.length);
System.out.println(msgFrom+":"+receiveData);
if(receiveData.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
//模拟学生
public class TalkStudent {
public static void main(String[] args) {
new Thread(new TalkSend(7777, "localhost", 9999)).start();
new Thread(new TalkReceive(8888, "老师")).start();
}
}
//模拟老师
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend(5555, "localhost", 8888)).start();
new Thread(new TalkReceive(9999, "学生")).start();
}
}
学生端:
老师端:
首先创建一个发送线程,里面定义要发送到的IP地址和端口,再创建一个DatagramSocket来进行通信,创建一个BufferReader(new InputStreamReader(System.in))来获取控制台的输入的数据,将获取到的数据打包进行发送。之后创建一个接收线程,里面定义自身的端口即可,再创建一个和发送端一样的DatagramSocket来进行通信,接收数据进行打印输出即可。最后模拟学生端和老师端,创建这两个线程来进行通信。学生发送线程到的端口是老师的接收端口,老师发送的端口是学生的接收端口,这样才能互相通信。
--结束END--
本文标题: Java实现UDP多线程在线咨询
本文链接: https://lsjlt.com/news/124066.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0