栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java聊天室的实现代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java聊天室的实现代码

本文实例为大家分享了java实现聊天室的具体代码,供大家参考,具体内容如下

聊天室界面:

源码:

public class Clientframe extends frame {

 private TextField textFieldContent = new TextField();
 private textarea textareaContent = new textarea();
 private Socket socket = null;
 private OutputStream out = null;
 private DataOutputStream dos = null;
 private InputStream in = null;
 private DataInputStream dis = null;
 private boolean flag = false;

 
 public static void main(String[] args) {
 new Clientframe().init();
 }

 
 private void init() {
 this.setSize(300, 300);
 setLocation(250, 150);
 setVisible(true);
 setTitle("WeChatRoom");

 // 添加控件
 this.add(textareaContent);
 this.add(textFieldContent, BorderLayout.SOUTH);
 textareaContent.setFocusable(false);
 pack();

 // 关闭事件
 addWindowListener(new WindowAdapter() {
  public void windowClosing(WindowEvent e) {
  System.out.println("用户试图关闭窗口");
  disconnect();
  System.exit(0);
  }

 });
 // textFieldContent添加回车事件
 textFieldContent.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
  onClickEnter();
  }
 });

 // 建立连接
 connect();
 new Thread(new ReciveMessage()).start();
 }

 private class ReciveMessage implements Runnable {
 @Override
 public void run() {
  flag = true;
  try {
  while (flag) {
   String message = dis.readUTF();
   textareaContent.append(message + "n");
  }
  } catch (EOFException e) {
  flag = false;
  System.out.println("客户端已关闭");
  // e.printStackTrace();
  } catch (SocketException e) {
  flag = false;
  System.out.println("客户端已关闭");
  // e.printStackTrace();
  } catch (IOException e) {
  flag = false;
  System.out.println("接受消息失败");
  e.printStackTrace();
  }
 }

 }

 
 private void onClickEnter() {
 String message = textFieldContent.getText().trim();
 if (message != null && !message.equals("")) {
  String time = new SimpleDateFormat("h:m:s").format(new Date());
  textareaContent.append(time + "n" + message + "n");
  textFieldContent.setText("");
  sendMessageToServer(message);
 }
 }

 
 private void sendMessageToServer(String message) {
 try {
  dos.writeUTF(message);
  dos.flush();
 } catch (IOException e) {
  System.out.println("发送消息失败");
  e.printStackTrace();
 }
 }

 
 private void connect() {
 try {
  socket = new Socket("localhost", 8888);
  out = socket.getOutputStream();
  dos = new DataOutputStream(out);
  in = socket.getInputStream();
  dis = new DataInputStream(in);
 } catch (UnknownHostException e) {
  System.out.println("申请链接失败");
  e.printStackTrace();
 } catch (IOException e) {
  System.out.println("申请链接失败");
  e.printStackTrace();
 }
 }

 
 private void disconnect() {
 flag = false;
 if (dos != null) {
  try {
  dos.close();
  } catch (IOException e) {
  System.out.println("dos关闭失败");
  e.printStackTrace();
  }
 }
 if (out != null) {
  try {
  out.close();
  } catch (IOException e) {
  System.out.println("dos关闭失败");
  e.printStackTrace();
  }
 }
 if (socket != null) {
  try {
  socket.close();
  } catch (IOException e) {
  System.out.println("socket关闭失败");
  e.printStackTrace();
  }
  ;
 }
 }

}
package com.chat;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;

public class ChatServer {

 private List clients = new ArrayList<>();

 
 public static void main(String[] args) {
 new ChatServer().init();
 }

 
 private void init() {
 System.out.println("服务器已开启");
 // BindException

 ServerSocket ss = null;
 Socket socket = null;
 try {
  ss = new ServerSocket(8888);
 } catch (BindException e) {
  System.out.println("端口已被占用");
  e.printStackTrace();
 } catch (IOException e1) {
  e1.printStackTrace();
 }
 try {
  Client client = null;
  while (true) {
  socket = ss.accept();
  System.out.println("客户驾到");
  client = new Client(socket);
  clients.add(client);
  new Thread(client).start();
  }
 } catch (IOException e) {
  e.printStackTrace();
 }
 }

 private class Client implements Runnable {
 private Socket socket = null;
 InputStream in = null;
 DataInputStream din = null;
 OutputStream out = null;
 DataOutputStream dos = null;
 boolean flag = true;

 public Client(Socket socket) {
  this.socket = socket;
  try {
  in = socket.getInputStream();
  din = new DataInputStream(in);
  } catch (IOException e) {
  System.out.println("接受消息失败");
  e.printStackTrace();
  }

 }

 public void run() {

  String message;
  try {
  while (flag) {
   message = din.readUTF();
   // System.out.println("客户说:" + message);
   forwordToAllClients(message);
  }
  } catch (SocketException e) {
  flag = false;
  System.out.println("客户下线");
  clients.remove(this);
  // e.printStackTrace();
  } catch (EOFException e) {
  flag = false;
  System.out.println("客户下线");
  clients.remove(this);
  // e.printStackTrace();
  } catch (IOException e) {
  flag = false;
  System.out.println("接受消息失败");
  clients.remove(this);
  e.printStackTrace();
  }

  if (din != null) {
  try {
   din.close();
  } catch (IOException e) {
   System.out.println("din关闭失败");
   e.printStackTrace();
  }
  }
  if (in != null) {
  try {
   in.close();
  } catch (IOException e) {
   System.out.println("din关闭失败");
   e.printStackTrace();
  }
  }
  if (socket != null) {
  try {
   socket.close();
  } catch (IOException e) {
   System.out.println("din关闭失败");
   e.printStackTrace();
  }
  }

 }

 
 private void forwordToAllClients(String message) throws IOException {
  for (Client c : clients) {
  if (c != this) {
   out = c.socket.getOutputStream();
   dos = new DataOutputStream(out);
   forwordToClient(message);
  }
  }
 }

 
 private void forwordToClient(String message) throws IOException {
  dos.writeUTF(message);
  dos.flush();
  System.out.println("转发成功!");
 }

 }
}

源码下载:java聊天室代码

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/140377.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号