Myqq代码

第一次写博客,希望可以结识很多朋友。奉上一个qq程序代码(代码非原创)

public class TestDemo {


/**
* @param args
*/
public static void main(String[] args) {
int []nums={10,20,40,80};
for(int i=0;i<nums.length;i++){
System.out.println(nums[i]);
}
System.out.println("--------------");
//forech循环  : in
for(int i:nums){
System.out.println(i);
}


}


}


package com.sxt.qq.dao;
import java.util.ArrayList;


import com.sxt.qq.entity.QQUser;
import com.sxt.qq.utils.DB;




/**
 * QQ用户的操作类
 * qq用户的登录
 * qq用户信息的初始化
 * @author wulin
 *
 */
public class QQUserDao {

/**
* 用户登录
* @param qq :qq号
* @param pwd:密码
* @return:返回相应的用户信息
*/
public static QQUser  login(String qq,String pwd){
//1.获取所有的qq用户
ArrayList<QQUser> users =DB.QQ_USERS;
//2.遍历每个账号,对比其号码和密码
QQUser user =null;
for(int i=0;i<users.size();i++){
if(users.get(i).getQqNumber().equals(qq) && users.get(i).getPwd().equals(pwd)){
user= users.get(i);
}
 
}
return user;


}



}


package com.sxt.qq.entity;


import java.util.ArrayList;
import java.util.HashMap;




/**
 * 好友类:存储一个 qq号对应的所有组的好友信息
 * 
 * @author wulin
 *
 */
public class Friend {

//String:组名: ArrayList<QQUser>:当前组下的所有好友的集合
private HashMap<String,ArrayList<QQUser>> friends=new HashMap<String,ArrayList<QQUser>>();


public Friend(){


}
 
 
public HashMap<String, ArrayList<QQUser>> getFriends() {
return friends;
}




/**
 * 添加好友
 */
public void add(String groupName,QQUser friend){
 
//同一个组的第二次添加好友
if(friends.containsKey(groupName)){
 
ArrayList<QQUser> users = friends.get(groupName);
users.add(friend);
 
}else{
//构建一个好友的集合,存放同一个组的好友
         ArrayList<QQUser>  friendList =new ArrayList<QQUser>();
         friendList.add(friend);
         //将组名和好友组的所有成员加入到friends
friends.put(groupName,friendList);
}
 
}
 
}



package com.sxt.qq.entity;


public class QQUser {

private  String qqNumber;
private String pwd;
private String nickName;
private String sign;
private String photo;
public QQUser(){

}
public QQUser(String qqNum,String pwd,String nickName,String sign,String photo){
this.qqNumber = qqNum;
this.pwd =pwd;
this.nickName = nickName;
this.sign = sign;
this.photo = photo;

}


public String getQqNumber() {
return qqNumber;
}
public void setQqNumber(String qqNumber) {
this.qqNumber = qqNumber;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}






}


package com.sxt.qq.thread;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;


import com.sxt.qq.ui.ChatFrame;
import com.sxt.qq.ui.MainFrame;




public class ReceiveThread extends Thread {

private InputStream is;
private boolean  isRun=true;
public ReceiveThread(InputStream is){
this.is = is;
}
public void stopReceive(){
isRun=false;
}
public void run(){
while(isRun){
//   7.接收服务器的信息

try {
String msg = "";
   byte[] data=new byte[1024];
   int len;
len = is.read(data);
msg=new String(data,0,len);
//得到发送方的qq号,判断发送方是否有窗口被打开,如果打开,就直接将信息放入显示框,如果没有打开,就打开,再放信息
String fromQQ= msg.substring(0,msg.indexOf("说"));
//判断发送方是否有窗口被打开
if(MainFrame.allOpenChats.containsKey(fromQQ)==false){
ChatFrame  cf=new ChatFrame(fromQQ);
MainFrame.allOpenChats.put(fromQQ, cf);
cf.setVisible(true);
}

String oldMsg= MainFrame.allOpenChats.get(fromQQ).txtShow.getText();
String newMsg=oldMsg+"\n"+msg;
MainFrame.allOpenChats.get(fromQQ).txtShow.setText(newMsg);


Thread.sleep(500);

} catch (IOException e) {

e.printStackTrace();
break;
} catch (InterruptedException e) {

e.printStackTrace();
break;
}




}
  
}


}


package com.sxt.qq.ui;


import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;


import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.Socket;




