2021-05-30

Java网络编程学习——简单模拟在线聊天

Java网络编程学习——简单模拟在线聊天

学了java网络,也是该做个小案例来巩固一下了。

本次案例将使用UDP和多线程模拟即时聊天,简单练练手。


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 {  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,以及模拟多线程通信的通信类ChatThread1ChatThread2

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 void 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(); }}

实例:

学生

在这里插入图片描述

老师

在这里插入图片描述


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









原文转载:http://www.shaoqun.com/a/771947.html

跨境电商:https://www.ikjzd.com/

打折网:https://www.ikjzd.com/w/74

kk馆:https://www.ikjzd.com/w/1713


Java网络编程学习——简单模拟在线聊天学了java网络,也是该做个小案例来巩固一下了。本次案例将使用UDP和多线程模拟即时聊天,简单练练手。1、前提知识需要知道简单的IO流操作,以及简单的UDP发送数据包的原理。需要用到的类:DatagramSocket、DatagramPacketUDP数据包基于DatagramSocket发送和接收,DatagramPacket用于封装数据包看下案例:客户端
五洲会:https://www.ikjzd.com/w/1068
周宁:https://www.ikjzd.com/w/1647
cbo:https://www.ikjzd.com/w/2670
谈谈亚马逊关联因素和预防措施:https://www.ikjzd.com/articles/125940
想当跨境电商的小商家怎么做:https://www.ikjzd.com/articles/16211
美国大选如何影响新上架产品的走势?:https://www.ikjzd.com/articles/133568

No comments:

Post a Comment