Java网络编程实例——简单模拟在线聊天

网友投稿 261 2023-01-11

Java网络编程实例——简单模拟在线聊天

1、前提知识

需要知道简单的IO流操作,以及简单的UDP发送数据包的原理。

需要用到的类:DatagramSocket、DatagramPacket

UDP数据包基于DatagramSocket发送和接收,DatagramPacket用于封装数据包

看下案例:

客户端发送消息:

正常情况下从控制台读信息,封装到DatagramPacket之中,再由DatagramSocket的send方法发出

读取到bye的时候退出聊天

public class UdpOnlineClient {

public static void main(String[] args) throws IOException {

DatagramSocket socket = new DatagramSocket(10000);

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

while (true) {

String data = bufferedReader.readLine();

byte[] datas = data.getBytes();

DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 9999));

socket.send(packet);

if (data.equals("bye")){

break;

}

}

socket.close();

}

}

服务端接收消息:

DatagramSocket的receive方法接收客户端发送的消息,打印到控制台,如果信息是bye则退出聊天

public class UdpOnlineServer {

public static void main(String[] args) throws IOException {

vBUqtowS

DatagramSocket socket = new DatagramSocket(9999);

while (true){

byte[] buffer = new byte[1024];

DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);

socket.receive(packet);

String msg = new String(packet.getData(), 0, packet.getLength());

System.out.println(msg);

if(msg.equals("bye")){

break;

}

}

socket.close();

}

}

试验:正常开启客户端和服务端后:

客户端发送消息:

服务端接收到信息即成功:

有了这个思路,我们只需要把发送和接收封装成发送类和接收类,使用多线程即可实现聊天了

2、实现思路:

创建UDP发送实现类TaskSend、UDP接收实现类TaskReceive,以及模拟多线程通信的通信类ChatThread1、ChatThread2

UDP发送实现类TaskSend:

public class TaskSend implements Runnable{

private String sendIP;

//自己的服务端口

private int ownPort;

private int sendPort;

//读取控制台输入流

private BufferedReader bufferedReader;

private DatagramSocket socket;

public TaskSend(String sendIP, int ownPort, int sendPort) {

this.sendIP = sendIP;

this.sendPort = sendPort;

try {

socket = new DatagramSocket(ownPort);

bufferedReader = new BufferedReader(new InputStreamReader(System.in));

} catch (SocketException e) {

e.printStackTrace();

} finally {

}

}

//发送消息

public vBUqtowSvoid run() {

while (true) {

try {

String data = bufferedReader.readLine();

//打印自己的消息

System.out.println("我:"+data);

byte[] datas = data.getBytes();

DatagramPacket packet = new DatagramPacket(datas, 0, datas.length,new InetSocketAddress(sendIP,sendPort));

socket.send(packet);

if (data.equals("bye")) {

break;

}

} catch (IOException e) {

e.printStackTrace();

} finally {

}

}

socket.close();

}

}

UDP接收实现类TaskReceive:

public class TaskReceive implements Runnable{

private int ownPort;

//发送信息的人

private String msgOwner;

private DatagramSocket socket;

public TaskReceive(int ownPort, String msgOwner) {

this.msgOwner = msgOwner;

try {

socket = new DatagramSocket(ownPort);

} catch (SocketException e) {

e.printStackTrace();

} finally {

}

}

//接收消息

public void run() {

while (true) {

try {

byte[] buffer = new byte[1024];

DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);

socket.receive(packet);

String msg = new String(packet.getData(), 0, packet.getLength());

System.out.println(msgOwner+":"+msg);

if (msg.equals("bye")) {

break;

}

} catch (IOException e) {

e.printStackTrace();

} finally {

}

}

socket.close();

}

}

之后使用两个通信类开启多线程模拟一下:

模拟老师

public class ChatThread1 {

public static void main(String[] args) {

new Thread(new TaskSend("localhost",9999,10000)).start();

new Thread(new TaskReceive(9998,"老师")).start();

}

}

模拟学生

public class ChatThread2 {

public static void main(String[] args) {

new Thread(new TaskReceive(9999,"学生")).start();

new Thread(new TaskSend("localhost",9998,10001)).start();

}

}

实例:

学生

老师

到此简单的在线聊天功能就完成了,可以巩固一下之前学习的网络知识

以上就是java网络编程实例——简单模拟在线聊天的详细内容,更多关于Java 模拟在线聊天的资料请关注我们其它相关文章!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:有哪些好的免费api接口(api接口有哪几种)
下一篇:国际顺丰快递物流查询单号(国际顺丰快递物流查询单号官网)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~