public class ChatFrame extends JFrame implements WindowListener{


private JPanel contentPane;


   private String qqNumber="";
public JTextArea txtSend = new JTextArea();
public JTextArea txtShow = new JTextArea();

Socket client =MainFrame.CLIENT;
/**
* Create the frame.
*/
public ChatFrame(String qqNumber1) {
//把当前聊天的qq号保存在自己的变量中,为其他的方法服务
this.qqNumber = qqNumber1;

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 694, 504);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
this.setResizable(false);
contentPane.setLayout(null);
this.setTitle("与"+qqNumber+" 聊天窗口");


txtShow.setBounds(10, 10, 617, 297);
contentPane.add(txtShow);

//final JTextArea txtSend = new JTextArea();
txtSend.setBounds(10, 346, 645, 81);
contentPane.add(txtSend);

JButton btnSend = new JButton("发送");
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
 //发送信息
     //1.获取用户输入的信息 (匿名内部类调用外部的局部变量,变量必须为final)
 String sendMsg = txtSend.getText();

 //1.2 使用socket将信息发送出去 
 try {
client.getOutputStream().write((LoginFrame.USER.getQqNumber()+":"+qqNumber+":"+sendMsg).getBytes());
   client.getOutputStream().flush();
 } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 
 //2.累加到显示框
 txtShow.setText(txtShow.getText()+"\n"+sendMsg);
 

    //3.清空发送框
txtSend.setText("");

}
});
btnSend.setBounds(562, 433, 93, 23);
contentPane.add(btnSend);

JButton btnShark = new JButton("闪");
btnShark.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
//btnShark.setIcon(new ImageIcon("imgs/1.jpg"));
btnShark.setBounds(20, 313, 56, 23);
contentPane.add(btnShark);
 
//添加窗口的事件监听器
this.addWindowListener(this);
}






@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub

}






@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub

}






/**
* 当聊天窗口关闭时,会调用的方法
*/
@Override
public void windowClosing(WindowEvent e) {
//调用 MainFrame中的集合,删除qqNumber对应的信息
System.out.println("before:"+MainFrame.allOpenChats.keySet());
MainFrame.allOpenChats.remove(qqNumber);
//System.out.println("删除"+qqNumber+"...");

//System.out.println("after:"+MainFrame.allOpenChats.keySet());

}






@Override
public void windowDeactivated(WindowEvent e) {


}






@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub

}






@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub

}






@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub

}
}


package com.sxt.qq.ui;


import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JLabel;


import com.sxt.qq.entity.QQUser;
import com.sxt.qq.dao.QQUserDao;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;




public class LoginFrame extends JFrame {




//姓名的文本框
private JTextField txtName;
//密码框
private JPasswordField txtPwd;
//当前登录的用户
public static QQUser  USER=null;


/**
* Launch the application.
*/
public static void main(String[] args) {

//构建一个登录窗体的对象
LoginFrame frame = new LoginFrame();
//设置窗体可见
frame.setVisible(true);

}


/**
* Create the frame.
*/
public LoginFrame() {
//设置窗体的属性
//1.是否改变窗体的大小
this.setResizable(false);
//2.设置窗体的标题
setTitle("qq登录");
//3.设置关闭时需要的操作:*DISPOSE_ON_CLOSE:关闭自己  EXIT_ON_CLOSE:退出应用程序
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//设置窗体出现的位置及大小
//setBounds(100, 100, 450, 300);
//4.设置大小
this.setSize(400,300);
//5.设置窗体在屏幕的中间显示
this.setLocationRelativeTo(null);

       //6.设置窗体的布局
getContentPane().setLayout(null);

//设置组件
txtName = new JTextField();
txtName.setBounds(144, 56, 193, 21);
//将文本框加入到窗体
getContentPane().add(txtName);
txtName.setColumns(10);

txtPwd = new JPasswordField();
txtPwd.setBounds(144, 110, 193, 21);
getContentPane().add(txtPwd);

JButton btnLogin = new JButton("登录");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//点击登录需要调用的代码

//JOptionPane.showMessageDialog(null, "haha");

//1.获取用户输入的用户名和密码  trim:去掉两端的空格
String name =txtName.getText().trim();
//valueOf:将char[] ->String
String pwd = String.valueOf(txtPwd.getPassword()).trim();
//JOptionPane.showMessageDialog(null, "name:"+name+",pwd:"+pwd);
//2.比较  357728949 123
//if("357728949".equals(name) && "123".equals(pwd)){
USER=QQUserDao.login(name, pwd);
if(USER!=null){
//2.1 比较成功
JOptionPane.showMessageDialog(null, name+"用户,登录成功!");
//跳转到主界面
MainFrame  mainFrame = new MainFrame();
mainFrame.setVisible(true);
LoginFrame.this.setVisible(false);

}else{
//2.2 失败,提示用户登录失败,清空输入的信息
JOptionPane.showMessageDialog(null, "用户名或密码不存在!");
txtName.setText("");
txtPwd.setText("");
}

}
});
btnLogin.setBounds(146, 165, 93, 23);
getContentPane().add(btnLogin);

