网络编程_手写聊天室_群聊过渡板
生活随笔
收集整理的這篇文章主要介紹了
网络编程_手写聊天室_群聊过渡板
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
實(shí)現(xiàn)功能
多好友間正常聊天,登錄退出提示**
如圖
此處涉及到之前的容器
點(diǎn)擊跳轉(zhuǎn)
只寫了一部分以后再更容器
關(guān)閉流工具類
package 網(wǎng)絡(luò)編程_手寫聊天室_群聊過渡板;public class MultiClient {public static void main(String[] args) throws UnknownHostException, IOException {BufferedReader str=new BufferedReader(new InputStreamReader(System.in));//獲取用戶名System.out.println("請(qǐng)輸入用戶名: ");String name=str.readLine();Socket client = new Socket("localhost",8888);new Thread(new Sent(client,name)).start();new Thread(new Receive(client)).start();} }客戶端接收類
package 網(wǎng)絡(luò)編程_手寫聊天室_群聊過渡板;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("接收時(shí)出錯(cuò)!");release();}}@Overridepublic void run() {isRunning =true;while(isRunning) {String str="";try {str = in.readUTF();} catch (IOException e) {System.out.println("讀取時(shí)出錯(cuò)");}if(str!=null) {System.out.println(str);}}this.release();}public void release() {CloseUtlis utlis =new CloseUtlis();utlis.close(in);isRunning=false;}}客戶端發(fā)送類
package 網(wǎng)絡(luò)編程_手寫聊天室_群聊過渡板;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);//將名稱發(fā)送到服務(wù)器} catch (IOException e) {System.out.println("發(fā)送數(shù)據(jù)時(shí)出錯(cuò)");}}@Overridepublic void run() {isRunning =true;while(isRunning) {String str="";try {str = console.readLine();} catch (IOException e) {System.out.println("讀取時(shí)出錯(cuò)");release();}if(str!=null) {sent(str);} }release();}public void sent(String mess) {try {out.writeUTF(mess);out.flush();} catch (IOException e) {System.out.println("發(fā)送時(shí)失敗!");release();}}public void release() {CloseUtlis utlis =new CloseUtlis();utlis.close(console,out);isRunning=false;}}客戶端
package 網(wǎng)絡(luò)編程_手寫聊天室_群聊過渡板;public class MultiClient {public static void main(String[] args) throws UnknownHostException, IOException {BufferedReader str=new BufferedReader(new InputStreamReader(System.in));//獲取用戶名System.out.println("請(qǐng)輸入用戶名: ");String name=str.readLine();Socket client = new Socket("localhost",8888);new Thread(new Sent(client,name)).start();new Thread(new Receive(client)).start();} }服務(wù)器
package 網(wǎng)絡(luò)編程_手寫聊天室_群聊過渡板;public class MultiServer {private static CopyOnWriteArrayList<channel> all =new CopyOnWriteArrayList<MultiServer.channel>();//channel類型的容器public static void main(String[] args) throws IOException {System.out.println("服務(wù)器開始工作");ServerSocket server=new ServerSocket(8888);boolean isRunning =true;while(isRunning) {Socket client = server.accept();channel c=new channel(client);all.add(c);//管理每一個(gè)成員new Thread(c).start();}}static class channel implements Runnable{//與服務(wù)器連接的客戶端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("建立了一個(gè)連接");while(true) { String mes=receive();//輸入 sentothers(mes,false);//向所有用戶輸出}} catch (IOException e) {System.out.println("多線程中出錯(cuò)");} }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+"對(duì)全體成員說: "+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("釋放時(shí)出錯(cuò)");}}} }效果
總結(jié)
以上是生活随笔為你收集整理的网络编程_手写聊天室_群聊过渡板的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java随机数
- 下一篇: 随机数算法 java_最全的java随机