最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java聊天室的實(shí)現(xiàn)代碼

 更新時(shí)間:2020年03月31日 16:41:16   作者:北漂程序員-阿力  
這篇文章主要為大家詳細(xì)介紹了java聊天室的實(shí)現(xiàn)代碼,一個(gè)多客戶(hù)端聊天室,支持多客戶(hù)端聊天,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

聊天室界面:

源碼:

public class ClientFrame extends Frame {

 private TextField textFieldContent = new TextField();
 private TextArea textAreaContent = new TextArea();
 private Socket socket = null;
 private OutputStream out = null;
 private DataOutputStream dos = null;
 private InputStream in = null;
 private DataInputStream dis = null;
 private boolean flag = false;

 /**
 * 學(xué)校:山東師范大學(xué) 程序員:外力_Victor 日期:2016年5月8日 上午9:19:51 功能:?jiǎn)?dòng)客戶(hù)端程序
 * 
 * @param args
 */
 public static void main(String[] args) {
 new ClientFrame().init();
 }

 /**
 * 學(xué)校:山東師范大學(xué) 程序員:外力_Victor 日期:2016年5月8日 上午9:20:43 功能:對(duì)窗口進(jìn)行初始化
 */
 private void init() {
 this.setSize(300, 300);
 setLocation(250, 150);
 setVisible(true);
 setTitle("WeChatRoom");

 // 添加控件
 this.add(textAreaContent);
 this.add(textFieldContent, BorderLayout.SOUTH);
 textAreaContent.setFocusable(false);
 pack();

 // 關(guān)閉事件
 addWindowListener(new WindowAdapter() {
  public void windowClosing(WindowEvent e) {
  System.out.println("用戶(hù)試圖關(guān)閉窗口");
  disconnect();
  System.exit(0);
  }

 });
 // textFieldContent添加回車(chē)事件
 textFieldContent.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
  onClickEnter();
  }
 });

 // 建立連接
 connect();
 new Thread(new ReciveMessage()).start();
 }

 private class ReciveMessage implements Runnable {
 @Override
 public void run() {
  flag = true;
  try {
  while (flag) {
   String message = dis.readUTF();
   textAreaContent.append(message + "\n");
  }
  } catch (EOFException e) {
  flag = false;
  System.out.println("客戶(hù)端已關(guān)閉");
  // e.printStackTrace();
  } catch (SocketException e) {
  flag = false;
  System.out.println("客戶(hù)端已關(guān)閉");
  // e.printStackTrace();
  } catch (IOException e) {
  flag = false;
  System.out.println("接受消息失敗");
  e.printStackTrace();
  }
 }

 }

 /**
 * 功能:當(dāng)點(diǎn)擊回車(chē)時(shí)出發(fā)的事件 學(xué)校:山東師范大學(xué) 程序員:外力_Victor 日期:2016年5月8日 上午9:49:30
 */
 private void onClickEnter() {
 String message = textFieldContent.getText().trim();
 if (message != null && !message.equals("")) {
  String time = new SimpleDateFormat("h:m:s").format(new Date());
  textAreaContent.append(time + "\n" + message + "\n");
  textFieldContent.setText("");
  sendMessageToServer(message);
 }
 }

 /**
 * 功能:給服務(wù)器發(fā)送消息 學(xué)校:山東師范大學(xué) 程序員:外力_Victor 日期:2016年5月8日 上午10:13:48
 * 
 * @param message
 */
 private void sendMessageToServer(String message) {
 try {
  dos.writeUTF(message);
  dos.flush();
 } catch (IOException e) {
  System.out.println("發(fā)送消息失敗");
  e.printStackTrace();
 }
 }

 /**
 * 功能:申請(qǐng)socket鏈接 學(xué)校:山東師范大學(xué) 程序員:外力_Victor 日期:2016年5月8日 上午10:00:38
 */
 private void connect() {
 try {
  socket = new Socket("localhost", 8888);
  out = socket.getOutputStream();
  dos = new DataOutputStream(out);
  in = socket.getInputStream();
  dis = new DataInputStream(in);
 } catch (UnknownHostException e) {
  System.out.println("申請(qǐng)鏈接失敗");
  e.printStackTrace();
 } catch (IOException e) {
  System.out.println("申請(qǐng)鏈接失敗");
  e.printStackTrace();
 }
 }

 /**
 * 功能:關(guān)閉流和鏈接 學(xué)校:山東師范大學(xué) 程序員:外力_Victor 日期:2016年5月8日 上午10:01:32
 */
 private void disconnect() {
 flag = false;
 if (dos != null) {
  try {
  dos.close();
  } catch (IOException e) {
  System.out.println("dos關(guān)閉失敗");
  e.printStackTrace();
  }
 }
 if (out != null) {
  try {
  out.close();
  } catch (IOException e) {
  System.out.println("dos關(guān)閉失敗");
  e.printStackTrace();
  }
 }
 if (socket != null) {
  try {
  socket.close();
  } catch (IOException e) {
  System.out.println("socket關(guān)閉失敗");
  e.printStackTrace();
  }
  ;
 }
 }

}
package com.chat;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;