JLabel lblPhoto = new JLabel("New label");
lblPhoto.setBounds(36, 59, 76, 82);
getContentPane().add(lblPhoto);

//缩放图片
ImageIcon icon= new ImageIcon("imgs/1.jpg");
Image  smallImg= icon.getImage().getScaledInstance(76, 82, Image.SCALE_SMOOTH);
//设置图片
lblPhoto.setIcon(new ImageIcon(smallImg));
}
}


package com.sxt.qq.ui;


import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultTreeSelectionModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JScrollPane;
import javax.swing.JTree;


import com.sxt.qq.entity.Friend;
import com.sxt.qq.entity.QQUser;
import com.sxt.qq.thread.ReceiveThread;
import com.sxt.qq.utils.DB;






public class MainFrame extends JFrame implements TreeSelectionListener,WindowListener{


private JPanel contentPane;
JTree treeFriends = new JTree();
//使用map来保存打开的窗体信息(qqNum,对应的窗口)
public static HashMap<String,ChatFrame>  allOpenChats=new HashMap<String, ChatFrame>();

public static Socket  CLIENT=null;
private ReceiveThread  rt=null;


/**
* Launch the application.
*/




/**
* Create the frame.
*/
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 600);
this.setResizable(false);
//显示在屏幕的右边
//1.获取屏幕的宽度
int screenWidth= Toolkit.getDefaultToolkit().getScreenSize().width;
//2.得到窗体需要显示的x坐标
int x = screenWidth -this.getWidth();
//3.设置窗体的坐标
this.setLocation(x, 40);
this.setResizable(false);
this.setTitle("主界面");
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JLabel lblPhoto = new JLabel("");
lblPhoto.setBounds(10, 23, 54, 52);
contentPane.add(lblPhoto);

JLabel lblNickName = new JLabel("New label");
lblNickName.setBounds(98, 23, 100, 15);
contentPane.add(lblNickName);

JLabel lblSign = new JLabel("New label");
lblSign.setBounds(98, 48, 200, 15);
contentPane.add(lblSign);

JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBounds(10, 85, 274, 450);
contentPane.add(tabbedPane);

JScrollPane scrollPane = new JScrollPane();
tabbedPane.addTab("私聊", null, scrollPane, null);


scrollPane.setViewportView(treeFriends);


//设置图片
//***
Image smallImg =new ImageIcon("1.jpg").getImage().getScaledInstance(54, 52, Image.SCALE_SMOOTH);
lblPhoto.setIcon(new ImageIcon(smallImg));

lblNickName.setText(LoginFrame.USER.getNickName());
lblSign.setText(LoginFrame.USER.getSign());

//设置树的信息
//1.构建树的节点
DefaultMutableTreeNode root =new DefaultMutableTreeNode("所有的组");

// DefaultMutableTreeNode friends =new DefaultMutableTreeNode("我的好友");
// DefaultMutableTreeNode mates =new DefaultMutableTreeNode("我的同学");
//
// DefaultMutableTreeNode friend1 =new DefaultMutableTreeNode("914024108");
// DefaultMutableTreeNode friend2 =new DefaultMutableTreeNode("441711443");
//
// DefaultMutableTreeNode mate1 =new DefaultMutableTreeNode("864722047");
// DefaultMutableTreeNode mate2 =new DefaultMutableTreeNode("229744285");
//
// //节点的添加
// root.add(friends);
// root.add(mates);
// friends.add(friend1);
// friends.add(friend2);
// mates.add(mate1);
// mates.add(mate2);

//1.获取当前登录的用户的好友信息
Friend friend =DB.QQ_FRIENDS.get(LoginFrame.USER.getQqNumber());

