当前位置: 移动技术网 > IT编程>开发语言>Java > java聊天室的实现代码

java聊天室的实现代码

2019年07月19日  | 移动技术网IT编程  | 我要评论

物业师,奥美网站,租房合同协议下载

本文实例为大家分享了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;

 /**
  * 学校:山东师范大学 程序员:外力_victor 日期:2016年5月8日 上午9:19:51 功能:启动客户端程序
  * 
  * @param args
  */
 public static void main(string[] args) {
  new clientframe().init();
 }

 /**
  * 学校:山东师范大学 程序员:外力_victor 日期:2016年5月8日 上午9:20:43 功能:对窗口进行初始化
  */
 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();
   }
  }

 }

 /**
  * 功能:当点击回车时出发的事件 学校:山东师范大学 程序员:外力_victor 日期:2016年5月8日 上午9:49:30
  */
 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);
  }
 }

 /**
  * 功能:给服务器发送消息 学校:山东师范大学 程序员:外力_victor 日期:2016年5月8日 上午10:13:48
  * 
  * @param message
  */
 private void sendmessagetoserver(string message) {
  try {
   dos.writeutf(message);
   dos.flush();
  } catch (ioexception e) {
   system.out.println("发送消息失败");
   e.printstacktrace();
  }
 }

 /**
  * 功能:申请socket链接 学校:山东师范大学 程序员:外力_victor 日期:2016年5月8日 上午10:00:38
  */
 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();
  }
 }

 /**
  * 功能:关闭流和链接 学校:山东师范大学 程序员:外力_victor 日期:2016年5月8日 上午10:01:32
  */
 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<client> clients = new arraylist<>();

 /**
  * 功能:启动chatsever 学校:山东师范大学 程序员:外力_victor 日期:2016年5月8日 上午10:26:41
  * 
  * @param args
  */
 public static void main(string[] args) {
  new chatserver().init();
 }

 /**
  * 功能:对chatserver初始化 学校:山东师范大学 程序员:外力_victor 日期:2016年5月8日 上午10:27:09
  */
 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();
    }
   }

  }

  /**
   * 功能:转发给所有客户端 学校:山东师范大学 程序员:外力_victor 日期:2016年5月8日 上午11:11:59
   * 
   * @param message
   * @throws ioexception
   */
  private void forwordtoallclients(string message) throws ioexception {
   for (client c : clients) {
    if (c != this) {
     out = c.socket.getoutputstream();
     dos = new dataoutputstream(out);
     forwordtoclient(message);
    }
   }
  }

  /**
   * 功能:发送给一个客户端 学校:山东师范大学 程序员:外力_victor 日期:2016年5月8日 上午11:16:12
   * 
   * @throws ioexception
   */
  private void forwordtoclient(string message) throws ioexception {
   dos.writeutf(message);
   dos.flush();
   system.out.println("转发成功!");
  }

 }
}

源码下载:

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网