日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

Java使用TCP实现群聊 聊天室(多线程和tcp的使用)

發(fā)布時(shí)間:2023/12/4 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java使用TCP实现群聊 聊天室(多线程和tcp的使用) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一:引言:

顯示結(jié)果在控制臺(tái)顯示,未能實(shí)現(xiàn)圖形界面的結(jié)合

二:上碼

1.服務(wù)端

package com.wyj.talkhome; /** * 實(shí)現(xiàn)一個(gè)用戶(hù)可以接發(fā)多條消息 * * */ 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_服務(wù)端群聊版 {//在遍歷一個(gè)list的時(shí)候,還向list中添加元素private static CopyOnWriteArrayList<pipe> all =new CopyOnWriteArrayList<pipe>();public static void main(String[] args) throws IOException {System.out.println("---server---");//1.使用ServerSocket創(chuàng)建一個(gè)服務(wù)端 并指定一個(gè)端口號(hào)ServerSocket server = new ServerSocket(7777); //阻塞式等待鏈接boolean aa = true;while( aa ) {Socket client = server.accept();System.out.println("一個(gè)客戶(hù)端鏈接成功");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("歡迎你的到來(lái)");sendothers(this.name+"來(lái)到了聊天室",true);//系統(tǒng)消息} 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;}//發(fā)送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();}}//發(fā)送信息給其他人private void sendothers(String msg,boolean isSys) {for( pipe others:all) {if( others == this) {continue;}if(!isSys)//群消息others.send(this.name+"對(duì)所有人說(shuō):"+msg);else//系統(tǒng)消息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流 //輸入流 接受客戶(hù)端的請(qǐng)求while( runing ) {//收到請(qǐng)求String msg = receive();//反饋結(jié)果if( !msg.equals(""))sendothers(msg,false);elseruning = false;}//釋放資源release();}} }

2.客戶(hù)端

package com.wyj.talkhome; /*** 實(shí)現(xiàn)多用戶(hù)可以接發(fā)多條消息* */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_客戶(hù)端群聊版 {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("請(qǐng)輸入你在群聊中的名稱(chēng):");String name = brr.readLine();//使用Socket創(chuàng)建客戶(hù)端并指定IP和端口號(hào)Socket client = new Socket("localhost",7777);new Thread(new Demo4_客戶(hù)端send(client,name)).start();new Thread(new Demo4_客戶(hù)端receive(client)).start();}}

3:客戶(hù)端中實(shí)現(xiàn)的發(fā)送信息給服務(wù)端

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_客戶(hù)端send implements Runnable{private Socket client;private DataOutputStream dos; private DataInputStream dis;private BufferedReader br;private boolean runing = true;private String name;public Demo4_客戶(hù)端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);//建立好通道 直接發(fā)送名字} 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();}}//從控制臺(tái)獲取信息private String fromconsole() {try {return br.readLine();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();this.release();}return "";} //向服務(wù)端發(fā)送信息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:客戶(hù)端接受服務(wù)端反饋的消息

package com.wyj.talkhome;import java.io.DataInputStream; import java.io.IOException; import java.net.Socket;public class Demo4_客戶(hù)端receive implements Runnable{private DataInputStream dis; private Socket client;boolean runing = true;public Demo4_客戶(hù)端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 ) {//收到反饋結(jié)果String msg1 = receive();if( !msg1.equals(""));System.out.println(msg1);}}}

三:成果展示

總結(jié)

以上是生活随笔為你收集整理的Java使用TCP实现群聊 聊天室(多线程和tcp的使用)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。