//2.遍历好友的信息,获取其对象,显示qq号
HashMap<String,ArrayList<QQUser>>  friends =friend.getFriends();

//3.逐步添加
//3.1获取所有的组名
for(String groupName:friends.keySet()){
DefaultMutableTreeNode group =new DefaultMutableTreeNode(groupName);
root.add(group);

//3.2获取相应的组下面的好友信息
ArrayList<QQUser> fs =friends.get(groupName);
// for(int i=0;i<fs.size();i++){
// //构建好友的节点
// DefaultMutableTreeNode friendNode =new DefaultMutableTreeNode(fs.get(i).getQqNumber());
// group.add(friendNode);
// }

for(QQUser u:fs){
DefaultMutableTreeNode friendNode =new DefaultMutableTreeNode(u.getQqNumber());
group.add(friendNode);
}

}



//节点添加到树

DefaultTreeModel  dtm =new DefaultTreeModel(root);
treeFriends.setModel(dtm);

//设置根节点不可见
treeFriends.setRootVisible(false);
//当用户点击好友的节点时,就需要跳出聊天窗口,给树加事件处理
treeFriends.addTreeSelectionListener(this);

//构建一个用于通信的客户端对象  
try {
CLIENT=new Socket("127.0.0.1",1234);
 //先发自己的名字
CLIENT.getOutputStream().write(("name:"+LoginFrame.USER.getQqNumber()).getBytes());
CLIENT.getOutputStream().flush();
 
//启动接收的线程
 rt = new ReceiveThread(CLIENT.getInputStream());
rt.start();
 
} catch (UnknownHostException e) {
JOptionPane.showMessageDialog(null, "网络异常,程序结束");
System.exit(0);
} catch (IOException e) {
System.exit(0);
}


}
public static void main(String[] args) {
MainFrame  mainFrame =new MainFrame();
mainFrame.setVisible(true);
}
@Override
public void valueChanged(TreeSelectionEvent e) {
//当用户点击好友的节点时,就需要跳出聊天窗口处理代码
//1.获取用户选中的节点
DefaultMutableTreeNode  selectNode=(DefaultMutableTreeNode)e.getPath().getLastPathComponent();
//2.判断是否为叶节点
   if(selectNode.isLeaf()){
    //是:做处理
 // JOptionPane.showMessageDialog(null, selectNode.toString());
    String qqNum = selectNode.toString();
    //如果集合的key值包含qq号,那么说明这个用户的聊天界面已打开
    if(allOpenChats.containsKey(qqNum)){
    JOptionPane.showMessageDialog(null, qqNum+"的聊天窗口已打开");
   
   
    }else{
   
           ChatFrame  cf=new ChatFrame(qqNum);
       cf.setVisible(true);
       //将打开的qq号和对应的窗体加入到集合
       allOpenChats.put(qqNum, cf);
       //System.out.println("current:"+MainFrame.allOpenChats.keySet());
    }
}

}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub

}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub

}
@Override
public void windowClosing(WindowEvent e) {
//主窗体关闭,通知服务器退出
try {
CLIENT.getOutputStream().write("88".getBytes());
//终止线程
rt.stopReceive();
//关闭套接字
CLIENT.close();

} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}


}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub

}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub

}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub

}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub


package com.sxt.qq.utils;
import java.util.ArrayList;
import java.util.HashMap;


import com.sxt.qq.entity.Friend;
import com.sxt.qq.entity.QQUser;




/**
 * 模拟数据库为应用提供数据
 * 数据相对还是固定的
 * @author wulin
 *
 */
