亚洲精品中文字幕无乱码_久久亚洲精品无码AV大片_最新国产免费Av网址_国产精品3级片

java語言

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

時間:2024-11-06 09:41:31 java語言 我要投稿
  • 相關(guān)推薦

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

  導(dǎo)語:如何運用Java socket實現(xiàn)多人聊天室功能呢?下面是小編給大家提供的代碼實現(xiàn),大家可以參考閱讀,更多詳情請關(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)聽

  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();

  }

  }

  //斷開連接

  private void disconnected(){

  bConnected = false;

  try {

  dos.close();

  dis.close();

  socket.close();

  } catch (IOException e1) {

  // TODO Auto-generated catch block

  e1.printStackTrace();

  }

  }

  //鍵盤回車事件

  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();

  }

  }

  }

  //開啟線程接受服務(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 {

  //開啟服務(wù)端Socket

  ServerSocket seso = new ServerSocket(8888);

  started = true;

  while(started){

  //接受客戶端連接請求

  Socket sos = seso.accept();

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

  //開啟線程處理客戶端通信

  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ā)送給每個客戶端

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

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

  }

  }

  }catch (IOException e) {

  try {

  //如果客戶端出錯或關(guān)閉,直接關(guān)閉連接,并移除List中的當前線程

  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();

  }

  }

  }

  }

  }

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

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

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

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

關(guān)于Java動態(tài)實現(xiàn)的方法08-23

java圖片處理功能介紹06-23

php實現(xiàn)圖片縮放功能類08-17

利用python實現(xiàn)簡單爬蟲功能09-25

實現(xiàn)java屏幕抓屏的方法08-24

java如何構(gòu)造09-21

PHP實現(xiàn)搜索查詢功能的方法技巧08-01

php中怎么實現(xiàn)回刪功能08-31