Java网络编程UDP实现多线程在线聊天
作者:跑起来要带风! 时间:2022-01-08 21:48:32
本文实例为大家分享了Java实现多线程在线聊天的具体代码,供大家参考,具体内容如下
上一篇博客通过UDP实现了聊天,但只能单方面发送消息,这次实现了多线程在线聊天,也就是可以双方互发消息。
发送消息:
package com.kuang.chat;
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 fromPort;
private String toIP;
private int toPort;
public TalkSend(int fromPort, String toIP, int toPort) {
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;
try {
socket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (Exception 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 (data.equals("bye")) {
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
接收消息:
package com.kuang.chat;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TalkReceive implements Runnable {
DatagramSocket socket = null;
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);//阻塞式接受包裹
//断开连接 bye
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();
}
}
用户1:学生
package com.kuang.chat;
public class TalkStudent {
public static void main(String[] args) {
//开启两个线程
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
}
用户2:老师
package com.kuang.chat;
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生")).start();
}
}
运行结果:
1、启动学生端,开始运行
2、启动教师端,开始运行
3、学生发送消息,教师收到
4、教师发送消息,学生收到
来源:https://blog.csdn.net/weixin_44668898/article/details/107319623
标签:Java,多线程,聊天
0
投稿
猜你喜欢
Java消息队列JMS实现原理解析
2023-05-14 04:02:04
Java8 Stream流的常用方法汇总
2023-07-17 17:56:59
高斯混合模型与EM算法图文详解
2022-10-02 12:05:02
spring bean.xml文件p标签使用报错的解决
2022-03-12 23:16:16
javax.mail.SendFailedException: Sending failed问题原因
2021-08-07 20:00:57
利用Java生成带有文字的二维码
2022-05-21 15:01:38
Android实现应用内置语言切换功能
2021-11-14 13:19:50
Java 实战项目之精美物流管理系统的实现流程
2023-10-30 19:53:46
浅谈几种常见语言的命名空间(Namespace)
2022-11-13 12:17:42
Java实战之飞翔的小鸟小游戏
2022-10-04 20:37:20
轻松学习C#的异常处理
2022-09-14 22:10:20
DevExpress实现GridView当无数据行时提示消息
2023-08-23 04:13:33
springmvc与mybatis集成配置实例详解
2021-06-16 22:10:27
详解 c# 克隆
2021-12-16 16:23:12
Spring多个数据源配置详解
2023-09-20 18:22:18
Android序列化接口Parcelable与Serializable接口对比
2023-03-24 17:48:59
Ubuntu搭建Java开发环境笔记
2023-10-10 14:27:49
C#语法之泛型的多种应用
2022-03-15 02:02:43
Struts和servlet不能共存问题解决方法
2022-09-20 07:26:59
Spring Cloud Alibaba Nacos 入门详解
2022-02-23 12:29:09