Java实现在线聊天功能 发表于 2020-07-05 | 更新于 2021-12-31 总字数: 487 | 阅读时长: 2分钟 | 阅读量: 0效果关键代码创建Client.java1234567891011121314151617181920212223242526272829303132333435import java.io.IOException;import java.net.Socket;import java.net.UnknownHostException;/** * @author Lete * @乐特的程序永无BUG * @createDate 2020- 07-04 22:13 * * 1. SendThread 发送消息线程 * 2. RecieveThread 接受消息线程 */public class Client { public static void main(String[] args) { try { // 创建8888端口 Socket s = new Socket("127.0.0.1", 8888); // 启动发送消息线程 new SendThread(s).start(); // 启动接受消息线程 new RecieveThread(s).start(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}创建Server.java123456789101112131415161718192021222324252627282930313233343536import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;/** * @author Lete * @乐特的程序永无BUG * @createDate 2020- 07-04 22:13 * * 1. SendThread 发送消息线程 * 2. RecieveThread 接受消息线程 */public class Server { public static void main(String[] args) { try { // 监听8888端口 ServerSocket ss = new ServerSocket(8888); System.out.println("监听在端口号:8888"); Socket s = ss.accept(); //启动发送消息线程 new SendThread(s).start(); //启动接受消息线程 new RecieveThread(s).start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}创建RecieveThread.java123456789101112131415161718192021222324252627282930313233343536373839import java.io.*;import java.net.Socket;/** * @author Lete * @乐特的程序永无BUG * @createDate 2020- 07-04 22:13 * * 1. SendThread 发送消息线程 * 2. RecieveThread 接受消息线程 */public class RecieveThread extends Thread { private Socket s; public RecieveThread(Socket s) { this.s = s; } public void run() { try { // 接收对方输入的内容 InputStream is = s.getInputStream(); DataInputStream dis = new DataInputStream(is); while (true) { String msg = dis.readUTF(); System.out.println(msg); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}创建SendThread.java1234567891011121314151617181920212223242526272829303132333435363738394041import java.io.*;import java.net.Socket;import java.util.Scanner;/** * @author Lete * @乐特的程序永无BUG * @createDate 2020- 07-04 22:13 * * 1. SendThread 发送消息线程 * 2. RecieveThread 接受消息线程 */public class SendThread extends Thread { private Socket s; public SendThread(Socket s) { this.s = s; } public void run() { try { // 获取输入的内容 OutputStream os = s.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); while (true) { Scanner sc = new Scanner(System.in); String str = sc.next(); dos.writeUTF(str); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}文章作者: Lete乐特文章链接: https://blog.imlete.cn/article/javachat.html版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Lete乐特's Blog !LeanCloud-Valine保姆级配置教程前言之前写了一篇LeanCloud+Yilia的文章👉 https://blog.lete114.top/article/addee0b6.html 我觉得还不够详细,而且之前写的没有Qmsg...Hexo-abbrlink博客URL持久化、优化前言为什么要优化URL? 对搜索引擎比较友好 URL缩短 Hexo默认的都是年/月/日/md的标题localhost:4000/2020/06/10/hello-world/ 方式一URL优... 评论