av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

java語(yǔ)言

如何運(yùn)用Java socket實(shí)現(xiàn)多人聊天室功能

時(shí)間:2025-01-14 14:49:15 java語(yǔ)言 我要投稿
  • 相關(guān)推薦

如何運(yùn)用Java socket實(shí)現(xiàn)多人聊天室功能

  導(dǎo)語(yǔ):如何運(yùn)用Java socket實(shí)現(xiàn)多人聊天室功能呢?下面是小編給大家提供的代碼實(shí)現(xiàn),大家可以參考閱讀,更多詳情請(qǐng)關(guān)注應(yīng)屆畢業(yè)生考試網(wǎng)。

  目錄結(jié)構(gòu):

  ChatClient:

  package com.panda.chat;

 

  import java.awt.*;

  import java.awt.event.*;

  import java.io.*;

  import java.net.*;

  @SuppressWarnings("serial")

  public class ChatClient extends Frame {

  private TextArea ta = new TextArea();

  private TextField tf = new TextField();

  private DataOutputStream dos = null;

  private DataInputStream dis = null;

  private Socket socket = null;

  private boolean bConnected = false;

  private Thread thread=null;

  public static void main(String[] args) {

  new ChatClient().frameClient();

  }

  public void frameClient(){

  setSize(400, 400);

  setLocation(400,300);

  add(ta,BorderLayout.NORTH);

  add(tf,BorderLayout.SOUTH);

  pack();

  tf.addActionListener(new TfListener());

  //關(guān)閉窗口事件監(jiān)聽(tīng)

  this.addWindowListener(new WindowAdapter() {

  @Override

  public void windowClosing(WindowEvent e) {

  disconnected();

  System.exit(0);

  }

  });

  this.connect();

  setVisible(true);

  }

  //鏈接服務(wù)器地址

  private void connect(){

  try {

  socket = new Socket("127.0.0.1", 8888);

  thread=new Thread(new ChatThread());

  thread.start();

  dos = new DataOutputStream(socket.getOutputStream());

  dis = new DataInputStream(socket.getInputStream());

  } catch (UnknownHostException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  }

  //斷開(kāi)連接

  private void disconnected(){

  bConnected = false;

  try {

  dos.close();

  dis.close();

  socket.close();

  } catch (IOException e1) {

  // TODO Auto-generated catch block

  e1.printStackTrace();

  }

  }

  //鍵盤(pán)回車(chē)事件

  private class TfListener implements ActionListener{

  @Override

  public void actionPerformed(ActionEvent e) {

  String strMsg = tf.getText();

  tf.setText("");

  try {

  dos.writeUTF(strMsg);

  dos.flush();

  } catch (IOException e1) {

  // TODO Auto-generated catch block

  e1.printStackTrace();

  }

  }

  }

  //開(kāi)啟線(xiàn)程接受服務(wù)器信息

  private class ChatThread implements Runnable{

  @Override

  public void run() {

  try {

  bConnected = true;

  while(bConnected){

  String msg = dis.readUTF();

  String taText = ta.getText();

  ta.setText(taText+msg+"\n");

  }

  } catch (SocketException e) {

  System.out.println("退出");;

  } catch (IOException e) {

  e.printStackTrace();

  }

  }

  }

  }

  ChatServer:

  package com.panda.chat;

  import java.io.*;

  import java.net.*;

  import java.util.*;

  public class ChatServer {

  private boolean started = false;

  private List<ChatThread> chatThreads = new ArrayList<ChatThread>();

  public static void main(String[] args) {

  new ChatServer().startServer();

  }

  private void startServer(){

  try {

  //開(kāi)啟服務(wù)端Socket

  ServerSocket seso = new ServerSocket(8888);

  started = true;

  while(started){

  //接受客戶(hù)端連接請(qǐng)求

  Socket sos = seso.accept();

  System.out.println("一個(gè)客戶(hù)端已連接");

  //開(kāi)啟線(xiàn)程處理客戶(hù)端通信

  ChatThread ct = new ChatThread(sos);

  chatThreads.add(ct);

  new Thread(ct).start();

  }

  } catch (IOException e) {

  e.printStackTrace();

  }

  }

  private class ChatThread implements Runnable{

  private Socket socket;

  private DataInputStream din=null;

  private DataOutputStream don=null;

  private boolean bConnected = false;

  public ChatThread(Socket socket) {

  super();

  this.socket = socket;

  }

  //發(fā)送信息的函數(shù)

  private void send(String strMsgIn){

  try{

  don.writeUTF(strMsgIn);

  don.flush();

  }catch(IOException e){

  e.printStackTrace();

  }

  }

  @Override

  public void run() {

  try{

  din = new DataInputStream(socket.getInputStream());

  don = new DataOutputStream(socket.getOutputStream());

  //讀取數(shù)據(jù)

  bConnected = true;

  while(bConnected){

  String strMsgIn = din.readUTF();

  System.out.println(strMsgIn);

  //接收到數(shù)據(jù)后發(fā)送給每個(gè)客戶(hù)端

  for(int i =0;i<chatThreads.size();i++){

  chatThreads.get(i).send(strMsgIn);

  }

  }

  }catch (IOException e) {

  try {

  //如果客戶(hù)端出錯(cuò)或關(guān)閉,直接關(guān)閉連接,并移除List中的當(dāng)前線(xiàn)程

  socket.close();

  chatThreads.remove(this);

  } catch (IOException e1) {

  // TODO Auto-generated catch block

  e1.printStackTrace();

  }

  } finally{

  try {

  din.close();

  don.close();

  socket.close();

  } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  }

  }

  }

  }

  運(yùn)行ChatSever后,再同時(shí)打開(kāi)多次ChatClient,就可以實(shí)現(xiàn)多人聊天了,你也試試。

【如何運(yùn)用Java socket實(shí)現(xiàn)多人聊天室功能】相關(guān)文章:

Java如何通過(guò)Socket實(shí)現(xiàn)TCP服務(wù)端10-08

java如何實(shí)現(xiàn)后臺(tái)自動(dòng)發(fā)郵件功能09-17

講解Java的Socket網(wǎng)絡(luò)編程的多播與廣播實(shí)現(xiàn)09-03

java實(shí)現(xiàn)后臺(tái)自動(dòng)發(fā)郵件功能08-06

java如何實(shí)現(xiàn)漢諾塔08-08

如何運(yùn)用PS實(shí)現(xiàn)人物姿態(tài)的改變08-20

java通用組合算法如何實(shí)現(xiàn)09-12

Java中如何實(shí)現(xiàn)顯示動(dòng)態(tài)的時(shí)間09-28

Java網(wǎng)絡(luò)基礎(chǔ)和Socket通信基礎(chǔ)10-25