Java用TCP手写聊天室 可以 私聊版加群聊版
生活随笔
收集整理的這篇文章主要介紹了
Java用TCP手写聊天室 可以 私聊版加群聊版
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一:引言
想要私聊必須有規(guī)定的格式:@名字:要說(shuō)的話
二:上碼
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 Demo5_服務(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ā)送信息給其他人//私聊:約定數(shù)據(jù)格式:@xxx:msg;private void sendothers(String msg,boolean isSys) {boolean isPrivate = msg.startsWith("@");if(isPrivate) {int idx = msg.indexOf(":");String tagetName = msg.substring(1,idx);msg = msg.substring(idx+1);for( pipe others :all) {if( others.name.equals(tagetName)) {others.send(this.name+"悄悄對(duì)你說(shuō):"+msg);}}}else {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 Demo5_客戶(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é)果:
總結(jié)
以上是生活随笔為你收集整理的Java用TCP手写聊天室 可以 私聊版加群聊版的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 通过一下几个方法阻止比特币勒索病毒的入侵
- 下一篇: 平板只有iPad和其他为什么平板只有ip