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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

网络编程_手写聊天室_群聊过渡板

發布時間:2024/8/1 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 网络编程_手写聊天室_群聊过渡板 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實現功能

多好友間正常聊天,登錄退出提示**
如圖

此處涉及到之前的容器
點擊跳轉
只寫了一部分以后再更容器

關閉流工具類

package 網絡編程_手寫聊天室_群聊過渡板;public class MultiClient {public static void main(String[] args) throws UnknownHostException, IOException {BufferedReader str=new BufferedReader(new InputStreamReader(System.in));//獲取用戶名System.out.println("請輸入用戶名: ");String name=str.readLine();Socket client = new Socket("localhost",8888);new Thread(new Sent(client,name)).start();new Thread(new Receive(client)).start();} }

客戶端接收類

package 網絡編程_手寫聊天室_群聊過渡板;public class Receive implements Runnable{private Socket client;private boolean isRunning;private DataInputStream in;public Receive(Socket client) {this.client=client; try {in=new DataInputStream(client.getInputStream());} catch (IOException e) {System.out.println("接收時出錯!");release();}}@Overridepublic void run() {isRunning =true;while(isRunning) {String str="";try {str = in.readUTF();} catch (IOException e) {System.out.println("讀取時出錯");}if(str!=null) {System.out.println(str);}}this.release();}public void release() {CloseUtlis utlis =new CloseUtlis();utlis.close(in);isRunning=false;}}

客戶端發送類

package 網絡編程_手寫聊天室_群聊過渡板;public class Sent implements Runnable{private Socket client;private BufferedReader console;private DataOutputStream out;private boolean isRunning;public Sent(Socket client,String name) {this.client=client;console=new BufferedReader(new InputStreamReader(System.in));try {out =new DataOutputStream(client.getOutputStream());sent(name);//將名稱發送到服務器} catch (IOException e) {System.out.println("發送數據時出錯");}}@Overridepublic void run() {isRunning =true;while(isRunning) {String str="";try {str = console.readLine();} catch (IOException e) {System.out.println("讀取時出錯");release();}if(str!=null) {sent(str);} }release();}public void sent(String mess) {try {out.writeUTF(mess);out.flush();} catch (IOException e) {System.out.println("發送時失敗!");release();}}public void release() {CloseUtlis utlis =new CloseUtlis();utlis.close(console,out);isRunning=false;}}

客戶端

package 網絡編程_手寫聊天室_群聊過渡板;public class MultiClient {public static void main(String[] args) throws UnknownHostException, IOException {BufferedReader str=new BufferedReader(new InputStreamReader(System.in));//獲取用戶名System.out.println("請輸入用戶名: ");String name=str.readLine();Socket client = new Socket("localhost",8888);new Thread(new Sent(client,name)).start();new Thread(new Receive(client)).start();} }

服務器

package 網絡編程_手寫聊天室_群聊過渡板;public class MultiServer {private static CopyOnWriteArrayList<channel> all =new CopyOnWriteArrayList<MultiServer.channel>();//channel類型的容器public static void main(String[] args) throws IOException {System.out.println("服務器開始工作");ServerSocket server=new ServerSocket(8888);boolean isRunning =true;while(isRunning) {Socket client = server.accept();channel c=new channel(client);all.add(c);//管理每一個成員new Thread(c).start();}}static class channel implements Runnable{//與服務器連接的客戶端private String name;private Socket client;private DataInputStream in;private DataOutputStream out;private int port;private boolean iSsys;//是否為官方消息private boolean isRunning;channel(Socket client) throws IOException{this.client=client;out =new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));in =new DataInputStream(client.getInputStream());String name=receive();this.name=name;sent("歡迎來到聊天室");sentothers(name+"加入了聊天室", true);}@Overridepublic void run() {try {System.out.println("建立了一個連接");while(true) { String mes=receive();//輸入 sentothers(mes,false);//向所有用戶輸出}} catch (IOException e) {System.out.println("多線程中出錯");} }public String receive() throws IOException {String mes="";mes=in.readUTF();return mes;}public void sentothers(String mes,boolean iSsys) throws IOException {for(channel other:all) {if(other==this) {continue;}if(!iSsys) {other.sent(this.name+"對全體成員說: "+mes);}else {other.sent(mes);}}}public void sent(String mes) throws IOException {out.writeUTF(mes);out.flush();}public void release() {//釋放資源并退出聊天室isRunning=false;CloseUtlis utlis =new CloseUtlis();utlis.close(in,client);all.remove(this);try {sentothers(this.name+" 離開了群聊",true );} catch (IOException e) {System.out.println("釋放時出錯");}}} }

效果

總結

以上是生活随笔為你收集整理的网络编程_手写聊天室_群聊过渡板的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。