欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java利用Socket实现聊天室功能实例

程序员文章站 2024-03-31 19:28:28
最近研究了下java socket通信基础,利用代码实现了一个简单的多人聊天室功能,现把代码共享下,希望能帮到有兴趣了解的人。 目录结构: chatclient...

最近研究了下java socket通信基础,利用代码实现了一个简单的多人聊天室功能,现把代码共享下,希望能帮到有兴趣了解的人。

目录结构:

java利用Socket实现聊天室功能实例

chatclient:

package com.panda.chat; 
 
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 
import java.net.*; 
 
@suppresswarnings("serial") 
public class chatclient extends frame { 
 private textarea ta = new textarea(); 
 private textfield tf = new textfield(); 
 private dataoutputstream dos = null; 
 private datainputstream dis = null; 
 private socket socket = null; 
 private boolean bconnected = false; 
 private thread thread=null; 
 public static void main(string[] args) { 
  new chatclient().frameclient(); 
 } 
 public void frameclient(){ 
  setsize(400, 400); 
  setlocation(400,300); 
  add(ta,borderlayout.north); 
  add(tf,borderlayout.south); 
  pack(); 
  tf.addactionlistener(new tflistener()); 
  //关闭窗口事件监听 
  this.addwindowlistener(new windowadapter() { 
   @override 
   public void windowclosing(windowevent e) { 
    disconnected(); 
    system.exit(0); 
   } 
  }); 
  this.connect(); 
  setvisible(true); 
 } 
 //链接服务器地址 
 private void connect(){ 
  try { 
   socket = new socket("127.0.0.1", 8888); 
   thread=new thread(new chatthread()); 
   thread.start(); 
   dos = new dataoutputstream(socket.getoutputstream()); 
   dis = new datainputstream(socket.getinputstream()); 
  } catch (unknownhostexception e) { 
   // todo auto-generated catch block 
   e.printstacktrace(); 
  } catch (ioexception e) { 
   // todo auto-generated catch block 
   e.printstacktrace(); 
  } 
 } 
 //断开连接 
 private void disconnected(){ 
  bconnected = false; 
  try { 
   dos.close(); 
   dis.close(); 
   socket.close(); 
  } catch (ioexception e1) { 
   // todo auto-generated catch block 
   e1.printstacktrace(); 
  } 
 } 
 //键盘回车事件 
 private class tflistener implements actionlistener{ 
 
  @override 
  public void actionperformed(actionevent e) { 
   string strmsg = tf.gettext(); 
   tf.settext(""); 
   try { 
    dos.writeutf(strmsg); 
    dos.flush(); 
   } catch (ioexception e1) { 
    // todo auto-generated catch block 
    e1.printstacktrace(); 
   } 
  } 
   
 } 
 //开启线程接受服务器信息 
 private class chatthread implements runnable{ 
  @override 
  public void run() { 
   try { 
    bconnected = true; 
    while(bconnected){ 
     string msg = dis.readutf(); 
     string tatext = ta.gettext(); 
     ta.settext(tatext+msg+"\n"); 
    } 
   } catch (socketexception e) { 
    system.out.println("退出");; 
   } catch (ioexception e) { 
    e.printstacktrace(); 
   } 
  } 
 } 
} 

chatserver:

package com.panda.chat; 
 
import java.io.*; 
import java.net.*; 
import java.util.*; 
 
public class chatserver { 
 private boolean started = false; 
 private list<chatthread> chatthreads = new arraylist<chatthread>(); 
 public static void main(string[] args) { 
  new chatserver().startserver(); 
 } 
 private void startserver(){ 
  try { 
   //开启服务端socket 
   serversocket seso = new serversocket(8888); 
   started = true; 
   while(started){ 
    //接受客户端连接请求 
    socket sos = seso.accept(); 
    system.out.println("一个客户端已连接"); 
    //开启线程处理客户端通信 
    chatthread ct = new chatthread(sos); 
    chatthreads.add(ct); 
    new thread(ct).start(); 
   } 
  } catch (ioexception e) { 
   e.printstacktrace(); 
  } 
 } 
 private class chatthread implements runnable{ 
  private socket socket; 
  private datainputstream din=null; 
  private dataoutputstream don=null; 
  private boolean bconnected = false; 
  public chatthread(socket socket) { 
   super(); 
   this.socket = socket; 
  } 
  //发送信息的函数 
  private void send(string strmsgin){ 
   try{ 
    don.writeutf(strmsgin); 
    don.flush(); 
   }catch(ioexception e){ 
    e.printstacktrace(); 
   } 
  } 
  @override 
  public void run() { 
   try{ 
    din = new datainputstream(socket.getinputstream()); 
    don = new dataoutputstream(socket.getoutputstream()); 
    //读取数据 
    bconnected = true; 
    while(bconnected){ 
     string strmsgin = din.readutf(); 
     system.out.println(strmsgin); 
     //接收到数据后发送给每个客户端 
     for(int i =0;i<chatthreads.size();i++){ 
      chatthreads.get(i).send(strmsgin); 
     } 
    } 
   }catch (ioexception e) { 
    try { 
     //如果客户端出错或关闭,直接关闭连接,并移除list中的当前线程 
     socket.close(); 
     chatthreads.remove(this); 
    } catch (ioexception e1) { 
     // todo auto-generated catch block 
     e1.printstacktrace(); 
    } 
   } finally{ 
    try { 
     din.close(); 
     don.close(); 
     socket.close(); 
    } catch (ioexception e) { 
     // todo auto-generated catch block 
     e.printstacktrace(); 
    } 
   } 
  } 
  
 } 
} 

运行chatsever后,再同时打开多次chatclient,就可以实现多人聊天了,你也试试。

java利用Socket实现聊天室功能实例

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