哎 类太多了,像什么vo类及实现类我就不发了,再说这些类没什么技术含量
如果有必要的话 你留下邮箱,我将两个工程和数据库代码发给你
服务器类:
package chat;
import impl.UserImpl;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import vo.Userinfo;
import dao.UserDao;
/**
* 服务器类,用于接受来自客户端的请求,本系统规定来自客户端每一次请求 所传过来的参数不少于两个,第一个用来标识此次请求将做什么操作,后面
* 的参数为客户端请求服务所需的必要参数
*
* @author 程胜
* @version 0.1
* @date 09-01-08
*
*/
public class ChatServer extends Thread {
// 定义最大连接数
private static final int MAXLINK = 50;
// 记录当前连接数
public static int linkNum = 0;
// 定义服务连接对象
private static ServerSocket ss;
public static void main(String[] args) throws Exception {
// 实例化服务类
ss = new ServerSocket(8001);
// 启用多线程:其实就是调用run方法,好像没必要另启线程,哎 写好了 就不改了
new ChatServer().start();
System.out.println("服务器已成功启动…………");
}
/*
* 覆写run方法
*/
public void run() {
Socket soc = null;
try {
// 循环监听客户端请求
while (true) {
while (true) {
// 如果连接数已满,则等待
if (linkNum >= MAXLINK)
this.sleep(100);
else {
break;
}
}
// 监听请求
soc = ss.accept();
// 将连接加一
linkNum++;
// 获得socket对象后调用处理类
new Operate(soc);
}
} catch (Exception e) {
System.out.println(e);
} finally {
if (soc != null) {
try {
soc.close();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("服务器出现莫名故障,服务器退出!");
}
}
}
}
/**
* 操作类,用来处理来自客户端的请求
*
* @author Administrator
*
*/
class Operate {
// 接定义用于实别客户端发来的头号请求将做何操作的变量
private String operate;
/**
* 以下几行用于声明网络操作的必要变量
*/
private Socket soc = null;
private InputStream ips = null;
private OutputStream ops = null;
private ObjectOutputStream soops = null;
private ObjectInputStream soips = null;
/**
* 构造函数,在其中为以上定义的变量的赋值
*
* @param soc
*/
Operate(Socket soc) {
this.soc = soc;
try {
ips = soc.getInputStream();
ops = soc.getOutputStream();
soips = new ObjectInputStream(ips);
// 接收从客户端发来的头号请求,它用于实别此次请求的内容
operate = soips.readObject().toString();
} catch (Exception e) {
System.out.println(e);
}
// 调用实际操作方法
whichOpe();
}
/**
* 判断是哪个操作
*/
private void whichOpe() {
if ("login".equals(operate)) {
login();
} else {
if ("".equals(operate)) {
}
}
}
/**
* 连接数据库,进行登录验证
*/
private void login() {
UserDao userdao = new UserImpl();
Userinfo user = null;
try {
// 读取从客户端传过来的数据
user = (Userinfo) soips.readObject();
user = userdao.login(user);
soops = new ObjectOutputStream(ops);
// 将结果传给客户端
soops.writeObject(user);
soops.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
soops.close();
soips.close();
soc.close();
// 将连接数减一
ChatServer.linkNum--;
} catch (IOException e) {
System.out.println(e);
}
}
}
}
客户端的两个类:
package client;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import vo.Userinfo;
/**
* 登录界面,继承自jframe类
*
* @author 程胜
* @version 0.1
* @date 09-01-08
* @address 学校寝室:综合楼424
*
*/
public class Login extends JFrame {
private final int TEXTSIZE = 10;// 定义文本长度的值
private JLabel lname = new JLabel("用户名");
private JLabel lpassword = new JLabel("密码");
private JLabel lhead = new JLabel("用户登录");
private JButton bok = new JButton("登录");
private JButton bcancel = new JButton("退出");
private JPanel pcenter = new JPanel();
private JPanel pname = new JPanel();
private JPanel ppwd = new JPanel();
private JPanel psouth = new JPanel();
private JPanel pnorth = new JPanel();
private TextField pwd = new TextField(TEXTSIZE);
private JTextField name = new JTextField(TEXTSIZE);
private GridLayout glayout = new GridLayout();
public Login() {
// 设置界面大小及位置
this.setBounds(300, 200, 320, 250);
this.setTitle("用户登录");
this.setResizable(false);
// 设置窗口的关闭方式
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
// 布置窗口
this.frameLayout();
// 添加事件
this.addAction();
// 显示窗口
this.show();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Login();
}
/**
* 布置容器
*/
private final void frameLayout() {
lhead.setFont(new Font("", Font.BOLD, 28));
glayout.setColumns(1);
glayout.setRows(2);
pcenter.setLayout(glayout);
pname.add(lname);
pname.add(name);
pwd.setEchoChar('*');
ppwd.add(lpassword);
ppwd.add(pwd);
pnorth.add(lhead);
psouth.add(bok);
psouth.add(bcancel);
pcenter.add(pname);
pcenter.add(ppwd);
this.add(pnorth, "North");
this.add(pcenter, "Center");
this.add(psouth, "South");
}
/**
* 添加事件
*/
private final void addAction() {
bok.addActionListener(new AddAction());
bcancel.addActionListener(new AddAction());
name.addActionListener(new AddAction());
pwd.addActionListener(new AddAction());
}
/**
* 此类为内置类,用于事件处理
*
* @author Administrator
*
*/
private class AddAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == bcancel) {
System.exit(0);
} else {
if ("".equals(name.getText()) || "".equals(pwd.getText())) {
JOptionPane.showMessageDialog(Login.this, "用户名和密码不能为空");
} else {
try {
// 判断输入的数据是否合法
Integer.parseInt(name.getText());
} catch (Exception ex) {
JOptionPane.showMessageDialog(Login.this, "请正确输入号码");
return;
}
new TcpLogin(name.getText(), pwd.getText()).check();
}
}
}
}
/**
* 登录验证类,内置类 将用户名和密码传到服务器做验证
*
* @author 程胜
* @version 0.1
* @date 09-01-08
*/
class TcpLogin {
private int name;
private String pwd = "";
/**
* 构造函数,初始化用户名和密码的值
*
* @param name
* @param pwd
*/
TcpLogin(String name, String pwd) {
this.name = Integer.parseInt(name);
this.pwd = pwd;
}
/**
* 连接服务器,验证用户信息方法
*/
void check() {
Window waitWin=new Window(Login.this);
// 获得界面
Login login = Login.this;
Userinfo user = new Userinfo();
user.setUserId(name);
user.setPassword(pwd);
Socket soc = null;
InputStream ips = null;
OutputStream ops = null;
ObjectInputStream coips = null;
ObjectOutputStream coops = null;
try {
soc = new Socket("127.0.0.1", 8001);
ips = soc.getInputStream();
ops = soc.getOutputStream();
coops = new ObjectOutputStream(ops);
coops.writeObject("login");
coops.writeObject(user);
coops.flush();
ObjectInputStream oips = new ObjectInputStream(ips);
user = null;
user = (Userinfo) oips.readObject();
if (user != null){
new XiHa(user);
login.dispose();
}
else {
JOptionPane.showMessageDialog(login, "用户名和密码错误");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ips.close();
ops.close();
soc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package client;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.Timer;
import javax.swing.tree.DefaultMutableTreeNode;
import vo.Groupinfo;
import vo.Relationinfo;
import vo.Userinfo;
/**
* 此类为本程序客户端的主界面,刚想了好会儿决定取名叫’嘻哈‘ 意为’嘻嘻哈哈‘
*
* @author 程胜
* @version 0.1
* @date 09-01-08
* @address 学校寝室:综合楼424
*/
public class XiHa extends JFrame {
// 好友信息
static List
// 用户信息
private Userinfo user = null;
private JPanel ptree = new JPanel();
private JPanel psouth = new JPanel();
private JButton bfind = new JButton("查找");
private JButton bsystem = new JButton("系统设置");
/**
* 构造函数,设置界面基本参数
*/
public XiHa(Userinfo user) {
this.user = user;
this.setTitle("嘻嘻哈哈");
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setBounds(800, 100, 200, 500);
this.setResizable(false);
// 布置界面
initFrame();
// this.pack();
this.setVisible(true);
}
/**
* 用于界面布置
*/
private void initFrame() {
ptree.add(new JScrollPane(new CreateTree(user).createNode()));
psouth.add(bsystem);
psouth.add(bfind);
this.add(ptree, "Center");
this.add(psouth, "South");
}
}
/**
* 本类用于创建以用户信息为节点的树
*
* @author 程胜
* @version 0.1
* @date 09-01-08
* @address 学校寝室:综合楼424
*
*/
class CreateTree {
JTree tree=null;
private Userinfo user = null;
CreateTree(Userinfo user) {
this.user = user;
}
/**
* 用服务器传回的数据创建一棵树
*
* @return
*/
JTree createNode() {
//根结点
DefaultMutableTreeNode all = new DefaultMutableTreeNode("所有好友");
Set
Set
Iterator
Iterator
Groupinfo group = null;
Relationinfo relation = null;
DefaultMutableTreeNode friendNode = null;
Userinfo friend = null;
while (ig.hasNext()) {
group = ig.next();
friendNode = new DefaultMutableTreeNode(group.getGroupName());
ir = relations.iterator();
while (ir.hasNext()) {
relation = ir.next();
if (relation.getResideGroupId() == group.getGroupId()) {
friend = relation.getFriend();
friendNode.add(new DefaultMutableTreeNode(friend
.getPetname()
+ "(" + relation.getFriendId() + ")"));
XiHa.friends.add(friend);
}
}
all.add(friendNode);
// rootTree.add(friendNode);
}
tree=new JTree(all);
tree.addMouseListener(new TreeListener());
return tree;
}
/**
* 此类为创建树的内置类,用于对结点的事件处理
*
* @author 程胜
* @version 0.1
* @date 09-01-09
* @address 学校寝室:综合楼424
*/
class TreeListener extends MouseAdapter{
private JFrame friendInfo=null;
private String nodeInfo="";
//用于区分单击双击
Timer mouseTimer =null;
/*
* 覆写mouseAdapter中的mouseClicked方法,鼠标点击时触发
*/
public void mouseClicked(MouseEvent e){
//返回选定的结点
DefaultMutableTreeNode treeNode =null;
treeNode =(DefaultMutableTreeNode)CreateTree.this.tree.getLastSelectedPathComponent();
if(treeNode==null)return;
//如果它不是叶子结点就返回
if(!treeNode.isLeaf())return;
nodeInfo=treeNode.toString();
if(e.getClickCount()==1){
mouseTimer = new javax.swing.Timer(350, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//布置信息界面
initFriendInfo();
mouseTimer.stop();
}
});
mouseTimer.restart();
}else{
if(e.getClickCount()==2&&mouseTimer.isRunning()){
mouseTimer.stop();
//实例化聊天界面
new WinChat();
}
}
}
/**
* 布置friendInfo的界面
*/
private void initFriendInfo() {
friendInfo=new JFrame();
friendInfo.setSize(300,120);
friendInfo.setDefaultCloseOperation(friendInfo.DISPOSE_ON_CLOSE);
friendInfo.setLocationRelativeTo(null);
friendInfoContent(this.treat());
friendInfo.setVisible(true);
}
/**
* 设置窗口中的具体内容
* @param friendId
* @return
*/
private void friendInfoContent(int friendId) {
friendInfo.dispose();
Userinfo friend=this.findFriend(friendId);
JPanel pmain=new JPanel();
pmain.setLayout(new GridLayout(3,1));
JPanel panel1=new JPanel();
panel1.add(new JLabel("嘻哈号:"+friend.getUserId()+" "+"昵称:"+friend.getPetname()));
JPanel panel2=new JPanel();
panel2.add(new JLabel("姓名:"+(friend.getName()==null?"":friend.getName())+" "+"年龄:"+friend.getAge()+" "+"性别:"+friend.getSex()));
JPanel panel3=new JPanel();
panel3.add(new JLabel("个性签名: "+(friend.getAutograph()==null?"":friend.getAutograph())));
pmain.add(panel1);pmain.add(panel2);pmain.add(panel3);
friendInfo.add(pmain);
}
/**
* 此方法用于获得好友的id号 从节点上的文本中获取
* @return 好友id
*/
private int treat(){
int len=nodeInfo.getBytes().length;
int friendId=Integer.parseInt(new String(nodeInfo.getBytes(),len-5,4));
return friendId;
}
/**
* 此方法用于按id查找好友信息
* @param friendId
* @return 好友信息
*/
private Userinfo findFriend(int friendId){
Userinfo friend=null;
Iterator friends=XiHa.friends.iterator();
while(friends.hasNext()){
Userinfo ifriend=(Userinfo)friends.next();
if(ifriend.getUserId()==friendId){
friend=ifriend;
break;
}
}
return friend;
}
}
}
/**
* 本类聊天窗口类,还不知道该怎么写,现在有点茫然,刚跟网友交流了一下
* 觉得前面写的通信代码很不规范,应该重新写,哎 先把界面写好再说
*
* 一会儿就要回去过年了,可能就没那么时间写这个系统了
* @author 程胜
* @version 0.1
* @date 09-01-11
* @address 学校寝室:综合楼424
*
*/
class WinChat extends JFrame{
private JPanel pmain=new JPanel();
private JPanel pcether=new JPanel();
private JPanel psouth=new JPanel();
//private JTextArea precod=new JTextArea(JTextArea.);
}