public class ChatServer {

 private List<Client> clients = new ArrayList<>();

 /**
 * 功能:?jiǎn)?dòng)ChatSever 學(xué)校:山東師范大學(xué) 程序員:外力_Victor 日期:2016年5月8日 上午10:26:41
 * 
 * @param args
 */
 public static void main(String[] args) {
 new ChatServer().init();
 }

 /**
 * 功能:對(duì)chatserver初始化 學(xué)校:山東師范大學(xué) 程序員:外力_Victor 日期:2016年5月8日 上午10:27:09
 */
 private void init() {
 System.out.println("服務(wù)器已開(kāi)啟");
 // BindException

 ServerSocket ss = null;
 Socket socket = null;
 try {
  ss = new ServerSocket(8888);
 } catch (BindException e) {
  System.out.println("端口已被占用");
  e.printStackTrace();
 } catch (IOException e1) {
  e1.printStackTrace();
 }
 try {
  Client client = null;
  while (true) {
  socket = ss.accept();
  System.out.println("客戶(hù)駕到");
  client = new Client(socket);
  clients.add(client);
  new Thread(client).start();
  }
 } catch (IOException e) {
  e.printStackTrace();
 }
 }

 private class Client implements Runnable {
 private Socket socket = null;
 InputStream in = null;
 DataInputStream din = null;
 OutputStream out = null;
 DataOutputStream dos = null;
 boolean flag = true;

 public Client(Socket socket) {
  this.socket = socket;
  try {
  in = socket.getInputStream();
  din = new DataInputStream(in);
  } catch (IOException e) {
  System.out.println("接受消息失敗");
  e.printStackTrace();
  }

 }

 public void run() {

  String message;
  try {
  while (flag) {
   message = din.readUTF();
   // System.out.println("客戶(hù)說(shuō):" + message);
   forwordToAllClients(message);
  }
  } catch (SocketException e) {
  flag = false;
  System.out.println("客戶(hù)下線(xiàn)");
  clients.remove(this);
  // e.printStackTrace();
  } catch (EOFException e) {
  flag = false;
  System.out.println("客戶(hù)下線(xiàn)");
  clients.remove(this);
  // e.printStackTrace();
  } catch (IOException e) {
  flag = false;
  System.out.println("接受消息失敗");
  clients.remove(this);
  e.printStackTrace();
  }

  if (din != null) {
  try {
   din.close();
  } catch (IOException e) {
   System.out.println("din關(guān)閉失敗");
   e.printStackTrace();
  }
  }
  if (in != null) {
  try {
   in.close();
  } catch (IOException e) {
   System.out.println("din關(guān)閉失敗");
   e.printStackTrace();
  }
  }
  if (socket != null) {
  try {
   socket.close();
  } catch (IOException e) {
   System.out.println("din關(guān)閉失敗");
   e.printStackTrace();
  }
  }

 }

 /**
  * 功能:轉(zhuǎn)發(fā)給所有客戶(hù)端 學(xué)校:山東師范大學(xué) 程序員:外力_Victor 日期:2016年5月8日 上午11:11:59
  * 
  * @param message
  * @throws IOException
  */
 private void forwordToAllClients(String message) throws IOException {
  for (Client c : clients) {
  if (c != this) {
   out = c.socket.getOutputStream();
   dos = new DataOutputStream(out);
   forwordToClient(message);
  }
  }
 }

 /**
  * 功能:發(fā)送給一個(gè)客戶(hù)端 學(xué)校:山東師范大學(xué) 程序員:外力_Victor 日期:2016年5月8日 上午11:16:12
  * 
  * @throws IOException
  */
 private void forwordToClient(String message) throws IOException {
  dos.writeUTF(message);
  dos.flush();
  System.out.println("轉(zhuǎn)發(fā)成功!");
 }

 }
}

源碼下載:java聊天室代碼

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

