java实现一个简单TCPSocket聊天室功能分享
本文实例为大家分享了java实现TCPSocket聊天室功能的相关代码,供大家参考,具体内容如下
1.TCPserver.java
importjava.net.*; importjava.io.*; importjava.util.*; importjava.util.concurrent.*; publicclassTCPserver{ privatestaticfinalintSERVERPORT=8888; privateServerSocketMyServer=null; privateList<Socket>Clients=newArrayList<Socket>(); privateExecutorServicemExecutorService; publicTCPserver(){ try{ MyServer=newServerSocket(SERVERPORT); mExecutorService=Executors.newCachedThreadPool(); System.out.println("start:"); SocketMySocket=null; while(true){ MySocket=MyServer.accept(); Clients.add(MySocket); mExecutorService.execute(newThreadServer(MySocket)); } }catch(Exceptione){ e.printStackTrace(); System.exit(0); } } classThreadServerimplementsRunnable{ privateSocketmsocket=null; privateBufferedReaderin=null; privatePrintWriterout=null; privateStringmStrMSG=null; publicThreadServer(Socketsocket){ try{ this.msocket=socket; in=newBufferedReader(newInputStreamReader(msocket.getInputStream(),"GB2312")); mStrMSG="user:"+msocket.getInetAddress()+"cometotal:"+Clients.size(); SendMassage(); }catch(IOExceptione){ System.out.println("erorr"); System.exit(0); } } privatevoidSendMassage(){ try{ System.out.println(mStrMSG); for(SocketMySocket:Clients) { out=newPrintWriter(newOutputStreamWriter(MySocket.getOutputStream(),"GB2312"),true); out.println(mStrMSG); } }catch(IOExceptione){ System.out.println("erorr"); System.exit(0); } } publicvoidrun(){ try{ while((mStrMSG=in.readLine())!=null){ if(mStrMSG.trim().equals("exit")){ Clients.remove(msocket); in.close(); out.close(); mStrMSG="user:"+msocket.getInetAddress()+"exittatal:"+Clients.size();; msocket.close(); SendMassage(); break; } else{ mStrMSG=msocket.getInetAddress()+":"+mStrMSG; SendMassage(); } } }catch(IOExceptione){ System.out.println("erorr"); System.exit(0); } } } publicstaticvoidmain(String[]args){ newTCPserver(); } }
2.TCPclient.java
importjava.net.*; importjava.io.*; importjava.util.concurrent.*; publicclassTCPclient{ privatestaticfinalintPORT=8888; privateSocketClient=null; privateBufferedReadersin=null; privateExecutorServicemExecutorService; publicTCPclient(){ try{ Client=newSocket("120.27.126.174",PORT); sin=newBufferedReader(newInputStreamReader(Client.getInputStream(),"GB2312")); mExecutorService=Executors.newCachedThreadPool(); mExecutorService.execute(newThreadClient(Client)); Stringmsg=null; while((msg=sin.readLine())!=null){ System.out.println(msg); } }catch(IOExceptione){ System.out.println(e.getMessage()); } } classThreadClientextendsThread{ privateSocketmSocket=null; privateStringmsg=null; BufferedReaderin=null; PrintWriterout=null; publicThreadClient(Socketsocket){ this.mSocket=socket; } publicvoidrun(){ try{ in=newBufferedReader(newInputStreamReader(System.in)); out=newPrintWriter(newOutputStreamWriter(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(IOExceptione){ System.out.println("seeyou"); System.exit(0); } } } publicstaticvoidmain(String[]args){ newTCPclient(); } }
以上就是java实现TCPSocket聊天室功能的代码,希望对大家的学习有所帮助。