Java网络编程UDP实现多线程在线聊天

时间:2022-09-12 10:38:52

本文实例为大家分享了Java实现多线程在线聊天的具体代码,供大家参考,具体内容如下

上一篇博客通过UDP实现了聊天,但只能单方面发送消息,这次实现了多线程在线聊天,也就是可以双方互发消息。

发送消息:

  1. package com.kuang.chat;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.DatagramPacket;
  7. import java.net.DatagramSocket;
  8. import java.net.InetSocketAddress;
  9. import java.net.SocketException;
  10.  
  11. public class TalkSend implements Runnable {
  12. DatagramSocket socket = null;
  13. BufferedReader reader = null;
  14.  
  15. private int fromPort;
  16. private String toIP;
  17. private int toPort;
  18.  
  19. public TalkSend(int fromPort, String toIP, int toPort) {
  20. this.fromPort = fromPort;
  21. this.toIP = toIP;
  22. this.toPort = toPort;
  23.  
  24. try {
  25. socket = new DatagramSocket(fromPort);
  26. reader = new BufferedReader(new InputStreamReader(System.in));
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. }
  31.  
  32. @Override
  33. public void run() {
  34. while (true) {
  35. try {
  36. String data = reader.readLine();
  37. byte[] datas = data.getBytes();
  38. DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress(this.toIP,this.toPort));
  39.  
  40. socket.send(packet);
  41. if (data.equals("bye")) {
  42. break;
  43. }
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. socket.close();
  49. }
  50. }

接收消息:

  1. package com.kuang.chat;
  2.  
  3. import java.io.IOException;
  4. import java.net.DatagramPacket;
  5. import java.net.DatagramSocket;
  6. import java.net.SocketException;
  7.  
  8. public class TalkReceive implements Runnable {
  9. DatagramSocket socket = null;
  10. private int port;
  11. private String msgFrom;
  12.  
  13. public TalkReceive(int port,String msgFrom) {
  14. this.port = port;
  15. this.msgFrom = msgFrom;
  16. try {
  17. socket = new DatagramSocket(port);
  18. } catch (SocketException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22.  
  23. @Override
  24. public void run() {
  25.  
  26. while (true) {
  27. try {
  28. //准备接收数据
  29. byte[] container = new byte[1024];
  30. DatagramPacket packet = new DatagramPacket(container,0,container.length);
  31. socket.receive(packet);//阻塞式接受包裹
  32.  
  33. //断开连接 bye
  34. byte[] data = packet.getData();
  35. String receiveData = new String(data,0,data.length);
  36.  
  37. System.out.println(msgFrom + ":" +receiveData);
  38. if (receiveData.equals("bye")) {
  39. break;
  40. }
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. socket.close();
  46. }
  47. }

用户1:学生

  1. package com.kuang.chat;
  2.  
  3. public class TalkStudent {
  4. public static void main(String[] args) {
  5. //开启两个线程
  6. new Thread(new TalkSend(7777,"localhost",9999)).start();
  7. new Thread(new TalkReceive(8888,"老师")).start();
  8. }
  9. }

用户2:老师

  1. package com.kuang.chat;
  2.  
  3. public class TalkTeacher {
  4. public static void main(String[] args) {
  5. new Thread(new TalkSend(5555,"localhost",8888)).start();
  6. new Thread(new TalkReceive(9999,"学生")).start();
  7. }
  8. }

运行结果:

1、启动学生端,开始运行

Java网络编程UDP实现多线程在线聊天

2、启动教师端,开始运行

Java网络编程UDP实现多线程在线聊天

3、学生发送消息,教师收到

Java网络编程UDP实现多线程在线聊天

Java网络编程UDP实现多线程在线聊天

4、教师发送消息,学生收到

Java网络编程UDP实现多线程在线聊天

Java网络编程UDP实现多线程在线聊天

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/weixin_44668898/article/details/107319623