客戶端:
多個(gè)客戶可以正常收發(fā)信息,因?yàn)榭梢酝瑫r(shí)發(fā)送和接受信息,不是發(fā)送完信息后等待
返回信息,所以要加入多線程
堅(jiān)守“ 做人真誠(chéng) · 做事靠譜 · 口碑至上 · 高效敬業(yè) ”的價(jià)值觀,專業(yè)網(wǎng)站建設(shè)服務(wù)10余年為成都成都攪拌罐車小微創(chuàng)業(yè)公司專業(yè)提供成都企業(yè)網(wǎng)站定制營(yíng)銷網(wǎng)站建設(shè)商城網(wǎng)站建設(shè)手機(jī)網(wǎng)站建設(shè)小程序網(wǎng)站建設(shè)網(wǎng)站改版,從內(nèi)容策劃、視覺(jué)設(shè)計(jì)、底層架構(gòu)、網(wǎng)頁(yè)布局、功能開(kāi)發(fā)迭代于一體的高端網(wǎng)站建設(shè)服務(wù)。
public class Client {
public static void main(String[]args) throws UnknownHostException, IOException
{
System.out.println("客戶端啟動(dòng)中...");
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
System.out.println("請(qǐng)輸入用戶名:");
String name=br.readLine();
Socket client =new Socket("localhost",9999);
new Thread(new Send(client,name)).start();//在線程還沒(méi)啟動(dòng)之前,通過(guò)構(gòu)造方法發(fā)送名稱,先于信息的發(fā)送,
//因?yàn)榻邮招畔r(shí),名稱已經(jīng)發(fā)送并被讀取到變量中
new Thread(new Receive(client)).start();
}
}
作為釋放資源工具類
public class utils {
public static void close(Closeable... target )
{
for(Closeable it:target)
{
try {
if(null!=it)
{
it.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客戶端的發(fā)送端封裝了:
發(fā)送消息
從鍵盤(pán)讀取消息
run()
釋放資源
public class Send implements Runnable{
private BufferedReader br;
private DataOutputStream dos;
private Socket client;
private boolean flag=true;
private String name;
public Send(Socket client,String name)
{
this.client=client;
this.name=name;
br=new BufferedReader(new InputStreamReader(System.in));
try {
dos=new DataOutputStream(client.getOutputStream());
//發(fā)送名稱
send(name);
} catch (IOException e) {
this.release();
}
}
public void run()
{
while(flag)
{
String msg=getstr();
send(msg);
}
}
private void release()
{
this.flag=false;
utils.close(dos,client);
}
//從控制臺(tái)獲取消息
private String getstr()
{
try {
String msg=br.readLine();
return msg;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private void send(String msg)
{
try {
dos.writeUTF(msg);
dos.flush();
} catch (IOException e) {
release();
}
}
}
客戶端的接收端封裝了:
接收消息
run()
釋放資源
public class Receive implements Runnable {
private DataInputStream dis;
private Socket client;
private boolean flag=true;
public Receive(Socket client)
{
this.client=client;
try {
dis=new DataInputStream(client.getInputStream());
} catch (IOException e) {
release();
}
}
private String receive()
{
String msg;
try {
msg = dis.readUTF();
return msg;
} catch (IOException e) {
release();
}
return null;
}
public void run()
{
while(flag)
{
String msg=receive();
System.out.println(msg);
}
}
private void release()
{
this.flag=false;
utils.close(dis,client);
}
}
在線聊天室
服務(wù)器:
public class Chat {
private static CopyOnWriteArrayList<channel> list=new CopyOnWriteArrayList<>();//使用并發(fā)容器,適合多線程
public static void main(String[]args) throws IOException
{
System.out.println("服務(wù)器啟動(dòng)中...");
ServerSocket server=new ServerSocket(9999);
while(true)
{
Socket client =server.accept();
System.out.println("一個(gè)客戶端建立了連接");
channel c=new channel(client);
list.add(c); //容器管理所有成員
new Thread(c).start();
}
}
static class channel implements Runnable{
private DataInputStream dis;
private DataOutputStream dos;
private BufferedReader br;
private Socket client;
private boolean flag;
private String name;
public channel(Socket client)
{
this.client=client;
try {
dis=new DataInputStream(client.getInputStream());
dos=new DataOutputStream(client.getOutputStream());
flag=true;
//獲取名稱
this.name=receive();
//歡迎到來(lái)
this.send("歡迎來(lái)到聊天室");
this.sendothers(this.name+"來(lái)到了聊天室",true);
} catch (IOException e) {
release(); }
}
//接收消息
private String receive()
{
try {
String msg=dis.readUTF();
return msg;
} catch (IOException e) {
release();
}
return null;
}
//發(fā)送給消息
private void send(String msg)
{
try {
dos.writeUTF(msg);
dos.flush();
} catch (IOException e) {
release();
}
}
//群聊:獲取自己的消息,發(fā)給其他人和私聊功能
private void sendothers(String msg,boolean issys)
{
boolean issecrete =msg.startsWith("@");
if(issecrete)
{
int index=msg.indexOf(":");
String target=msg.substring(1,index);
msg=msg.substring(index+1);
for(channel it:list)
{
if(it.name.equals(target)) //查找目標(biāo)
{
it.send(this.name+"悄悄地對(duì)你說(shuō):"+msg); //私聊消息
break;
}
}
}else
{
for(channel it:list)
{
if(it==this)
{
continue;
}
if(!issys)
{
it.send(this.name+":"+msg);
}else
{
it.send(msg);
}
}
}
}
private void release()
{
this.flag=false;
utils.close(dis,dos,client); //寫(xiě)一個(gè)工具類,使用Closeable...可變參數(shù)
//退出時(shí),從容器里面移掉自身
list.remove(this);
sendothers("離開(kāi)了聊天室",false);
}
public void run()
{
while(flag)
{
String msg=receive();
sendothers(msg,false);
}
release();
}
}
}
名稱欄目:java網(wǎng)絡(luò)編程-TCP-多人群聊究極版
標(biāo)題鏈接:http://aaarwkj.com/article30/igdpso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)、搜索引擎優(yōu)化、品牌網(wǎng)站設(shè)計(jì)、面包屑導(dǎo)航、手機(jī)網(wǎng)站建設(shè)、定制開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)