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

JavaGUI模仿QQ聊天功能完整版

 更新時(shí)間:2021年07月04日 15:56:54   作者:KillerCodes  
這篇文章主要為大家詳細(xì)介紹了JavaGUI模仿QQ聊天功能完整版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了JavaGUI模仿QQ聊天功能完整代碼,供大家參考,具體內(nèi)容如下

ClientForm代碼:

package GUISocket.chat.Client;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.EventQueue;
 
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;
 
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
 
public class ClientForm extends JFrame {
 
 private JPanel contentPane;
 DefaultListModel<String> itemUsers;
 private JTextField textIP;
 private JTextField textPort;
 public JTextField textUser;
 public JTextArea textLog;
 public JList listUser;
 public JTextArea textSend ;
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     ClientForm frame = new ClientForm();
     frame.setVisible(true);
     ClientMG.getClientMG().setClientForm(frame);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }
 public ClientForm() {
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setBounds(100, 100, 589, 607);
  contentPane = new JPanel();
  contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  setContentPane(contentPane);
  contentPane.setLayout(null);
  
  JLabel label = new JLabel("配置信息");
  label.setBounds(10, 10, 54, 15);
  contentPane.add(label);
  
  JLabel lblIp = new JLabel("IP");
  lblIp.setBounds(10, 35, 27, 15);
  contentPane.add(lblIp);
  
  textIP = new JTextField();
  textIP.setText("192.168.1.2");
  textIP.setBounds(33, 35, 92, 21);
  contentPane.add(textIP);
  textIP.setColumns(10);
  
  JLabel label_1 = new JLabel("端口");
  label_1.setBounds(137, 35, 38, 15);
  contentPane.add(label_1);
  
  textPort = new JTextField();
  textPort.setText("8900");
  textPort.setBounds(168, 32, 66, 21);
  contentPane.add(textPort);
  textPort.setColumns(10);
  
  JLabel label_2 = new JLabel("用戶名");
  label_2.setBounds(255, 38, 54, 15);
  contentPane.add(label_2);
  
  textUser = new JTextField();
  textUser.setBounds(302, 35, 66, 21);
  contentPane.add(textUser);
  textUser.setColumns(10);
  
  JButton LOGIN = new JButton("登錄");
  LOGIN.setBounds(395, 34, 66, 23);
  contentPane.add(LOGIN);
  
  JButton btnClose = new JButton("關(guān)閉");
  btnClose.addActionListener(new BtnCloseActionListener());
  btnClose.setBounds(480, 31, 71, 23);
  contentPane.add(btnClose);
  
  JPanel panel = new JPanel();
  panel.setBounds(0, 10, 573, 61);
  contentPane.add(panel);
  panel.setLayout(null);
  
  JPanel panel_1 = new JPanel();
  panel_1.setBounds(0, 81, 573, 369);
  contentPane.add(panel_1);
  panel_1.setLayout(null);
  
  JLabel label_3 = new JLabel("聊天記錄");
  label_3.setBounds(10, 10, 54, 15);
  panel_1.add(label_3);
  
  JScrollPane scrollPane = new JScrollPane();
  scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  scrollPane.setBounds(20, 35, 323, 324);
  panel_1.add(scrollPane);
  
  textLog = new JTextArea();
  textLog.setWrapStyleWord(true);
  textLog.setLineWrap(true);
  scrollPane.setViewportView(textLog);
  
  JLabel label_4 = new JLabel("在線用戶");
  label_4.setBounds(351, 10, 54, 15);
  panel_1.add(label_4);
  
  JScrollPane scrollPane_1 = new JScrollPane();
  scrollPane_1.setBounds(353, 35, 210, 324);
  panel_1.add(scrollPane_1);
  
  
  this.itemUsers=new DefaultListModel<String>();
  this.listUser=new JList(itemUsers);
  scrollPane_1.setViewportView(this.listUser);
  
  
  JPanel panel_2 = new JPanel();
  panel_2.setBounds(10, 449, 553, 119);
  contentPane.add(panel_2);
  panel_2.setLayout(null);
  
  JLabel label_5 = new JLabel("操作");
  label_5.setBounds(10, 10, 54, 15);
  panel_2.add(label_5);
  
  JScrollPane scrollPane_2 = new JScrollPane();
  scrollPane_2.setBounds(10, 22, 533, 64);
  panel_2.add(scrollPane_2);
  
  textSend = new JTextArea();
  textSend.setWrapStyleWord(true);
  textSend.setLineWrap(true);
  scrollPane_2.setViewportView(textSend);
  
  JButton button_1 = new JButton("群發(fā)");
  button_1.addActionListener(new Button_1ActionListener());
  button_1.setBounds(307, 86, 93, 23);
  panel_2.add(button_1);
  
  JButton sendMG = new JButton("發(fā)送");
  sendMG.addActionListener(new SendMGActionListener());
  sendMG.setBounds(432, 86, 93, 23);
  panel_2.add(sendMG);
  
  LOGIN.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    //連接服務(wù)器user
    String IP=textIP.getText().trim();
    int port=Integer.parseInt(textPort.getText().trim());
    String user=textUser.getText().trim();
    
    if(ClientMG.getClientMG().Connect(IP,port,user)) {
     ClientMG.getClientMG().setLogTxt("已經(jīng)連接到服務(wù)器");
     
    }
    else {
     ClientMG.getClientMG().setLogTxt("連接服務(wù)器失敗");
    }
   }
  });
 }
 private class SendMGActionListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
   
   //發(fā)送信息
   //1.獲取選擇的用戶名稱
   //2.發(fā)送給服務(wù)器端含有接收用戶信息的交互協(xié)議串
   String SenderName=ClientMG.getClientMG().getClientThd().getName();
   String RecName=listUser.getSelectedValue().toString();
   String MSGinfo=textSend.getText().trim();
   
   String sMsg="MSG|"+SenderName+"|"+RecName+"|"+MSGinfo;
   ClientMG.getClientMG().getClientThd().sendMsg(sMsg);
   //將消息內(nèi)容顯示到聊天記錄中
   //[發(fā)送者]
   //消息內(nèi)容
   //清空發(fā)送消息框
   ClientMG.getClientMG().setLogTxt("[我]:");
   ClientMG.getClientMG().setLogTxt(MSGinfo);
   
   textSend.setText("");
  }
 }
 private class Button_1ActionListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
   //群發(fā)信息
   //1.獲取選擇的用戶名稱
   //2.發(fā)送給服務(wù)器端含有接收用戶信息的交互協(xié)議串
   //發(fā)送到服務(wù)器,MSG|SenderName|RecName|MSGInfo
   String SenderName=ClientMG.getClientMG().getClientThd().getName();
   String RecName="ALL";
   String MSGinfo=textSend.getText().trim();
   
   String sMsg="MSG|"+SenderName+"|"+RecName+"|"+MSGinfo;
   ClientMG.getClientMG().getClientThd().sendMsg(sMsg);
   //將消息內(nèi)容顯示到聊天記錄中
   //[發(fā)送者]
   //消息內(nèi)容
   //清空發(fā)送消息框
   ClientMG.getClientMG().setLogTxt("[我]:");
   ClientMG.getClientMG().setLogTxt(MSGinfo);
   
   textSend.setText("");
  }
 }
 private class BtnCloseActionListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
   //向服務(wù)器發(fā)送線下信息,OFFLINE|username
   String SenderName=ClientMG.getClientMG().getClientThd().getName();
   String str="OFFLINE|"+SenderName;
   ClientMG.getClientMG().getClientThd().sendMsg(str);
   
   //清空在線用戶列表
   ClientMG.getClientMG().clearItem();
   //消息記錄中顯示斷開連接
   ClientMG.getClientMG().setLogTxt("已經(jīng)斷開連接");
  }
 }
 
 
}