public class DB {
//构建一个动态数组用于存储所有的qq用户
public  static ArrayList<QQUser>   QQ_USERS=null;
//构建一个map集合用于存放用户的好友信息
public  static HashMap<String,Friend>  QQ_FRIENDS= null;
static{
QQ_USERS=new ArrayList<QQUser>();

//多多
  QQ_USERS.add(new QQUser("761334318","123","wdp","123.....","imgs/1.jpg"));
  //李彪
  QQ_USERS.add(new QQUser("441711443","123","BB","bba.....","imgs/1.jpg"));
  //康康
  QQ_USERS.add(new QQUser("864722047","1234","KK","╮(╯▽╰)╭","imgs/4.png"));
  //月月
  QQ_USERS.add(new QQUser("1091559488","12345","LY","O(∩_∩)O哈哈~","imgs/5.png"));
  //小杰
  QQ_USERS.add(new QQUser("814525295","777","AI","(*^__^*) 嘻嘻……","imgs/3.png"));

  //李彪的好友组
QQ_FRIENDS =new HashMap<String,Friend>();
 Friend  friendBB =new Friend();
 friendBB.add("我的同学", QQ_USERS.get(0));
 friendBB.add("我的同学", QQ_USERS.get(2));  
 friendBB.add("我的好友", QQ_USERS.get(3));
 friendBB.add("我的好友", QQ_USERS.get(4));
 QQ_FRIENDS.put("441711443", friendBB);
 
 Friend  friendJ=new Friend();
 friendJ.add("我的老乡", QQ_USERS.get(0));
 friendJ.add("我的老乡", QQ_USERS.get(1));
 friendJ.add("我的老乡", QQ_USERS.get(2));
 friendJ.add("我的好基友", QQ_USERS.get(3));
 QQ_FRIENDS.put("814525295", friendJ);
 
 Friend  friendKK=new Friend();
 friendKK.add("我的同学", QQ_USERS.get(1));
 friendKK.add("我的同学", QQ_USERS.get(4));
 
 QQ_FRIENDS.put("864722047", friendKK);
 
 
 
 
 
  

}



}
}
}



import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;


/**
 * 服务器
 * @author wulin
 *
 */
public class Server {

  public static HashMap<String,Socket>  ALL_ONLINE_USER=new HashMap<String,Socket>();
  public static void main(String[] args) throws IOException {
  
// 服务器                     
//   1.构建服务器(ip,port)    
 ServerSocket  server=new ServerSocket(1234);
 //保存所有在线的客户
 ArrayList<Socket> allOnLines=new ArrayList<Socket>();
 System.out.println("服务器构建完毕...");
//   3.服务器开始监听客户端的请求
 while(true){
    Socket  client =server.accept();
    System.out.println(client.getRemoteSocketAddress()+"连接成功...");
    allOnLines.add(client);
    new ServerThread(client,allOnLines).start();
   
 }
  
      // server.close();


  }
}
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;




public class ServerThread extends Thread{
//client就是当前请求成功连接到服务器的客户端
private Socket  client;
 InputStream  is =null;
 OutputStream  os =null;
 ArrayList<Socket>  allOnLines=null;
 String name= null;
public ServerThread(Socket client,ArrayList<Socket> allOnlines) throws IOException{
this.client = client;
is =client.getInputStream();
os =client.getOutputStream();
this.allOnLines = allOnlines;
}


public void run(){
//5.接收客户端的信息 
      while(true){
 //5.2构建一个byte数组,用于接收输入流中的数据,客户端发过来的
 byte[]  data=new byte[1024];
 //5.3将输入流的数据保存到data数组中,共保存了len个byte
 int len=0;
try {

 len = is.read(data);
 //5.4byte[]用户读不懂,所以需要转成字符串 byte[] ->String
 if(len<=0){
 continue;
 }
  String msg=new String(data,0,len);
 System.out.println("接收的信息:"+msg);
 
 //先接收用户的名字
 if(msg.startsWith("name")){
 //接收我客户发来的姓名
  name=msg.split(":")[1];
 Server.ALL_ONLINE_USER.put(name, client);
 continue;
 }
 
 //再接收客户的聊天信息 "BB:Jie:hello" 谁发的:发给谁的:信息
// System.out.println("客户端说:"+msg);
 String []msgs=msg.split(":");
 String from = msgs[0];
 String to= msgs[1];
 String m=msgs[2];
 
 boolean isFind=false;
 for(String name:Server.ALL_ONLINE_USER.keySet()){
if(name.equals(to)){
Socket otherClient = Server.ALL_ONLINE_USER.get(name);
OutputStream  os =otherClient.getOutputStream();
os.write((from+"说:"+m).getBytes());
os.flush();
isFind=true;
}  
 
 }
 //说明to用户没有登录,数据转服务器的数据库,当离线用户登录时,推送给用户
 if(isFind==false){
 System.out.println("当前用户没有上线,信息发送失败....");
 
 }
 
 if(msg.equals("88")){
 //1.将自己从集合中移除
 allOnLines.remove(client);
 Server.ALL_ONLINE_USER.remove(name);
 break;
 }
 
 
 } catch (IOException e) {

//e.printStackTrace();
break;
}
 


}
      //   9.服务器的关闭
try {
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值