java實(shí)現(xiàn)一個(gè)簡(jiǎn)單TCPSocket聊天室功能分享
本文實(shí)例為大家分享了java實(shí)現(xiàn)TCPSocket聊天室功能的相關(guān)代碼,供大家參考,具體內(nèi)容如下
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實(shí)現(xiàn)TCPSocket聊天室功能的代碼,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
Java基礎(chǔ)將Bean屬性值放入Map中的實(shí)例
這篇文章主要介紹了Java基礎(chǔ)將Bean屬性值放入Map中的實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-07-07詳解SpringBoot構(gòu)建Docker鏡像的3種方式
這篇文章主要介紹了SpringBoot構(gòu)建Docker鏡像的3種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06解決Springboot-application.properties中文亂碼問題
這篇文章主要介紹了解決Springboot-application.properties中文亂碼問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Java實(shí)現(xiàn)Dijkstra輸出最短路徑的實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)Dijkstra輸出最短路徑的實(shí)例的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能(上)
這篇文章主要為大家詳細(xì)介紹了springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能,比較基礎(chǔ)的學(xué)習(xí)教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Java創(chuàng)建多線程局域網(wǎng)聊天室實(shí)例
這篇文章主要介紹了Java創(chuàng)建多線程局域網(wǎng)聊天室實(shí)例,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07springboot實(shí)現(xiàn)rabbitmq的隊(duì)列初始化和綁定
這篇文章主要介紹了springboot實(shí)現(xiàn)rabbitmq的隊(duì)列初始化和綁定,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10