ClientMG代碼:

package GUISocket.chat.Client;
 
import java.net.Socket; 
 
public class ClientMG {
 private static final ClientMG clientmg=new ClientMG();
 private ClientMG() {}
 public static ClientMG getClientMG() {
  return clientmg;
 }
 
 private ClientForm clientWin;
 public void setClientForm(ClientForm c) {
  clientWin=c;
 }
 
 public void setLogTxt(String str) {
  clientWin.textLog.append(str+"\r\n");  
 }
 
 public void addItem(String user) {
  
  clientWin.itemUsers.addElement(user);
 }
 
 public void addItems(String[] users) {
  for(int i=0;i<users.length;i++) {
   clientWin.itemUsers.addElement(users[i]);
  }
 
 }
 //所有用戶列表清空
 public void clearItem() {
  clientWin.itemUsers.clear();
 }
 //刪除一個(gè)用戶
 public void removeItem(String str) {
  clientWin.itemUsers.removeElement(str);
 }
 
 SocketThread sthd;
 public boolean Connect(String IP,int port,String user) {
  Socket socket=null;
  try {
   socket=new Socket(IP,port);
   sthd=new SocketThread(socket, user);
   sthd.start();
   return true;
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
   return false;
  }
 }
 
 public SocketThread getClientThd() {
  return sthd;
 }
}

