我现在在做一个java的通信项目   主要是scoket实现的   整个项目类似于QQ   我在局域网测试运行没有什么问题   但是一到外网去   那么就会出现一些问题了   主要的是  拿到外网上面去  那个软件上面的很多功能都不能实现了。比如在客户端登陆成功之后,显示到了软件主页面(如QQ主面板),这是我们就可以双击自己的好友进行聊天了,但是双击却没有反应,却在控制台报错了,提示为“ Connection refused: connect”。但是当我用在局域网的时候则不会出现这类问题,嚼劲脑袋都没搞清楚了,请高手指教,谢谢了啊
这是服务器:package server;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;import javax.swing.JOptionPane;import entity.Userinfo;import services.UserInfoServices;/**
 * 
 * @author 李佳
 * 
 */
public class Server {
/**
 * 服务器ServerSocket
 */ ServerSocket ss;
/**
 * 存储用户的输出流
 */
Map<String, PrintStream> mapps = new HashMap<String, PrintStream>();
/**
 * 存储用户的IP地址
 */
Map<String, String> mapip = new HashMap<String, String>(); /**
 * 无参构造器
 * 
 */
public Server() {
try {
ss = new ServerSocket(1001);
ServerAccept sa = new ServerAccept();
sa.start();
System.out.println("服务器已启动");
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
} /**
 * 服务器循环等待连接线程
 */
class ServerAccept extends Thread {
public void run() {
try {
while (true) {
Socket s;
s = ss.accept();
ServerRead sr = new ServerRead();
sr.srs = s;
sr.start();
}
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
} /**
 * 服务器对每个连接处理循环读线程
 * 
 * 
 */
class ServerRead extends Thread {
Socket srs;
String sadress;
BufferedReader br;
String username; public void run() {
try {
br = new BufferedReader(new InputStreamReader(srs
.getInputStream()));
PrintStream ps = new PrintStream(srs.getOutputStream());
sadress = srs.getInetAddress().toString().substring(1);
while (true) {
String codeline = br.readLine();
if (codeline != null) {
System.out.println(codeline + ";;;;;;;;;;");
if (codeline.startsWith("@")) {// 有人上线,并通知在线用户更新在线人员列表
if (readFromFile(codeline.split("@")[1],
codeline.split("@")[2]).equals("true")) {
if (!mapip.containsKey(codeline.split("@")[1])) {
System.out.println(codeline + "有人登陆了"); this.username = codeline.split("@")[1];
mapps.put(username, ps);
mapip.put(username, sadress); System.out.println(username); Set set = mapps.keySet();
Iterator it = set.iterator();
StringBuffer sb = new StringBuffer(",");
while (it.hasNext()) {
String a = (String) it.next();
sb.append(a + ",");
}
Iterator iit = set.iterator();
while (iit.hasNext()) {
String a = (String) iit.next();
PrintStream pss = (PrintStream) mapps
.get(a);
pss.println(sb.toString());
}
} else {
ps.println("@two");
}
} else {
ps.println("用户名或密码错误");
} } else if (codeline.startsWith("end=")) {// 有人下线,并通知在线用户更新在线人员列表
String leave = codeline.split("=")[1];
System.out.println(leave + "下线");
mapps.remove(leave);
mapip.remove(leave); Set set = mapps.keySet();
Iterator it = set.iterator();
StringBuffer sb = new StringBuffer(",");
while (it.hasNext()) {
String a = (String) it.next();
sb.append(a + ",");
}
Iterator iit = set.iterator();
while (iit.hasNext()) {
String a = (String) iit.next();
PrintStream pss = (PrintStream) mapps.get(a);
pss.println(sb.toString());
} } else if (codeline.startsWith("giveip=")) {// 要ip,并发回对应Ip
String nme = codeline.split("=")[1];
String tonme = codeline.split("=")[2];
PrintStream pss = mapps.get(nme);
pss.println("ip=" + tonme + "=" + mapip.get(tonme)); } else if (codeline.startsWith("=@")) {
String na = codeline.split("=@")[1];
String psw = codeline.split("=@")[2];
if (writeToFile(na, psw)) {
ps.println("=true");
} else {
ps.println("=false");
} } else if (codeline.contains("=states=")) {
String user = codeline.split("=")[0];
String states = codeline.split("=")[2];
if (states.equals("在线")) {
Set set = mapps.keySet();
Iterator it = set.iterator();
StringBuffer sb = new StringBuffer(",");
while (it.hasNext()) {
String a = (String) it.next();
sb.append(a + ","); }
Iterator iit = set.iterator();
while (iit.hasNext()) {
String a = (String) iit.next();
PrintStream pss = (PrintStream) mapps
.get(a);
pss.println(sb.toString());
} } else {
Set set = mapps.keySet();
Iterator it = set.iterator();
StringBuffer sb = new StringBuffer(",");
while (it.hasNext()) {
String a = (String) it.next();
if (!a.equals(user))
sb.append(a + ","); }
Iterator iit = set.iterator();
while (iit.hasNext()) {
String a = (String) iit.next();
PrintStream pss = (PrintStream) mapps
.get(a);
pss.println(sb.toString());
} }
} else {// 发送群消息
Set set = mapps.keySet();
Iterator iit = set.iterator();
while (iit.hasNext()) {
String a = (String) iit.next();
PrintStream pss = (PrintStream) mapps.get(a);
pss.println(codeline);
}
} } }
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
} /**
 * 匹配登录文件
 * 
 * @param name
 *            用户名
 * @param pass
 *            密码
 * @return 是否存在
 */
public String readFromFile(String name, String pass) {
try { System.out.println(name + "登陆名");
System.out.println(pass);
// FileReader fr=new FileReader("login.txt");
// BufferedReader br=new BufferedReader(fr);
// String str=br.readLine(); // 根据用户名查询数据库
UserInfoServices userInfoServices = new UserInfoServices();
Userinfo userInfo = userInfoServices.getUserInfo(name); while (userInfo != null) {
if (userInfo.getUserName().equals(name)
&& userInfo.getUserPwd().equals(pass)) {
return "true";
} else {
return "no";
}
// else if(str.split("=")[0].equals(name)){
// return "have";
// }
// str=br.readLine(); }
return "no";
} catch (Exception e) {
// TODO: handle exception
}
return "no";
} /**
 * 将新用户写入登录文件
 * 
 * @param name
 *            用户名
 * @param pass
 *            密码
 * @return 是否写入成功
 */
public boolean writeToFile(String name, String pass) {
UserInfoServices userInfoServices = new UserInfoServices();
if(name==null&&name.equals(""))
return false;
if(pass.length()<6)
return false;
Userinfo userInfo = new Userinfo();
userInfo.setUserName(name);
userInfo.setUserPwd(pass);
userInfoServices.addUser(userInfo);
System.out.println("插入成功");
return true;
// try {
// PrintStream ps = new PrintStream(new FileOutputStream("login.txt",
// true));
// if (readFromFile(name, pass).equals("no")) {
// ps.println(name + "=" + pass);
// return true;
// } else {
//// return false;
// }// } catch (FileNotFoundException e) {
// // TODO 自动生成 catch 块
// e.printStackTrace();
// }
} /**
 * 主函数
 * 
 * @param args
 */
public static void main(String[] args) {
// TODO 自动生成方法存根
Server ser = new Server();
}}

解决方案 »

  1.   

    这是客户端登陆:package client;import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.HashMap;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;/**
     * 
     * @author 李佳
     * 
     * 
     */
    public class Clientlogin { /**
     * 登录
     */
    JFrame jf = new JFrame("登陆");
    MyPanel mp = new MyPanel();
    JPanel jp1 = new JPanel();
    JPanel jp2 = new JPanel();
    JPanel jp3 = new JPanel();
    /**
     * 帐号标签
     */
    JLabel jl1 = new JLabel("账号:");
    /**
     * 密码标签
     */
    JLabel jl2 = new JLabel("密码:");
    /**
     * 登录界面帐号文本框
     */
    JTextField jtf1 = new JTextField(15);
    /**
     * 登录界面密码文本框
     */
    JTextField jtf2 = new JPasswordField(15);
    /**
     * 登录按钮
     */
    JButton jb1 = new JButton("登陆");
    /**
     * 登录界面重置按钮
     */
    JButton jb2 = new JButton("重置");
    /**
     * 注册标签(点击进入注册界面)
     */
    JLabel jl3 = new JLabel("注册新用户");
    /**
     * 找回密码标签
     */
    JLabel jl4 = new JLabel("找回密码");
    /**
     * 输出流
     */
    PrintStream ps;
    /**
     * 输入流
     */
    BufferedReader br; /**
     * 注册
     */
    JFrame zhuce = new JFrame("注册");
    JPanel jpz1 = new JPanel();
    JPanel jpz2 = new JPanel();
    JPanel jpz3 = new JPanel();
    JPanel jpz4 = new JPanel();
    /**
     * 注册帐号标签
     */
    JLabel jlz1 = new JLabel("用户账号:");
    /**
     * 注册密码标签
     */
    JLabel jlz2 = new JLabel("用户密码:");
    /**
     * 密码确认标签
     */
    JLabel jlz3 = new JLabel("确认密码:");
    /**
     * 注册界面帐号文本框
     */
    JTextField jtfz1 = new JTextField(15);
    /**
     * 注册界面密码文本框
     */
    JTextField jtfz2 = new JPasswordField(15);
    /**
     * 注册界面确认密码文本框
     */
    JTextField jtfz3 = new JPasswordField(15);
    /**
     * 注册按钮
     */
    JButton jbz1 = new JButton("注册");
    /**
     * 取消注册按钮
     */
    JButton jbz2 = new JButton("重置");
    Socket s; /**
     * 
     *无参构造方法
     */
    public Clientlogin() { zhuce.setSize(300, 200);
    zhuce.setLayout(new GridLayout(4, 1));
    jpz1.setLayout(new FlowLayout(FlowLayout.LEFT));
    jpz2.setLayout(new FlowLayout(FlowLayout.LEFT));
    jpz3.setLayout(new FlowLayout(FlowLayout.LEFT));
    jpz4.setLayout(new FlowLayout(FlowLayout.CENTER));
    jpz1.add(jlz1);
    jpz1.add(jtfz1); jpz2.add(jlz2);
    jpz2.add(jtfz2); jpz3.add(jlz3);
    jpz3.add(jtfz3); jpz4.add(jbz1);
    jpz4.add(jbz2);
    zhuce.add(jpz1);
    zhuce.add(jpz2);
    zhuce.add(jpz3);
    zhuce.add(jpz4);
    zhuce.setVisible(false);
    /**
     * 注册界面注册按钮点击事件
     */
    jbz1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
    String username = jtfz1.getText();
    String pass = jtfz2.getText();
    if(username.equals("")||pass.equals("")){
    JOptionPane.showMessageDialog(zhuce, "您输入的信息不完整", "提示",
    JOptionPane.INFORMATION_MESSAGE);
    }else
    if (jtfz2.getText().equals(jtfz3.getText())) {
    try {
    ps.println("=@" + username + "=@" + pass);
    String flag = br.readLine();
    if (flag.equals("=true")) {
    JOptionPane.showMessageDialog(zhuce, "注册成功", "提示",
    JOptionPane.INFORMATION_MESSAGE);
    jf.setVisible(true);
    zhuce.setVisible(false);
    }
    if (flag.equals("=false")) {
    JOptionPane.showMessageDialog(zhuce, "您输入的信息有误", "提示",
    JOptionPane.INFORMATION_MESSAGE);
    }
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    } else {
    JOptionPane.showMessageDialog(zhuce, "两次密码不一致", "提示",
    JOptionPane.INFORMATION_MESSAGE);
    }
    }
    });
    jbz2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
    jtfz1.setText("");
    jtfz2.setText("");
    jtfz3.setText("");
    }
    });
    jb2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
    jtf1.setText("");
    jtf2.setText("");
    }
    }); jf.setSize(300, 200);
    jf.setLayout(new GridLayout(4, 1));
    jf.add(mp);
    mp.repaint(); jp1.setLayout(new FlowLayout(FlowLayout.LEFT));
    jp3.setLayout(new FlowLayout(FlowLayout.LEFT));
    jf.add(jp1);
    jp1.add(jl1); jp1.add(jtf1);
    jp1.add(jl3);
    jp3.add(jl2);
    jp3.add(jtf2);
    jp3.add(jl4);
    jf.add(jp3);
    jf.add(jp2);
    jp2.add(jb1);
    jp2.add(jb2);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setVisible(true); jl3.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) {
    zhuce.setVisible(true);
    jf.setVisible(false); }
    });
    try {
    s = new Socket("125.83.28.144", 1001);
    ps = new PrintStream(s.getOutputStream());
    br = new BufferedReader(new InputStreamReader(s.getInputStream()));
    } catch (UnknownHostException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    jb1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    String username = jtf1.getText();
    String pass = jtf2.getText();
    if (username != null && pass != null && !username.equals("")
    && !pass.equals("")) {
    try {
    ps.println("@" + username + "@" + pass);
    String flag = br.readLine();
    if (flag.endsWith(",")) {
    jf.setVisible(false);
    System.out.println(flag + "0000000000");
    ClientMain.jl.setListData(flag.split(","));
    new ClientMain(ps, br, username);
    } else if (flag.equals("用户名或密码错误")) {
    JOptionPane.showMessageDialog(jf, "用户名密码错误", "提示",
    JOptionPane.INFORMATION_MESSAGE);
    } else if (flag.equals("@two")) {
    JOptionPane.showMessageDialog(jf,
    "该账号已经在其他地方登录,请先退出", "提示",
    JOptionPane.INFORMATION_MESSAGE);
    } } catch (UnknownHostException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    } else {
    JOptionPane.showMessageDialog(jf, "用户名密码不能为空", "提示",
    JOptionPane.INFORMATION_MESSAGE);
    }
    }
    });
    } /**
     * 主函数
     * 
     * @param args
     * 
     */
    public static void main(String[] args) {
    new Clientlogin();
    }
    }
      

  2.   

    这是客户端聊天(主面板):package client;import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.Window;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.WindowConstants;/**
     * @author 李佳
     * 
     */
    public class ClientMain {
    JFrame jf = new JFrame();
    /**
     * 在线人员列表
     */
    static JList jl = new JList();
    JPanel jp = new JPanel();
    JPanel jpp1 = new JPanel();
    /**
     * 在线列表标签
     */
    JLabel jll = new JLabel("在线列表");
    /**
     * 进入聊天室按钮
     */
    JButton jb = new JButton("进入聊天室");
    /**
     * 在线,隐身选择框
     */
    JComboBox jcb = new JComboBox();
    /**
     * 群聊窗口
     */
    JFrame jfchat = new JFrame();
    /**
     * 群聊输出文本域
     */
    JTextArea jtaout = new JTextArea();
    /**
     * 群聊输入文本域
     */
    JTextArea jtain = new JTextArea();
    JScrollPane jsp1 = new JScrollPane(jtain);
    JScrollPane jsp2 = new JScrollPane(jtaout);// 发送消息
    /**
     * 发送消息按钮
     */
    JButton jb1 = new JButton("发送信息");
    JPanel jp1 = new JPanel();
    /**
     * 输出流
     */
    PrintStream ps;
    /**
     * 输入流
     */
    BufferedReader br;
    /**
     * 存放聊天窗口(String,ClientChat)
     */
    HashMap mm = new HashMap();
    /**
     * 存放用户的ip地址 (String,String)
     */
    Map<String, String> ipmap = new HashMap<String, String>();
    /**
     * 存放用户的输出流 (String,PrintStream)
     */
    Map<String, PrintStream> psmap = new HashMap<String, PrintStream>();
    /**
     * 用户名
     */
    String username;
    /**
     * 客户端用于私聊的ServerSocket
     * 
     */
    ServerSocket ss; /**
     * 构造器
     * 
     * @param p
     *            输出流
     * @param b
     *            输入流
     * @param user
     *            用户帐号
     */
    public ClientMain(PrintStream p, BufferedReader b, String user) {
    this.ps = p;
    this.br = b;
    this.username = user;
    jfchat.setTitle(username + "在群里");
    jfchat.setSize(400, 400);
    jp1.setLayout(new GridLayout(2, 1, 15, 15)); jp1.add(jsp1);
    jp1.add(jsp2);
    jfchat.add(jp1);
    jfchat.add(jb1, BorderLayout.SOUTH); jcb.addItem("在线");
    jcb.addItem("隐身");
    jcb.setSelectedIndex(0);
    jpp1.add(jcb);
    jf.setTitle(username);
    System.out.println("当前用户");
    jf.setSize(180, 500);
    jp.setLayout(new BorderLayout());
    jp.add(jll, BorderLayout.NORTH);
    jp.add(jl, BorderLayout.CENTER);
    jf.add(jpp1, BorderLayout.NORTH);
    jf.add(jp, BorderLayout.CENTER);
    jf.setLocation(1000, 0);
    jf.add(jb, BorderLayout.SOUTH);
    jf.setVisible(true); jcb.addActionListener(new ActionListener() {
    String states = "在线"; public void actionPerformed(ActionEvent arg0) {
    // TODO 自动生成方法存根
    if (!jcb.getSelectedItem().toString().equals(states)) { System.out.println(jcb.getSelectedItem().toString());
    states = jcb.getSelectedItem().toString();
    ps.println(username + "=states=" + states);
    } // if()
    } }); jf.addWindowListener(new WindowAdapter() {// 用户下线 public void windowClosing(WindowEvent e) {
    ps.println("end=" + username);
    System.exit(0); }
    });
    try {
    ss = new ServerSocket(9999);
    ClientSerThread cst = new ClientSerThread();
    cst.start(); } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    ClientThread ct = new ClientThread();
    ct.start();
    jb.addActionListener(new ActionListener() {// 进入聊天室 public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    jfchat.setVisible(true);
    } });
    jb1.addActionListener(new ActionListener() {// 提交聊天室聊天消息 public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    Date date = new Date();
    ps.println(username + "在" + date.toLocaleString()
    + "对大家说" + jtaout.getText());
    jtaout.setText("");
    }
    });
    jl.addMouseListener(new MouseAdapter() {// 在线列表双击事件处理 public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub
    if (arg0.getClickCount() == 2 && !jl.isSelectionEmpty()) {
    // 或者双击所选择的用户
    String name = jl.getSelectedValue().toString();
    if (name != null && !name.equals("")) {
    System.out.println(name + "-------");
    // 当前在线用户与双击所选用户比较
    if (username.equals(name)) {
    System.out.println("对不起,你不能自言自语!");
    } else {
    if (!mm.containsKey(name)) {
    ps.println("giveip=" + username + "="
    + name);
    System.out.println("新建");
    } else {
    System.out.println(name + "+++++++-");
    ClientChat pf = (ClientChat) mm
    .get(name);
    pf.jf.setVisible(true);
    System.out.println("取出");
    }
    }
    }
    }
    }
    });
    } /**
     * 客户端的服务器连接线程(内部类)
     */
    class ClientSerThread extends Thread {
    public void run() { try {
    while (true) {
    Socket sk = ss.accept();
    ClientSerRead csr = new ClientSerRead();
    csr.s = sk;
    csr.start();
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
      

  3.   

    接上
    /**
     * 客户端的服务器读线程(内部类)
     */
    class ClientSerRead extends Thread {
    Socket s; public void run() {
    try {
    BufferedReader brr = new BufferedReader(new InputStreamReader(s
    .getInputStream())); while (true) {
    String tonam = brr.readLine(); System.out.println(tonam); if (tonam.startsWith("me=")) {// 连接方先自我介绍
    PrintStream pss = new PrintStream(s.getOutputStream());
    psmap.put(tonam.substring(3), pss); // 如果聊天对话框已经存在,则显示
    if (mm.containsKey(tonam.substring(3))) {
    ClientChat cc = (ClientChat) mm.get(tonam
    .substring(3));
    cc.jf.setVisible(true);
    } else {// 新建聊天对话框
    ipmap.put(tonam.substring(3), s.getInetAddress()
    .toString().substring(1));
    ClientChat cc = new ClientChat(psmap.get(tonam
    .substring(3)), username, tonam
    .substring(3), ipmap
    .get(tonam.substring(3)));
    mm.put(tonam.substring(3), cc); }
    } else if (tonam.contains("在") && tonam.contains("说:")) {// 聊天信息
    ClientChat cc = (ClientChat) mm
    .get(tonam.split("在")[0]);
    cc.jtain.append(tonam + "\n");
    cc.jf.setVisible(true);
    } else if (tonam.contains("=readfile=")) {// 发送文件toname=readfile
    ClientChat cc = (ClientChat) mm
    .get(tonam.split("=")[0]);
    cc.jtain.append("        " + tonam.split("=")[0]
    + "向你发送了文件:" + tonam.split("=")[2]
    + " 请及时接收文件\n");
    cc.jf.setVisible(true);
    cc.jb2.setVisible(true);
    } else if (tonam.contains("=begin=")) {// 发送文件提示消息toname=begin=提示消息
    ClientChat cc = (ClientChat) mm
    .get(tonam.split("=")[0]);
    cc.jtain.append(tonam.split("=")[2] + "\n");
    cc.jf.setVisible(true);
    } else if (tonam.endsWith("=end=")) {
    ClientChat cc = (ClientChat) mm
    .get(tonam.split("=")[0]);
    cc.jtain.append("        对方取消了文件发送\n");
    cc.jb2.setVisible(false);
    cc.jf.setVisible(true);
    }
    } } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } }
    } /**
     * 客户端的客户端线程(内部类)
     */
    class ClientCtThread extends Thread {
    BufferedReader brr; public void run() {
    try {
    while (true) {
    String tonam = brr.readLine();
    System.out.println(tonam);
    if (tonam.contains("在") && tonam.contains("说:")) {// 聊天信息
    ClientChat cc = (ClientChat) mm
    .get(tonam.split("在")[0]);
    cc.jtain.append(tonam + "\n");
    cc.jf.setVisible(true);
    } else if (tonam.contains("=readfile")) {// 发送文件
    ClientChat cc = (ClientChat) mm
    .get(tonam.split("=")[0]);
    cc.jtain.append("        " + tonam.split("=")[0]
    + "向你发送了文件:" + tonam.split("=")[2]
    + " 请及时接收文件\n");
    cc.jb2.setVisible(true);
    cc.jf.setVisible(true);
    } else if (tonam.contains("=begin=")) {
    ClientChat cc = (ClientChat) mm
    .get(tonam.split("=")[0]);
    cc.jtain.append(tonam.split("=")[2] + "\n");
    cc.jf.setVisible(true);
    } else if (tonam.endsWith("=end=")) {// 取消发送文件
    ClientChat cc = (ClientChat) mm
    .get(tonam.split("=")[0]);
    cc.jtain.append("        对方取消了文件发送\n");
    cc.jb2.setVisible(false);
    cc.jf.setVisible(true);
    }
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    } /**
     * 循环等待服务器端的消息的线程(内部类)
     */
    class ClientThread extends Thread {
    public void run() {
    while (true) {
    try {
    String message = br.readLine();
    System.out.println(message);
    if (message != null) {
    if (message.endsWith(",") && message.startsWith(",")) {// 获得在线人员名单
    jl.setListData(message.split(",")); } else if (message.startsWith("ip=")) {// 获取聊天对象的地址ip=name=adress
    ipmap.put(message.split("=")[1],
    message.split("=")[2]);
    Socket s = new Socket(message.split("=")[2], 9999);
    PrintStream pps = new PrintStream(s
    .getOutputStream());
    pps.println("me=" + username);// 自我介绍 BufferedReader bbr = new BufferedReader(
    new InputStreamReader(s.getInputStream()));
    ClientCtThread cct = new ClientCtThread();
    cct.brr = bbr;
    cct.start();
    ClientChat cc = new ClientChat(pps, username,
    message.split("=")[1],
    message.split("=")[2]);
    mm.put(message.split("=")[1], cc); } else {
    // 群聊
    jfchat.setVisible(true);
    jtain.append(message + "\n"); }
    } } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } } } }}