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

java实现一个简单TCPSocket聊天室功能分享

程序员文章站 2024-03-11 15:30:13
本文实例为大家分享了java实现tcpsocket聊天室功能的相关代码,供大家参考,具体内容如下 1.tcpserver.java import java....

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

1.tcpserver.java

import java.net.*;
import java.io.*;
import java.util.*;
import java.util.concurrent.*;
public class tcpserver{
  private static final int serverport = 8888;
  private serversocket myserver = null;
  private list<socket> clients = new arraylist<socket>();
  private executorservice mexecutorservice;
  public tcpserver(){
    try{
      myserver = new serversocket(serverport);
      mexecutorservice = executors.newcachedthreadpool(); 
      system.out.println("start:");
      socket mysocket = null;
      while(true){
      mysocket = myserver.accept();
      clients.add(mysocket);
      mexecutorservice.execute(new threadserver(mysocket)); 
      }
    }catch(exception e){
        e.printstacktrace(); 
        system.exit(0);
      }
  }
  class threadserver implements runnable{
    private socket msocket=null;
    private bufferedreader in= null;
    private printwriter out = null;
    private string mstrmsg = null; 
    public threadserver(socket socket) {
      try{      
      this.msocket=socket;
      in = new bufferedreader(new inputstreamreader(msocket.getinputstream(), "gb2312"));
      mstrmsg = "user:" + msocket.getinetaddress() + " come total:" + clients.size(); 
      sendmassage();
      }catch(ioexception e){
        system.out.println("erorr");
        system.exit(0);
      }
    }
    private void sendmassage(){
      try{
        system.out.println(mstrmsg);
        for(socket mysocket:clients)
        {
        out = new printwriter(new outputstreamwriter(mysocket.getoutputstream(),"gb2312"),true);
        out.println(mstrmsg);
        }
      }catch(ioexception e){
        system.out.println("erorr");
        system.exit(0);
      }
    }
    public void run(){
      try{
        while((mstrmsg = in.readline())!=null){
          if(mstrmsg.trim().equals("exit")){
            clients.remove(msocket);
            in.close();
            out.close();
            mstrmsg = "user:" + msocket.getinetaddress() + " exit tatal:" + clients.size();            ;
            msocket.close();
            sendmassage();           
            break; 
          } 
          else{
            mstrmsg = msocket.getinetaddress()+":" + mstrmsg; 
            sendmassage();
          }
         
        }
      }catch(ioexception e){
        system.out.println("erorr");
        system.exit(0);
      }
       
    }
  }
  public static void main(string[] args){
    new tcpserver();
  }
}

2.tcpclient.java

import java.net.*;
import java.io.*;
import java.util.concurrent.*;
public class tcpclient {
  private static final int port = 8888;
  private socket client = null;
  private bufferedreader sin = null;
  private executorservice mexecutorservice;
  public tcpclient(){
    try{
      client = new socket("120.27.126.174",port);
    sin = new bufferedreader(new inputstreamreader(client.getinputstream(),"gb2312"));
    mexecutorservice = executors.newcachedthreadpool();
    mexecutorservice.execute(new threadclient(client)); 
    string msg = null;
   while((msg = sin.readline()) != null) { 
        system.out.println(msg); 
      } 
    }catch(ioexception e){
           system.out.println(e.getmessage()); 
        }
     
  }
  class threadclient extends thread{
    private socket msocket = null;
    private string msg = null;
    bufferedreader in = null;
    printwriter out = null;
      public threadclient(socket socket){
        this.msocket = socket;
      }
      public void run(){
        try{
          in = new bufferedreader(new inputstreamreader(system.in));
          out = new printwriter(new outputstreamwriter(msocket.getoutputstream(), "gb2312"), true);
          while(true){
          msg = in.readline();
          out.println(msg);
            if(msg.trim().equals("exit")){
              in.close();
              out.close();
               mexecutorservice.shutdownnow();        
            break;
            }
          }
        }catch(ioexception e){
          system.out.println("see you");
          system.exit(0);
        }
      }
  }
  public static void main(string[] args){
    new tcpclient();
  }
}

以上就是java实现tcpsocket聊天室功能的代码,希望对大家的学习有所帮助。