SocketThread代碼:

package GUISocket.chat.Client;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
 
public class SocketThread extends Thread{
 BufferedReader br=null;
 PrintWriter pw=null;
 Socket socket=null;
 
 public SocketThread(Socket socket,String user){
  super(user);//登錄時(shí)用的用戶名
  this.socket=socket;
 }
 
 public void run() {
  try {
   br=new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
   pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF-8")));
   String sLOGIN="LOGIN|"+this.getName();
   sendMsg(sLOGIN);
   
   String str="";
   while((str=br.readLine())!=null) {
    String[] commands=str.split("\\|");
    if(commands[0].equals("USERLISTS")) {//USERLISTS|user1_user2_user3
     String[] sUsers=commands[1].split("\\_");
     ClientMG.getClientMG().addItems(sUsers);
    }
    else if(commands[0].equals("ADD")) {//ADD|UserName
     String sNewUser=commands[1];
     ClientMG.getClientMG().addItem(sNewUser);
    }
    else if(commands[0].equals("MSG")) {//格式 MSG|SenderName|MSGinfo
     String SenderName=commands[1];
     String MSGinfo=commands[2];
     //將消息內(nèi)容顯示到聊天記錄中
     //[發(fā)送者]
     //消息內(nèi)容
     ClientMG.getClientMG().setLogTxt("["+SenderName+"]");
     ClientMG.getClientMG().setLogTxt(MSGinfo);
    }
    else if(commands[0].equals("DEL")) {
     //3.處理下線用戶信息,DEL|username
     //刪除用戶列表中的username
     String sUser=commands[1];
     ClientMG.getClientMG().removeItem(sUser);
     ClientMG.getClientMG().setLogTxt(sUser+"下線了。");
    }
    else if(commands[0].equals("CLOSE")) { //CLOSE
     //1.處理CLOSE命令,界面顯示服務(wù)器關(guān)閉
     //2。清空在線用戶列表
     ClientMG.getClientMG().clearItem();
     ClientMG.getClientMG().setLogTxt("服務(wù)器關(guān)閉");
     //3.關(guān)閉ClientChat
     break;
    }
    
    //ClientMG.getClientMG().setLogTxt(str);
    
   }
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }finally {
   try {
    if(pw!=null)
     pw.close();
    if(br!=null)
     br.close();
    if(socket!=null)
     socket.close();
   } catch (Exception e2) {
    // TODO: handle exception
   }
  }
 }
 public void sendMsg(String str) {
  pw.println(str);
  pw.flush();
 }
}

ServerForm代碼:

package GUISocket.chat.Server;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.EventQueue;
 
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;
 
 
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
 
public class ServerForm extends JFrame {
 /**
  * 
  */
 
 private JPanel contentPane;
 public JTextArea textLog;
 private JTextField textPort;
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     ServerForm frame = new ServerForm();
     frame.setVisible(true);
     ServerMG.getServerMG().setServerForm(frame);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }
 
 public ServerForm() {
  setTitle("多人聊天服務(wù)器");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setBounds(100, 100, 510, 566);
  contentPane = new JPanel();
  contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  setContentPane(contentPane);
  contentPane.setLayout(null);
  
  JLabel lblNewLabel = new JLabel("配置信息");
  lblNewLabel.setBounds(20, 10, 54, 15);
  contentPane.add(lblNewLabel);
  
  JLabel label = new JLabel("端口:");
  label.setBounds(30, 34, 39, 15);
  contentPane.add(label);
  
  textPort = new JTextField();
  textPort.setText("8900");
  textPort.setBounds(65, 31, 66, 21);
  contentPane.add(textPort);
  textPort.setColumns(10);
  
  JButton btnStart = new JButton("開啟服務(wù)");
  btnStart.addActionListener(new BtnStartActionListener());
  btnStart.setBounds(180, 30, 93, 23);
  contentPane.add(btnStart);
  
  JButton btnClose = new JButton("關(guān)閉服務(wù)");
  btnClose.addActionListener(new BtnCloseActionListener());
  btnClose.setBounds(325, 30, 93, 23);
  contentPane.add(btnClose);
  
  JPanel panel = new JPanel();
  panel.setBounds(10, 10, 474, 54);
  contentPane.add(panel);
  panel.setLayout(null);
  
  JLabel label_1 = new JLabel("消息記錄");
  label_1.setBounds(10, 94, 54, 15);
  contentPane.add(label_1);
  
  JPanel panel_1 = new JPanel();
  panel_1.setBounds(0, 81, 474, 436);
  contentPane.add(panel_1);
  panel_1.setLayout(null);
  
  JScrollPane scrollPane = new JScrollPane();
  scrollPane.setBounds(10, 41, 464, 368);
  panel_1.add(scrollPane);
  scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
  
  textLog = new JTextArea();
  textLog.setLineWrap(true);
  textLog.setWrapStyleWord(true);
  scrollPane.setViewportView(textLog);
  
  
  
 }
 private class BtnCloseActionListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
   
  }
 }
 private class BtnStartActionListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
   //開啟服務(wù)
   
   
   int port=Integer.parseInt(textPort.getText().trim());
   
   if(ServerMG.getServerMG().CreateServer(port)) {
 
    ServerMG.getServerMG().setLogTxt("服務(wù)器開啟...");
   }
   else {
    ServerMG.getServerMG().setLogTxt("服務(wù)器開啟失敗...");
   }
  }
 }
}

