亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java編寫實現(xiàn)多人聊天室

 更新時間:2022年09月15日 16:46:59   作者:java_zhangwei  
這篇文章主要為大家詳細(xì)介紹了Java編寫實現(xiàn)多人聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Java實現(xiàn)多人聊天室的具體代碼,供大家參考,具體內(nèi)容如下

1.客戶端

package tk.javazhangwei.net.tcp.chat.Demo03;
?
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
?
/***
?* 創(chuàng)建客戶端 ?發(fā)送數(shù)據(jù)+接收數(shù)據(jù)
?*?
?* @author zw
?*
?*/
public class Client {
?? ?public static void main(String[] args) throws IOException {
?? ??? ?BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
?? ??? ?System.out.println("請輸入一個喜歡的名稱:");
?? ??? ?String name = bf.readLine();
?? ??? ?if(name.equals("")) {
?? ??? ??? ?return;
?? ??? ?}
?? ??? ?
?? ??? ?Socket client = new Socket("localhost",1025);
?? ??? ?//控制臺輸入信息
?? ??? ?//控制臺輸入信息
?? ??? ?new Thread(new Send(client,name)).start();//一條路徑
?? ??? ?new Thread(new Receive(client)).start();//一條路徑
}
}

2.服務(wù)端(寫了個內(nèi)部類:負(fù)責(zé)接收與發(fā)送多進(jìn)程)