相關(guān)文章

  • 詳解Java8?StreamAPI中的map()方法

    詳解Java8?StreamAPI中的map()方法

    Stream?API?是Java8中新加入的功能,這篇文章主要帶大家了解一下?Stream?API?中的?map()?方法的使用,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-04-04
  • SpringBoot實(shí)現(xiàn)整合微信支付方法詳解

    SpringBoot實(shí)現(xiàn)整合微信支付方法詳解

    這篇文章主要介紹了SpringBoot實(shí)現(xiàn)整合微信支付的過(guò)程詳解,文中的示例代碼對(duì)我們的工作或?qū)W習(xí)有一定的幫助,感興趣的小伙伴可以跟隨小編學(xué)習(xí)一下
    2021-12-12
  • 使用Spring Boot+MyBatis框架做查詢(xún)操作的示例代碼

    使用Spring Boot+MyBatis框架做查詢(xún)操作的示例代碼

    這篇文章主要介紹了使用Spring Boot+MyBatis框架做查詢(xún)操作的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • Java Web項(xiàng)目部署在Tomcat運(yùn)行出錯(cuò)與解決方法示例

    Java Web項(xiàng)目部署在Tomcat運(yùn)行出錯(cuò)與解決方法示例

    這篇文章主要介紹了Java Web項(xiàng)目部署在Tomcat運(yùn)行出錯(cuò)與解決方法,結(jié)合具體實(shí)例形式分析了Java Web項(xiàng)目部署在Tomcat過(guò)程中由于xml配置文件導(dǎo)致的錯(cuò)誤問(wèn)題常見(jiàn)提示與解決方法,需要的朋友可以參考下
    2017-03-03
  • Spring MVC實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)增刪改查完整實(shí)例

    Spring MVC實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)增刪改查完整實(shí)例

    這篇文章主要介紹了Spring MVC實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)增刪改查完整實(shí)例,從創(chuàng)建一個(gè)web項(xiàng)目開(kāi)始,分享了項(xiàng)目結(jié)構(gòu)以及具體Java代碼和前端頁(yè)面等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以了解下。
    2017-12-12
  • spring-data-jpa使用自定義repository來(lái)實(shí)現(xiàn)原生sql

    spring-data-jpa使用自定義repository來(lái)實(shí)現(xiàn)原生sql

    這篇文章主要介紹了在spring-data-jpa中使用自定義repository來(lái)實(shí)現(xiàn)原生sql,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • mybatis-plus 批量插入示例代碼

    mybatis-plus 批量插入示例代碼

    正常我們使用mybatis-plus插入的時(shí)候,首先想到的是saveBatch方法,不過(guò)看了下打印出來(lái)的sql和底層代碼,才發(fā)現(xiàn)它并不是真正的批量插入這篇文章主要介紹了mybatis-plus 批量插入示例,需要的朋友可以參考下
    2023-07-07
  • springboot整合JSR303校驗(yàn)功能實(shí)現(xiàn)代碼

    springboot整合JSR303校驗(yàn)功能實(shí)現(xiàn)代碼

    這篇文章主要介紹了springboot整合JSR303校驗(yàn)功能實(shí)現(xiàn),JSR303校驗(yàn)方法有統(tǒng)一校驗(yàn)的需求,統(tǒng)一校驗(yàn)實(shí)現(xiàn)以及分組校驗(yàn),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-01-01
  • Spring boot外部配置(配置中心化)詳解

    Spring boot外部配置(配置中心化)詳解

    這篇文章主要給大家介紹了關(guān)于Spring boot外部配置(配置中心化)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12
  • 簡(jiǎn)單操作實(shí)現(xiàn)Java jsp servlet文件上傳過(guò)程解析

    簡(jiǎn)單操作實(shí)現(xiàn)Java jsp servlet文件上傳過(guò)程解析

    這篇文章主要介紹了簡(jiǎn)單操作實(shí)現(xiàn)Java jsp servlet文件上傳過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10

最新評(píng)論

竹山县| 富宁县| 伊吾县| 富源县| 清苑县| 水富县| 白银市| 岳阳县| 通江县| 平陆县| 清徐县| 博客| 拜泉县| 寿光市| 大丰市| 拜泉县| 安康市| 通江县| 岱山县| 循化| 湖北省| 右玉县| 合阳县| 织金县| 长垣县| 商河县| 柯坪县| 万州区| 上林县| 玉龙| 丘北县| 大洼县| 蓝山县| 临沭县| 调兵山市| 普兰县| 冕宁县| 江西省| 关岭| 南通市| 峡江县|