ServerListener代碼:

package GUISocket.chat.Server;
 
import java.net.ServerSocket;
import java.net.Socket;
 
public class ServerListener extends Thread{
 Socket socket=null;
 ServerSocket server=null;
 public ServerListener(ServerSocket server) {
  this.server=server;
 }
 public void run() {
  try {
   while(true) {
    socket=server.accept();
    ServerMG.getServerMG().setLogTxt("客戶端: "+socket);  
    new SocketThread(socket).start();
   }
   
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }
 }
 
}

ServerMG代碼:

package GUISocket.chat.Server;
 
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
 
import javax.swing.JTextArea;
 
public class ServerMG {
 private static final ServerMG servermg=new ServerMG();
 private ServerMG() {}
 public static ServerMG getServerMG() {
  return servermg;
  
 }
 
 //主界面的操作
 private ServerForm serverWin;
 
 //將窗體對象注冊到管理類當(dāng)中
 public void setServerForm(ServerForm s) {
  serverWin=s;
 }
 //設(shè)置主界面
 public void setLogTxt(String str) {
  serverWin.textLog.append(str+"\r\n");
 }
 
 private ServerSocket server;
 public boolean CreateServer(int port) {
  try {   
   server=new ServerSocket(port);
   new ServerListener(server).start();
   return true;
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
   return false;
  }
 }
 
 public void CloseServer() {
  try {
   
   //1.向所有在線用戶發(fā)送關(guān)閉服務(wù)器的信息,CLOSE
   sendClosetoAll();
   //2.遍歷Arraylist將其中的每一個(gè)ServerChat關(guān)閉
   closeAllThread();
   //3.ArrayList要清空
   clearList();
   //4.關(guān)閉ServerListener
   //5.關(guān)閉ServerSocket
   server.close();
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }
 }
 
 //ArrayList操作
 ArrayList<SocketThread> a1OnlineList=new ArrayList<>();//存放所有和
 public synchronized void addList(SocketThread sc) {
  //限制重名
  a1OnlineList.add(sc);
 }
 
 public void clearList() {
  a1OnlineList.clear();
 }
 
 public synchronized void removeList(SocketThread sc) {
  for(int i=0;i<a1OnlineList.size();i++) {
   SocketThread s=a1OnlineList.get(i);
   if(s.equals(sc)) {
    a1OnlineList.remove(sc);
    break;
   }
  }
 }
 
 //信息的管理
 public void getOnlineNames(SocketThread sc) {
  //非第1次登錄時(shí),得到所有的在線用戶
  if(a1OnlineList.size()>0) {
   String sUsers="";//給客戶端,USERLISTS|user1_user2_user3
   for(int i=0;i<a1OnlineList.size();i++) {
    SocketThread s=a1OnlineList.get(i);
    sUsers+=s.getName()+"_";
   }
   sc.sendMsg("USERLISTS|"+sUsers);
  }
  
 }
 public void sendNewUsertoAll(SocketThread sc) {
  for(int i=0;i<a1OnlineList.size();i++) {
   SocketThread s=a1OnlineList.get(i);
   s.sendMsg("ADD|"+sc.getName());
  }
 }
 //通過Mame用戶名查找目標(biāo)
 public SocketThread getSocketThreadByName(String sName) {
  for(int i=0;i<a1OnlineList.size();i++) {
   SocketThread s=a1OnlineList.get(i);
   if(s.getName().equals(sName)) {
    return s;
   }
  }
  return null;
 }
 