package tk.javazhangwei.net.tcp.chat.Demo03;
?
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
?
public class Server {
?? ?List<myChannel> all = new ArrayList<myChannel>();
?? ?
?? ?
?? ?
?? ?public static void main(String[] args) throws IOException {
?? ??? ?new Server().start();
?? ??? ?
?
?? ?}
?? ?
?? ?
?? ?public void start() throws IOException {
?? ??? ?ServerSocket server = new ServerSocket(1025);
?? ??? ?while (true) {
?? ??? ??? ?Socket socket = server.accept();
?? ??? ??? ?myChannel mc = new myChannel(socket);
?? ??? ??? ?Thread t = new Thread(mc);
?? ??? ??? ?all.add(mc);
?? ??? ??? ?t.start();
?? ?}
}
?
?
/***
?* 一個客戶端 一個通路
?* @author zw
?*
?*/
class myChannel implements Runnable{
?? ?private DataInputStream dis;
?? ?private DataOutputStream dos;
?? ?private boolean isRuning=true;
?? ?private String name;
?? ?
?? ?public myChannel(Socket socket) throws IOException{
?? ??? ?try {
?? ??? ??? ?dis = new DataInputStream(socket.getInputStream());
?? ??? ??? ?dos = new DataOutputStream(socket.getOutputStream());
?? ??? ??? ?this.name = dis.readUTF();
?? ??? ??? ?send("歡迎進(jìn)入聊天室");
?? ??? ??? ?senOthers(this.name + "進(jìn)入了聊天室");
?? ??? ?} catch (IOException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?//e.printStackTrace();
?? ??? ??? ??? ?isRuning=false;
?? ??? ??? ??? ?dos.close();
?? ??? ??? ??? ?dis.close();
?? ??? ??? ??? ?
?? ??? ?}
?? ??? ?
?? ?}
?? ?/***
?? ? * 讀取數(shù)據(jù)
?? ? *?
?? ? * @return
?? ? * @throws IOException
?? ? */
?? ?private String receive() throws IOException {
?? ??? ?String msg ="";
?? ??? ?try {
?? ??? ??? ?msg =dis.readUTF();
?? ??? ?} catch (IOException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?//e.printStackTrace();
?? ??? ??? ?isRuning=false;
?? ??? ??? ?dis.close();
?? ??? ?
?? ??? ?}
?? ??? ?
?? ??? ?
?? ??? ?return msg;?? ?
?? ?}
?? ?/***
?? ? * 發(fā)送數(shù)據(jù)
?? ? * @throws IOException?
?? ? */
?? ?private void send(String msg) throws IOException {
?? ??? ?if(msg==null&& msg.equals("")) {
?? ??? ??? ?return;
?? ??? ?}
?? ??? ?try {
?? ??? ??? ?dos.writeUTF(msg);
?? ??? ??? ?dos.flush();
?? ??? ?} catch (IOException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?//e.printStackTrace();
?? ??? ??? ?isRuning=false;
?? ??? ??? ?dos.close();
?? ??? ??? ?all.remove(this);
?? ??? ?}
?? ??? ?
?? ?}
?? ?/***
?? ? * 發(fā)送給其他客戶端
?? ? * @throws IOException?
?? ? */
?? ?private void senOthers(String msg) throws IOException {
?? ??? ?if (msg.startsWith("@")&&msg.indexOf(":")>-1) {// 表示為私聊
?? ??? ??? ?//獲取name
?? ??? ??? ?String name = msg.substring(1, msg.indexOf(":"));
?? ??? ??? ?String content = msg.substring(msg.indexOf(":")+1);//獲取冒號后的正文
?? ??? ??? ?for (myChannel others : all) {
?? ??? ??? ??? ?if(others.name.equals(name)) {
?? ??? ??? ??? ??? ?others.send(this.name+"對您瞧瞧的說:"+content);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?
?? ??? ?} else {
?? ??? ?//遍歷容器
?? ??? ?for(myChannel others:all) {
?? ??? ??? ?if(others == this) {//如果是本身,就跳過
?? ??? ??? ??? ?continue;
?? ??? ??? ?}
?? ??? ??? ?others.send(this.name+"對所有人說:"+msg);
?? ??? ?}
?? ??? ?}
?? ?}
?? ?
?? ?@Override
?? ?public void run() {
?? ??? ?while(isRuning) {
?? ??? ??? ?try {
?? ??? ??? ??? ?senOthers(receive()) ;
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ?}
?? ??? ?}
}
}

3.客戶端的發(fā)送與接收多進(jìn)程

package tk.javazhangwei.net.tcp.chat.Demo03;
?
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
?
/***
?* 發(fā)送數(shù)據(jù)
?*?
?* @author zw
?*
?*/
public class Send implements Runnable{
?? ?//控制臺輸入流
?? ?private BufferedReader console;
?? ?//管道輸出流
?? ?private DataOutputStream dos;
?? ?private String name;
?? ?
?? ?private boolean isRuning =true;//線程是否運行
?? ?
?? ?public Send() {
?? ??? ?console =new BufferedReader(new InputStreamReader(System.in));
?? ?}
?? ?
?? ?public Send(Socket client,String name) throws IOException {
?? ??? ?this();
?? ??? ?try {
?? ??? ??? ?dos = new DataOutputStream(client.getOutputStream());
?? ??? ??? ?this.name = name;
?? ??? ??? ?send(name);
?? ??? ?} catch (IOException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?//e.printStackTrace();
?? ??? ??? ? isRuning =false;
?? ??? ??? ? dos.close();
?? ??? ??? ? console.close();
?? ??? ?}
?? ?}
?? ?private String getMsgFromConsole() {
?? ??? ?try {
?? ??? ??? ?return console.readLine();
?? ??? ?} catch (IOException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?return "";
?? ?}
?
?? ?
@Override
?? ?public void run() {
?? ??? ?
?? ??? ?while(isRuning) {
?? ??? ??? ?try {
?? ??? ??? ??? ?send(getMsgFromConsole());
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ?}
?? ?}

?
?? ?public void send(String msg) throws IOException {
?? ??? ?if (msg!=null && !msg.equals("")) {
?? ??? ??? ?try {
?? ??? ??? ??? ?dos.writeUTF(msg);
?? ??? ??? ??? ?dos.flush();// 強(qiáng)制刷新
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?// e.printStackTrace();
?? ??? ??? ??? ?isRuning = false;
?? ??? ??? ??? ?dos.close();
?? ??? ??? ??? ?console.close();
?
?? ??? ??? ?}
?? ??? ?}
?? ?}
?
}
package tk.javazhangwei.net.tcp.chat.Demo03;
?
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
?
/***
?* 接收數(shù)據(jù)
?* @author zw
?*
?*/
public class Receive implements Runnable{
?? ?//客戶端的輸入流
?? ?private DataInputStream dis;
?? ?private boolean isRuning = true;
?? ?
?? ?public Receive() {
?? ?
?? ?}
?
?? ?public Receive(Socket client) {
?? ??? ?super();
?? ??? ?try {
?? ??? ??? ?dis = new DataInputStream(client.getInputStream());
?? ??? ?} catch (IOException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?//e.printStackTrace();
?? ??? ??? ?isRuning =false;
?? ??? ??? ?try {
?? ??? ??? ??? ?dis.close();
?? ??? ??? ?} catch (IOException e1) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e1.printStackTrace();
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?/***
?? ? * 接收數(shù)據(jù)
?? ? * @return
?? ? * @throws IOException?
?? ? */
?? ?public String receive() throws IOException {
?? ??? ?String msg = "";
?? ??? ?try {
?? ??? ??? ?msg =dis.readUTF();
?? ??? ?} catch (IOException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ??? ?isRuning =false;
?? ??? ??? ?dis.close();
?? ??? ?}
?? ??? ?
?? ??? ?return msg;
?? ?}
?? ?@Override
?? ?public void run() {
?? ??? ?while(isRuning) {
?? ??? ??? ?try {
?? ??? ??? ??? ?System.out.println(receive());
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ?}
?
}

4.效果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 超級好用的輕量級JSON處理命令jq(最新推薦)

    超級好用的輕量級JSON處理命令jq(最新推薦)

    jq是一個輕量級的命令行工具,讓你可以非常方便地處理JSON數(shù)據(jù),如切分、過濾、映射、轉(zhuǎn)化等,就像sed、awk、grep文本處理三劍客一樣,這篇文章主要介紹了超級好用的輕量級JSON處理命令jq,需要的朋友可以參考下
    2023-01-01
  • java中Sources目錄Resources目錄的區(qū)別解讀

    java中Sources目錄Resources目錄的區(qū)別解讀

    這篇文章主要介紹了java中Sources目錄Resources目錄的區(qū)別解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Mybatis通用Mapper(tk.mybatis)的使用

    Mybatis通用Mapper(tk.mybatis)的使用

    本文主要介紹了Mybatis通用Mapper(tk.mybatis)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 詳解Swagger接口文檔和常用注解的使用

    詳解Swagger接口文檔和常用注解的使用

    Swagger是一款遵循?Restful?風(fēng)格的接口文檔開發(fā)神器,支持基于?API?自動生成接口文檔。本文將為大家講講Swagger接口文檔和常用注解的使用方法,需要的可以參考一下
    2022-08-08
  • Idea入門教程之一分鐘創(chuàng)建一個Java工程

    Idea入門教程之一分鐘創(chuàng)建一個Java工程

    idea作為Java開發(fā)最好用的編寫代碼軟件之一,首先進(jìn)行的就是工程的創(chuàng)建,這篇文章主要給大家介紹了關(guān)于Idea入門教程之一分鐘創(chuàng)建一個Java工程的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2024-07-07
  • Struts2實現(xiàn)文件上傳功能

    Struts2實現(xiàn)文件上傳功能

    這篇文章主要為大家詳細(xì)介紹了Struts2實現(xiàn)文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 16個SpringBoot擴(kuò)展接口的總結(jié)和實例

    16個SpringBoot擴(kuò)展接口的總結(jié)和實例

    Spring Boot是一個開源的Java框架,它簡化了基于Spring的應(yīng)用程序的開發(fā)和部署,它提供了許多強(qiáng)大的特性和擴(kuò)展接口,本文給大家介紹了16個常用的Spring Boot擴(kuò)展接口,需要的朋友可以參考下
    2023-09-09
  • Java構(gòu)造函數(shù)通透理解篇

    Java構(gòu)造函數(shù)通透理解篇

    這篇文章主要介紹了Java構(gòu)造函數(shù),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • java實現(xiàn)基因序列比較的示例代碼

    java實現(xiàn)基因序列比較的示例代碼

    這篇文章主要介紹了java實現(xiàn)基因序列比較的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • SpringMVC異步處理操作(Callable和DeferredResult)

    SpringMVC異步處理操作(Callable和DeferredResult)

    這篇文章主要介紹了SpringMVC異步處理操作(Callable和DeferredResult),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01

最新評論