您好,登录后才能下订单哦!
本篇文章为大家展示了怎么在Java中利用网络编程模拟在线聊天,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
需要知道简单的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(); } }
试验:正常开启客户端和服务端后:
客户端发送消息:
服务端接收到信息即成功:
有了这个思路,我们只需要把发送和接收封装成发送类和接收类,使用多线程即可实现聊天了
创建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 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(); } }
实例:
学生
老师
上述内容就是怎么在Java中利用网络编程模拟在线聊天,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。