 //發(fā)送給所有人,但是要排除自身
 public void sendMsgtoAll(String sMsg,SocketThread sc) {
  for(int i=0;i<a1OnlineList.size();i++) {
   SocketThread s=a1OnlineList.get(i);
   if(!s.equals(sc)) {
    s.sendMsg(sMsg);
   }
  }
 }
 
 public void sendOfflieUsertoAll(SocketThread sc) {
  //向所有的其他在線用戶發(fā)送用戶下線信息,DE|username
  for(int i=0;i<a1OnlineList.size();i++) {
   SocketThread s=a1OnlineList.get(i);
   if(!s.equals(sc)) {
    s.sendMsg("DEL|"+sc.getName());
   }
  }
 }
 
 public void sendClosetoAll() {
  for(int i=0;i<a1OnlineList.size();i++) {
   SocketThread s=a1OnlineList.get(i);
   s.sendMsg("CLOSE");
  }
 }
 
 public void closeAllThread() {
  for(int i=0;i<a1OnlineList.size();i++) {
   SocketThread s=a1OnlineList.get(i);
   s.closeChat();
  }
 }
 
 
}

SocketThread代碼:

package GUISocket.chat.Server;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
 
public class SocketThread extends Thread{
 BufferedReader br=null;
 PrintWriter pw=null;
 Socket socket=null;
 public SocketThread(Socket socket) {
  this.socket = socket;
 }
 
 public void run() {
  try {
   br=new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
   pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF-8")));
   String str="";
   while((str=br.readLine())!=null) {//循環(huán)響應(yīng)客戶的發(fā)送信息//接受客戶端發(fā)過來的信息
    String [] commands=str.split("\\|");
    
    if(commands[0].equals("LOGIN")) {//解析登錄請求,格式,LOGIN|UserName
     String sUSER=commands[1];
     this.setName(sUSER);//將用戶名信息放入Threadname中
     //1.得到所有在線用戶信息名稱,發(fā)回客戶端:USERLISTS|user1_user2_user3
     ServerMG.getServerMG().getOnlineNames(this);
     //2.將當(dāng)前登錄用戶的信息(用戶名),發(fā)送給已經(jīng)在線的其他用戶,ADD|userName
     
     ServerMG.getServerMG().sendNewUsertoAll(this);
     //3.將當(dāng)前登錄的Socket信息放入ArrayList中
     ServerMG.getServerMG().addList(this);
     
    }
    else if(commands[0].equals("MSG")) {//格式:MSG|SenderName|RecName|MSGoinfo
     String SenderName=commands[1];
     String RecName=commands[2];
     String MSGinfo=commands[3];
     //群聊
     if(RecName.equals("ALL")) {
      String sMsg="MSG!"+SenderName+"|"+MSGinfo;//格式:MSG|SenderName|MSGinfo
      ServerMG.getServerMG().sendMsgtoAll(sMsg,this);
      ServerMG.getServerMG().setLogTxt(SenderName+"發(fā)送信息["+MSGinfo+"]到所有人。");
     }
     //私聊
      else {
       //通過RecName用戶名查找,找到目標(biāo)SocketThread
       SocketThread sc=ServerMG.getServerMG().getSocketThreadByName(RecName);
       if(sc!=null) {
        //目標(biāo)對象發(fā)送信息,MSG|SenderName|MSGinfo
        String sMsg="MSG!"+SenderName+"|"+MSGinfo;
        sc.sendMsg(sMsg);
        
        //寫入信息日志
        ServerMG.getServerMG().setLogTxt(SenderName+"發(fā)送信息["+MSGinfo+"]到"+RecName);
      }
      
     }
    }
    
    else if(commands[0].equals("OFFLINE")) {
     //1.創(chuàng)建OFFLINE
     String sUser=commands[1];//獲取下線的用戶名
     //2.向所有的其他在線用戶發(fā)送用戶下線信息,DEL|username
     ServerMG.getServerMG().sendOfflieUsertoAll(this);
     //3.清除ArrayList中的當(dāng)前用戶信息()socketChat
     ServerMG.getServerMG().removeList(this);
     
     //用戶下線需后要退出監(jiān)聽用的while循環(huán)
     ServerMG.getServerMG().setLogTxt(sUser+"下線了");
     break;
    }
    
   }
  } catch (Exception e) {
   e.printStackTrace();
  }finally {
   try {
    if(pw!=null)
     pw.close();
    if(br!=null)
     br.close();
    if(socket!=null)
     socket.close();
   } catch (Exception e2) {
    // TODO: handle exception
   }
  }
 }
 public void closeChat() {
  try {
   if(pw!=null)
    pw.close();
   if(br!=null)
    br.close();
   if(socket!=null)
    socket.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 public void sendMsg(String str) {
  pw.println(str);
  pw.flush();
 }
}

運(yùn)行結(jié)果如下:

缺點(diǎn):只能發(fā)單行信息,如發(fā)多行,需用到base4.

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

相關(guān)文章

  • Java實(shí)現(xiàn)基于NIO的多線程Web服務(wù)器實(shí)例

    Java實(shí)現(xiàn)基于NIO的多線程Web服務(wù)器實(shí)例

    在本篇文章里小編給大家整理的是關(guān)于Java實(shí)現(xiàn)基于NIO的多線程Web服務(wù)器實(shí)例內(nèi)容,需要的朋友們可以學(xué)習(xí)下。
    2020-03-03
  • Spring事件監(jiān)聽器之@EventListener原理分析

    Spring事件監(jiān)聽器之@EventListener原理分析

    這篇文章主要介紹了Spring事件監(jiān)聽器之@EventListener原理分析,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 總結(jié)Junit4,Junit5,Jupiter之間的聯(lián)系

    總結(jié)Junit4,Junit5,Jupiter之間的聯(lián)系

    Jupiter和Junit5之間有什么聯(lián)系?Jupiter提供了哪些新的測試方法?如何用IDEA和Jupiter生成可讀性更好的測試報(bào)告?文中有非常詳細(xì)的說明,需要的朋友可以參考下
    2021-06-06
  • 關(guān)于Java中修飾符的總結(jié)(fina除外)

    關(guān)于Java中修飾符的總結(jié)(fina除外)

    下面小編就為大家?guī)硪黄P(guān)于Java中修飾符的總結(jié)(fina除外)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-09-09
  • java_時(shí)間戳與Date_相互轉(zhuǎn)化的實(shí)現(xiàn)代碼

