Java使用TCP实现群聊 聊天室(多线程和tcp的使用)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Java使用TCP实现群聊 聊天室(多线程和tcp的使用)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                一:引言:
顯示結果在控制臺顯示,未能實現圖形界面的結合
二:上碼
1.服務端
package com.wyj.talkhome; /** * 實現一個用戶可以接發多條消息 * * */ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.CopyOnWriteArrayList;public class Demo4_服務端群聊版 {//在遍歷一個list的時候,還向list中添加元素private static CopyOnWriteArrayList<pipe> all =new CopyOnWriteArrayList<pipe>();public static void main(String[] args) throws IOException {System.out.println("---server---");//1.使用ServerSocket創建一個服務端 并指定一個端口號ServerSocket server = new ServerSocket(7777); //阻塞式等待鏈接boolean aa = true;while( aa ) {Socket client = server.accept();System.out.println("一個客戶端鏈接成功");pipe p = new pipe(client);all.add(p);new Thread(p).start();}}static class pipe implements Runnable{private Socket client;private DataInputStream dis;private DataOutputStream dos;private boolean runing = true;private String name;public pipe( Socket client ) {this.client = client;try {dis = new DataInputStream(client.getInputStream());dos = new DataOutputStream(client.getOutputStream());this.name = receive();this.send("歡迎你的到來");sendothers(this.name+"來到了聊天室",true);//系統消息} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();release();}}//收到private String receive() {String msg = "";try {if (msg != null )msg = dis.readUTF();} catch (IOException e) {// TODO Auto-generated catch block// e.printStackTrace();release();}return msg;}//發送private void send ( String msg ) {try {dos.writeUTF(msg);dos.flush();} catch (IOException e) {// TODO Auto-generated catch block//e.printStackTrace();this.runing = false;release();}}//發送信息給其他人private void sendothers(String msg,boolean isSys) {for( pipe others:all) {if( others == this) {continue;}if(!isSys)//群消息others.send(this.name+"對所有人說:"+msg);else//系統消息others.send(msg);}}//釋放資源private void release() {try {if( dos != null )dos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if( dis != null )dis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if( client != null )client.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Overridepublic void run() {// TODO Auto-generated method stub//Io流 //輸入流 接受客戶端的請求while( runing ) {//收到請求String msg = receive();//反饋結果if( !msg.equals(""))sendothers(msg,false);elseruning = false;}//釋放資源release();}} }2.客戶端
package com.wyj.talkhome; /*** 實現多用戶可以接發多條消息* */import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; import java.net.UnknownHostException;public class Demo4_客戶端群聊版 {public static void main(String[] args) throws UnknownHostException, IOException {// TODO Auto-generated method stubSystem.out.println("---client---");BufferedReader brr = new BufferedReader( new InputStreamReader(System.in));System.out.print("請輸入你在群聊中的名稱:");String name = brr.readLine();//使用Socket創建客戶端并指定IP和端口號Socket client = new Socket("localhost",7777);new Thread(new Demo4_客戶端send(client,name)).start();new Thread(new Demo4_客戶端receive(client)).start();}}3:客戶端中實現的發送信息給服務端
package com.wyj.talkhome;import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket;public class Demo4_客戶端send implements Runnable{private Socket client;private DataOutputStream dos; private DataInputStream dis;private BufferedReader br;private boolean runing = true;private String name;public Demo4_客戶端send( Socket client ,String name) {this.client = client;this.name = name;br = new BufferedReader(new InputStreamReader(System.in));try {dos = new DataOutputStream(client.getOutputStream());send(this.name);//建立好通道 直接發送名字} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();this.release();}}//釋放資源private void release() {try {if( dos != null )dos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if( dis != null )dis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if( client != null )client.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}//從控制臺獲取信息private String fromconsole() {try {return br.readLine();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();this.release();}return "";} //向服務端發送信息private void send( String msg) {try {dos.writeUTF(msg);dos.flush();} catch (IOException e) {// TODO Auto-generated catch block//e.printStackTrace();this.runing = false;this.release();}}public void run() {// TODO Auto-generated method stubwhile(runing) {String msg = fromconsole();if( !msg.equals("")) {send(msg);}} }}4:客戶端接受服務端反饋的消息
package com.wyj.talkhome;import java.io.DataInputStream; import java.io.IOException; import java.net.Socket;public class Demo4_客戶端receive implements Runnable{private DataInputStream dis; private Socket client;boolean runing = true;public Demo4_客戶端receive(Socket client) {try {dis = new DataInputStream(client.getInputStream());} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();this.release();}}private String receive() {String msg = "";try {if (msg != null )msg = dis.readUTF();} catch (IOException e) {// TODO Auto-generated catch block// e.printStackTrace();release();}return msg;}//釋放資源private void release() {try {if( dis != null )dis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if( client != null )client.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Overridepublic void run() {// TODO Auto-generated method stubwhile( runing ) {//收到反饋結果String msg1 = receive();if( !msg1.equals(""));System.out.println(msg1);}}}三:成果展示
總結
以上是生活随笔為你收集整理的Java使用TCP实现群聊 聊天室(多线程和tcp的使用)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 只能打字的智能打字机只能打字的智能打字机
- 下一篇: 通过一下几个方法阻止比特币勒索病毒的入侵