    java_時(shí)間戳與Date_相互轉(zhuǎn)化的實(shí)現(xiàn)代碼

    本篇文章是對java_時(shí)間戳與Date_相互轉(zhuǎn)化的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下如下。
    2016-11-11
  • Java全面細(xì)致講解類與對象

    Java全面細(xì)致講解類與對象

    類和對象是兩種以計(jì)算機(jī)為載體的計(jì)算機(jī)語言的合稱。對象是對客觀事物的抽象,類是對對象的抽象。類是一種抽象的數(shù)據(jù)類型;變量就是可以變化的量,存儲(chǔ)在內(nèi)存中—個(gè)可以擁有在某個(gè)范圍內(nèi)的可變存儲(chǔ)區(qū)域
    2022-05-05
  • Spring Boot中使用LDAP來統(tǒng)一管理用戶信息的示例

    Spring Boot中使用LDAP來統(tǒng)一管理用戶信息的示例

    本篇文章主要介紹了Spring Boot中使用LDAP來統(tǒng)一管理用戶信息的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Java中如何使用?byte?數(shù)組作為?Map?的?key

    Java中如何使用?byte?數(shù)組作為?Map?的?key

    本文將討論在使用HashMap時(shí),當(dāng)byte數(shù)組作為key時(shí)所遇到的問題及其解決方案,介紹使用String和List這兩種數(shù)據(jù)結(jié)構(gòu)作為臨時(shí)解決方案的方法,感興趣的朋友跟隨小編一起看看吧
    2023-06-06
  • 詳解SpringCloud服務(wù)認(rèn)證(JWT)

    詳解SpringCloud服務(wù)認(rèn)證(JWT)

    本篇文章主要介紹了SpringCloud服務(wù)認(rèn)證(JWT),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • Spring?Boot2.6.0新特性之默認(rèn)禁止循環(huán)引用

    Spring?Boot2.6.0新特性之默認(rèn)禁止循環(huán)引用

    Spring?Boot2.6.0為我們帶來很多好用的新特性/改進(jìn),這篇文章主要給大家介紹了關(guān)于Spring?Boot2.6.0新特性之默認(rèn)禁止循環(huán)引用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02

最新評論