一、主要代码说明:
1.后台服务器交互的关键类
package comm.langsin.server;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;
import comm.langsin.createadd.MsgAddFriend;
import comm.langsin.createadd.MsgAddFriendResp;
import comm.langsin.createadd.MsgAddGroup;
import comm.langsin.jdbcuntil.JdbcUntil;
import comm.langsin.model.PacketObject;
import comm.langsin.model.ToolsCreateMsg;
import comm.langsin.model.ToolsParseMsg;
import comm.langsin.model.UserObject;
import comm.langsin.msg_file.MsgGroupMsg;
import comm.langsin.msg_file.MsgSendChat;
import comm.langsin.msg_log_reg.MsgHead;
import comm.langsin.msg_log_reg.MsgLog;
import comm.langsin.msg_log_reg.MsgLogResp;
import comm.langsin.msg_log_reg.MsgReg;
import comm.langsin.msg_log_reg.MsgRegResp;
import comm.langsin.msgoffline.MsgFriendList;
import comm.langsin.msgoffline.MsgGroupList;
import comm.langsin.protocol.Protocol;
public class ServerThread extends Thread {// 服务器处理客户端线程
private JdbcUntil JDBC = new JdbcUntil();
private Socket ClientObject = null;// 客户端线程对象
private InputStream Ins = null;// 读取客户端发来的消息
private OutputStream Ous = null;// 向客户端发送消息
private DataInputStream Datain = null;// 数据流对象对基本流对象进行包装
private DataOutputStream Dataou = null;// 数据流对象对基本流对象进行包装
private UserObject User = null;// 用户对象
private boolean LogOk = false;// 用户登陆成功的标志
public ServerThread(Socket Client) {// 构造方法
this.ClientObject = Client;
try {
this.Ins = Client.getInputStream();// 用于接收客户端发来的数据的输入流
this.Ous = Client.getOutputStream();// 用于向客户端发送消息的输出流
this.Datain = new DataInputStream(Ins);// 将普通输入流对象包装成
this.Dataou = new DataOutputStream(Ous);// 将普通输入流对象包装成
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public UserObject getUser() {// 取得这个线程对象代表的用户对象;
return this.User;
}
public void run() {
LogOk = ReadFirstMsg();// 读取为true
System.out.println(LogOk);
if (LogOk) {
DealTools.AddClient(User, this);// 将线程保存到Map队列中
}
while (LogOk) {// 登陆成功跳转到消息处理类,等到玩家匹配
try {
MsgHead Msg = this.ReceiveData();
DealTools.SendMsg(User, Msg);
} catch (Exception e) {
// TODO Auto-generated catch block
// e.printStackTrace();
LogOk = false;
// 这句话是让服务器打印,注释掉的话就不会报 Connection refused
}
}
DealTools.RemoveClient(User);// 当用户没加入游戏推出时将线程移除
}
private boolean ReadFirstMsg() {// 接收客户端发来的第一条消息
try {
MsgHead Msg = ReceiveData();
if (Msg.getType() == Protocol.common_log) {// 登陆请求消息
MsgLog Ml = (MsgLog) Msg;
return CheckLogin(Ml);
} else if (Msg.getType() == Protocol.common_reg) {// 注册请求消息
MsgReg Mr = (MsgReg) Msg;// 注册请求消息
int Srcnum = JDBC.GetMaxNum();// 从数据库中获得最大的账号作为返回给注册者
JDBC.Reg(Srcnum, Mr.getUsername(), Mr.getUserpwd(),
Mr.getUserSign()); // 将注册消息资料保存到数据库
MsgRegResp Mrr = new MsgRegResp();// 注册应答消息
Mrr.setTotalLength(4 + 1 + 4 + 4 + 1);
Mrr.setType(Protocol.common_reg_resp);
Mrr.setSrcNum(Protocol.ServerNUMBER);
Mrr.setDestNum(Srcnum);
Mrr.setState((byte) 1);
this.SendMsgToClient(Mrr);
}
} catch (Exception e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}
return false;
}
private boolean CheckLogin(MsgLog msg) throws IOException {// 检验是否登陆成功
int Srcnum = msg.getSrcNum();
String Pwd = msg.getPasswd();
User = JDBC.Log(Srcnum, Pwd);// 到数据库中去验证
MsgLogResp Mlr = new MsgLogResp();
if (User != null) {
String Nickname = User.getUsername();// 得到用户昵称
String UserSign = User.getSignature();// 得到用户个性签名
byte[] UserHead = User.getUserhead();// 得到用户头像
Mlr.setTotalLength(4 + 1 + 4 + 4 + 10 + 100 + 5318);
Mlr.setType(Protocol.common_log_resp);
Mlr.setSrcNum(Protocol.ServerNUMBER);
Mlr.setDestNum(Srcnum);
Mlr.setUsername(Nickname);
Mlr.setUsersign(UserSign);
Mlr.setUserhead(UserHead);
this.SendMsgToClient(Mlr);
SendFriendList(Srcnum);
SendGroupList(Srcnum);
GetChatMsg(Srcnum);
GetGroupMsg(Srcnum);
GetAddFriendOffline(Srcnum);
GetAddFriendResp(Srcnum);
GetAddGroup(Srcnum);
return true;
} else if (User == null) {
Mlr.setTotalLength(4 + 1 + 4 + 4 + 1);
Mlr.setType(Protocol.common_log_resp);
Mlr.setSrcNum(Protocol.ServerNUMBER);
Mlr.setDestNum(Srcnum);
Mlr.setState((byte) 1);
this.SendMsgToClient(Mlr);
}
this.disConn();
return false;
}
private MsgHead ReceiveData() throws Exception {// 从流对象上读取数据块,解析为消息对象
int TotalLength = Datain.readInt();// 读取消息总长度
System.out.println("服务器读取到消息总长度为: " + TotalLength);
byte[] Data = new byte[TotalLength - 4];// 减去消息总长度创建出byte数组,用于存放数据
Datain.readFully(Data);// 必须用readFully
System.out.println("服务器将消息放入缓冲区 Data【】数组中!!!!");
MsgHead Msg = ToolsParseMsg.parseMsg(Data);// 消息对象解包
System.out.println("服务器读到的消息对象" + Msg);
return Msg;
}
// 发送离线以及好友列表的方法
public void SendFriendList(int MAINNUM) {
List<UserObject> Friendlist = JDBC.FriendList(MAINNUM);
MsgFriendList Mfl = new MsgFriendList();
int Friendnum = Friendlist.size();
Mfl.setTotalLength(4 + 1 + 4 + 4 + 4 + Friendnum
* (4 + 10 + 100 + 5318));
Mfl.setType(Protocol.common_friendlist);
Mfl.setSrcNum(Protocol.ServerNUMBER);
Mfl.setDestNum(MAINNUM);
Mfl.setFriendsize(Friendnum);
Mfl.setFriendlist(Friendlist);
this.SendMsgToClient(Mfl);
}
// 发送群组列表
public void SendGroupList(int Mainnum) {
List<PacketObject> Packetlist = JDBC.GroupList(Mainnum);
MsgGroupList Mgl = new MsgGroupList();
int Groupnum = Packetlist.size();
if (Groupnum > 0) {
Mgl.setTotalLength(4 + 1 + 4 + 4 + 4 + Groupnum
* (4 + 10 + 100 + 1218));
Mgl.setType(Protocol.common_grouplist);
Mgl.setSrcNum(Protocol.ServerNUMBER);
Mgl.setDestNum(Mainnum);
Mgl.setPacketlist(Packetlist);
Mgl.setGroupsize(Packetlist.size());
this.SendMsgToClient(Mgl);
}
}
public boolean SendMsgToClient(MsgHead msg) {// 发送消息给客户端
try {
byte[] data = ToolsCreateMsg.PackMsg(msg);
this.Dataou.write(data);
this.Dataou.flush();
System.out.println("服务器发出消息对象:" + msg);
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("服务器发出消息对象出错:" + msg);
return false;
}
public void GetChatMsg(int Destnum) {// 获得离线消息
List<Object> Msg = JDBC.GetChatMsg(Destnum);
int NUM = Msg.size();
while (NUM > 0) {
NUM = NUM - 3;
MsgSendChat Msc = new MsgSendChat();
int Srcnum = (Integer) Msg.get(0);
String Srcname = (String) Msg.get(1);
String Info = (String) Msg.get(2);
Msc.setTotalLength(4 + 1 + 4 + 4 + 4 + 4 + 10
+ Info.getBytes().length);
Msc.setSrcNum(Srcnum);
Msc.setDestNum(Destnum);
Msc.setType(Protocol.common_chatmsg);
Msc.setMsg(Info);
Msc.setName(Srcname);
Msc.setBqnum(0);
Msc.setMsgsize(Info.getBytes().length);
this.SendMsgToClient(Msc);
}
}
public void GetGroupMsg(int Srcnum) {
List<Object> GroupMsg = JDBC.GetGroupMsg(Srcnum);
int NUM = GroupMsg.size();
while (NUM > 0) {
NUM = NUM - 5;
MsgGroupMsg Mgm = new MsgGroupMsg();
Mgm.setType(Protocol.common_chatgroupmsg);
Mgm.setDestNum(Srcnum);
Mgm.setSrcNum((Integer) GroupMsg.get(0));
Mgm.setName((String) GroupMsg.get(1));
Mgm.setPacketname((String) GroupMsg.get(3));
Mgm.setPacketnum((Integer) GroupMsg.get(2));
List<UserObject> Member = JDBC.GetMember((Integer) GroupMsg.get(2));
Mgm.setMsg((String) GroupMsg.get(4));
Mgm.setMembeList(Member);
Mgm.setMsgsize(GroupMsg.get(4).toString().getBytes().length);
Mgm.setTotalLength(4 + 1 + 4 + 4 + 10 + 10 + 4
+ GroupMsg.get(4).toString().getBytes().length + 4 + 1 + 4
+ 4 + Member.size() * (4 + 10) + 4);
byte State = JDBC.GroupState(Srcnum, (Integer) GroupMsg.get(2));
Mgm.setBqnum(0);
Mgm.setState(State);
Mgm.setFilenum(0);
this.SendMsgToClient(Mgm);
}
}
public void GetAddFriendOffline(int Srcnum) {
List<Object> Msg = JDBC.GetAddFriendOffline(Srcnum);
int NUM =Msg.size();
while(NUM>0){
NUM = NUM - 4;
MsgAddFriend Maf = new MsgAddFriend();
Maf.setTotalLength(4 + 1 + 4 + 4 + 10 + 100 + 100);
Maf.setType(Protocol.common_addfriend);
Maf.setSrcNum((int) Msg.get(0));
Maf.setDestNum(Srcnum);
Maf.setMyName((String) Msg.get(1));
Maf.setSign((String) Msg.get(2));
Maf.setMymag((String) Msg.get(3));
this.SendMsgToClient(Maf);
}
}
public void GetAddFriendResp(int Srcnum){
List<Object> Msg = JDBC.GetAddFriendResp(Srcnum);
int NUM = Msg.size();
while(NUM>0){
NUM = NUM-4;
MsgAddFriendResp Mafr = new MsgAddFriendResp();
Mafr.setTotalLength(4 + 1 + 4 + 4 + 10 + 100 + 1);
Mafr.setType(Protocol.common_addfriend_resp);
Mafr.setSrcNum((int) Msg.get(0));
Mafr.setDestNum(Srcnum);
Mafr.setFriendname((String) Msg.get(1));
Mafr.setFriendsign((String) Msg.get(2));
Mafr.setState((byte) Msg.get(3));
this.SendMsgToClient(Mafr);
}
}
public void GetAddGroup(int Mainnum){
List<Object> Msg = JDBC.GetAddGroup(Mainnum);
int NUM =Msg.size();
while(NUM>0){
NUM = NUM - 5;
MsgAddGroup Mag = new MsgAddGroup();
Mag.setTotalLength(4 + 1 + 4 + 4 + 4 + 10 + 10 + 100);
Mag.setType(Protocol.common_addgroup);
Mag.setSrcNum((int) Msg.get(0));
Mag.setDestNum(Mainnum);
Mag.setPacketnum((int) Msg.get(1));
Mag.setName((String) Msg.get(2));
Mag.setPascketname((String) Msg.get(3));
Mag.setContent((String) Msg.get(4));
this.SendMsgToClient(Mag);
}
}
// 断开连结这个处理线程与客户机的连结, 发生异常,或处理线程退出时调用
public void disConn() {
try {
LogOk = false;
this.ClientObject.close();
} catch (Exception ef) {
}
}
}
2.前台登陆界面及验证类
package com.langsin.gui;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
//登录界面
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import com.langsin.client.ClientConnection;
import comm.langsin.model.UserObject;
public class LogIn {
private ClientConnection conn = new ClientConnection();
private JFrame log;
private JPanel mainpanel;
private JPanel panel1;
private JPanel panel2;
private CardLayout cl;// 卡片布局管理器
private JComboBox<Integer> NUM;
private JPasswordField PWD;
private JCheckBox box1;
private JCheckBox box2;
private JLabel logon;
private JLabel background;
private JLabel label;
private JLabel cancel;
private JLabel min;
private JLabel exit;
private JLabel reg;
private JLabel want;
private JLabel head;
private JLabel head1;
private Toolkit kit = Toolkit.getDefaultToolkit();
public LogIn() {
Dimension src = kit.getScreenSize();
log = new JFrame();
background = new JLabel(new ImageIcon(
LogIn.class.getResource("/com/langsin/image/log.jpg")));
mainpanel = new JPanel();
panel1 = new JPanel();
panel1.setSize(430, 146);
NUM = new JComboBox<Integer>();
NUM.setEditable(true);
PWD = new JPasswordField();
PWD.setFont(new Font("楷体", Font.PLAIN, 15));
panel1.setLayout(null);
NUM.setBounds(130, 10, 180, 30);
PWD.setBounds(130, 40, 180, 30);
logon = new JLabel("安全登录");
logon.setFont(new Font("楷体", Font.PLAIN, 25));
logon.setForeground(new Color(0, 0, 255));
logon.setBounds(165, 100, 100, 30);
box1 = new JCheckBox("记住密码");
box2 = new JCheckBox("自动登录");
box1.setBounds(130, 70, 80, 30);
box2.setBounds(230, 70, 80, 30);
reg = new JLabel("注册账号");
reg.setForeground(new Color(0, 0, 100));
reg.setFont(new Font("楷体", Font.PLAIN, 15));
reg.setBounds(320, 10, 100, 30);
want = new JLabel("忘记密码");
want.setBounds(320, 40, 100, 30);
want.setForeground(new Color(0, 0, 100));
want.setFont(new Font("楷体", Font.PLAIN, 15));
head1 = new JLabel(new ImageIcon(
LogIn.class.getResource("/com/langsin/image/head.png")));
head1.setBounds(30, 12, 75, 75);
panel1.add(head1);
panel1.add(reg);
panel1.add(want);
panel1.add(box1);
panel1.add(box2);
panel1.add(NUM);
panel1.add(PWD);
panel1.add(logon);
panel2 = new JPanel();
panel2.setSize(430, 146);
panel2.setLayout(null);
label = new JLabel("正在登录!!!");
cancel = new JLabel("取消登录");
label.setFont(new Font("楷体", Font.PLAIN, 20));
cancel.setFont(new Font("楷体", Font.PLAIN, 25));
label.setForeground(new Color(0, 100, 250));
label.setBounds(150, 5, 160, 20);
cancel.setForeground(new Color(0, 50, 255));
cancel.setBounds(160, 100, 100, 30);
head = new JLabel(new ImageIcon(
LogIn.class.getResource("/com/langsin/image/head.png")));
head.setBounds(170, 25, 75, 75);
panel2.add(label);
panel2.add(head);
panel2.add(cancel);
log.setSize(430, 330);
log.setUndecorated(true);
new Drag(log).setDragable();
log.setLayout(null);
background.setBounds(0, 0, 430, 184);
mainpanel.setBounds(0, 184, 430, 146);
exit = new JLabel("X");
exit.setFont(new Font("楷体", Font.PLAIN, 20));
exit.setForeground(new Color(0, 0, 0));
min = new JLabel("-");
min.setFont(new Font("楷体", Font.PLAIN, 30));
min.setForeground(new Color(0, 0, 0));
exit.setBounds(410, 0, 20, 20);
min.setBounds(390, 0, 20, 20);
background.setLayout(null);
background.add(exit);
background.add(min);
cl = new CardLayout();
mainpanel.setLayout(cl);
mainpanel.add(panel1, "1");
mainpanel.add(panel2, "2");
log.add(background);
log.add(mainpanel);
log.setLocation((src.width - 430) / 2, (src.height - 330) / 2);
log.setVisible(true);
logon.addMouseListener(new Event());
cancel.addMouseListener(new Event());
exit.addMouseListener(new Event());
min.addMouseListener(new Event());
reg.addMouseListener(new Event());
}
// 事件类
class Event extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
if (e.getComponent().equals(logon)) {
cl.show(mainpanel, "2");
} else if (e.getComponent().equals(cancel)) {
cl.show(mainpanel, "1");
conn.CloseClient();
}
}
public void mouseReleased(MouseEvent e) {
if (e.getComponent().equals(logon)) {
int Usernum = Integer.parseInt((String) NUM.getSelectedItem());
String Userpwd = String.valueOf(PWD.getPassword());
if (conn.ConnServer()) {
UserObject User = conn.Log(Usernum, Userpwd);
if (User != null) {
MainGUI Main = new MainGUI(User, conn);
conn.start();
conn.AddMsgListener(Main);
log.dispose();
} else if(User ==null){
conn.CloseClient();
cl.first(mainpanel);
JOptionPane.showMessageDialog(log,"该用户不存在!!", "",
JOptionPane.ERROR_MESSAGE);
}
}
} else if (e.getComponent().equals(cancel)) {
cl.first(mainpanel);
conn.CloseClient();
} else if (e.getComponent().equals(exit)) {
log.dispose();
} else if (e.getComponent().equals(min)) {
log.setExtendedState(JFrame.ICONIFIED);
} else if (e.getComponent().equals(reg)) {
new Regeister();
}
}
public void mouseEntered(MouseEvent e) {
if (e.getComponent().equals(exit)) {
exit.setForeground(new Color(255, 0, 0));
exit.setFont(new Font("楷体", Font.PLAIN, 27));
} else if (e.getComponent().equals(min)) {
min.setForeground(new Color(0, 255, 0));
min.setFont(new Font("楷体", Font.PLAIN, 37));
}
}
public void mouseExited(MouseEvent e) {
if (e.getComponent().equals(min)) {
min.setForeground(new Color(0, 0, 0));
min.setFont(new Font("楷体", Font.PLAIN, 30));
} else if (e.getComponent().equals(exit)) {
exit.setForeground(new Color(0, 0, 0));
exit.setFont(new Font("楷体", Font.PLAIN, 20));
}
}
}
public static void main(String[] args) {
new LogIn();
}
}
3.java中连接说句库的JDBC
package comm.langsin.jdbcuntil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import comm.langsin.model.PacketObject;
import comm.langsin.model.UserObject;
public class JdbcUntil {
private static String Username = "tt";
private static String Passwd = "123";
private static String Url = "jdbc:oracle:thin:@localhost:1521:ORCL";
private static String Driver = "oracle.jdbc.driver.OracleDriver";
private static Connection Conn = null;
private FileInputStream fis;
private FileInputStream fis2;
private FileInputStream fis3;
private FileInputStream fis4;
private FileInputStream fis5;
public static Connection getconnection() {
try {
Class.forName(Driver);
Conn = DriverManager.getConnection(Url, Username, Passwd);// 试图与数据库建立连接
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Conn;
}
public int GetMaxNum() {// 获得最大的账号
int Maxnum = 0;
Conn = JdbcUntil.getconnection();
String sql = "select max(USERNUM) from USERMSG";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = Conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Maxnum = rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Maxnum++;
return Maxnum;
}
public int GetPacketNum() {// 获得最大的群号码
int PacketNum = 0;
Conn = JdbcUntil.getconnection();
String sql = "select max(GROUPNUM ) from USERGROUP ";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = Conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
PacketNum = rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PacketNum++;
return PacketNum;
}
public UserObject Log(int Mainnum, String Pwd) {// 登录验证
UserObject User = null;
Conn = JdbcUntil.getconnection();
String sql = "select USERNUM ,USERNAME,USERSIGN ,USERHEAD from USERMSG where USERNUM = ? and USERPWD = ?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Mainnum);
ps.setString(2, Pwd);
rs = ps.executeQuery();
while (rs.next()) {
try {
fis = new FileInputStream(new File(rs.getString(4)));
byte[] head = new byte[fis.available()];
fis.read(head);
User = new UserObject(rs.getInt(1), null, rs.getString(2),
rs.getString(3), head);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return User;
}
public void Reg(int oonum, String nickname, String pwd, String signature) {
Conn = JdbcUntil.getconnection();// 与数据库;建立连接
String sql = "insert into USERMSG values(?,?,?,?,?)";
PreparedStatement ps = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, oonum);
ps.setString(2, nickname);
ps.setString(3, pwd);
ps.setString(4, signature);
String head = "E:\\Head\\head.png";
ps.setString(5, head);
ps.executeQuery();
Conn.commit();
Conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public UserObject FindUser(int Friendnum) {// 查找好友
UserObject User = null;
Conn = JdbcUntil.getconnection();
String sql = "select USERNUM , USERNAME ,USERHEAD from USERMSG where USERNUM = ?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Friendnum);
rs = ps.executeQuery();
while (rs.next()) {
fis3 = new FileInputStream(new File(rs.getString(3)));
byte[] head = new byte[fis3.available()];
fis3.read(head);
User = new UserObject(rs.getInt(1), null, rs.getString(2),
null, head);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return User;
}
public PacketObject FindGroup(int Packetnum) {
PacketObject Packet = null;
Conn = JdbcUntil.getconnection();
String sql = "select MAINNUM,GROUPNUM ,GROUPNAME , GROUPHEAD from USERGROUP where GROUPNUM = ?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Packetnum);
rs = ps.executeQuery();
while (rs.next()) {
fis4 = new FileInputStream(new File(rs.getString(4)));
byte[] packethead = new byte[fis4.available()];
fis4.read(packethead);
Packet = new PacketObject(rs.getInt(1), rs.getInt(2),
rs.getString(3), null, packethead);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Packet;
}
public void CreatePacket(int Mainnum, int Packetnum, String Packetname,
String Packetsign) {// 创建群组
Conn = JdbcUntil.getconnection();
String path = "E:\\Head\\group.png";
String sql = "INSERT INTO USERGROUP VALUES(?,?,?,?,?)";
PreparedStatement ps = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Mainnum);
ps.setInt(2, Packetnum);
ps.setString(3, Packetname);
ps.setString(4, Packetsign);
ps.setString(5, path);
ps.executeQuery();
Conn.commit();
Conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void SaveMember(int Membernum, int Packetnum) {
Conn = JdbcUntil.getconnection();
String sql = "INSERT INTO GROUPMEMBERS values(?,?)";
PreparedStatement ps = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Membernum);
ps.setInt(2, Packetnum);
ps.executeQuery();
Conn.commit();
Conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void SaveFriend(int MAINNUM, int FRIENDNUM) {// 存储好友列表
Conn = JdbcUntil.getconnection();
String sql = "INSERT INTO USERFRIENDS VALUES(?,?)";
PreparedStatement ps = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, MAINNUM);
ps.setInt(2, FRIENDNUM);
ps.executeQuery();
Conn.commit();
Conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<UserObject> FriendList(int Mainnum) {
List<UserObject> FriendList = new ArrayList<UserObject>();
Conn = JdbcUntil.getconnection();
String sql1 = "SELECT FRIENDNUM FROM USERFRIENDS WHERE MAINNUM = ?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = Conn.prepareStatement(sql1);
ps.setInt(1, Mainnum);
rs = ps.executeQuery();
while (rs.next()) {
String sql2 = "SELECT USERNUM,USERNAME ,USERHEAD,USERSIGN FROM USERMSG WHERE USERNUM = ?";
PreparedStatement ps1 = null;
ResultSet rs1 = null;
ps1 = Conn.prepareStatement(sql2);
ps1.setInt(1, rs.getInt(1));
rs1 = ps1.executeQuery();
while (rs1.next()) {
fis2 = new FileInputStream(new File(rs1.getString(3)));
byte[] head = new byte[fis2.available()];
fis2.read(head);
UserObject Friend = new UserObject(rs1.getInt(1), null,
rs1.getString(2), rs1.getString(4), head);
FriendList.add(Friend);
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return FriendList;
}
public List<PacketObject> GroupList(int Mainnum) {
List<PacketObject> Packetlist = new ArrayList<PacketObject>();
Conn = JdbcUntil.getconnection();
String sql1 = "SELECT GROUPNUM FROM GROUPMEMBERS WHERE MEMBERNUM = ?";
PreparedStatement ps1 = null;
ResultSet rs1 = null;
try {
ps1 = Conn.prepareStatement(sql1);
ps1.setInt(1, Mainnum);
rs1 = ps1.executeQuery();
while (rs1.next()) {
String sql2 = "SELECT GROUPNUM ,GROUPNAME,GROUPSIGN,GROUPHEAD FROM USERGROUP WHERE GROUPNUM = ?";
PreparedStatement ps2 = null;
ResultSet rs2 = null;
ps2 = Conn.prepareStatement(sql2);
ps2.setInt(1, rs1.getInt(1));
rs2 = ps2.executeQuery();
while (rs2.next()) {
FileInputStream fis;
try {
fis = new FileInputStream(new File(rs2.getString(4)));
byte[] head = new byte[fis.available()];
fis.read(head);
PacketObject Packet = new PacketObject(0,
rs2.getInt(1), rs2.getString(2),
rs2.getString(3), head);
Packetlist.add(Packet);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Packetlist;
}
public String GetSign(int Mainnum) {
String Sign = null;
Conn = JdbcUntil.getconnection();
String sql = "SELECT USERSIGN FROM USERMSG WHERE USERNUM = ?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Mainnum);
rs = ps.executeQuery();
while (rs.next()) {
Sign = rs.getString(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Sign;
}
public List<UserObject> GetMember(int Packetnum) {
Conn = JdbcUntil.getconnection();
List<UserObject> Member = new ArrayList<UserObject>();
String sql1 = "SELECT MEMBERNUM FROM GROUPMEMBERS WHERE GROUPNUM = ?";
PreparedStatement ps1 = null;
ResultSet rs1 = null;
try {
ps1 = Conn.prepareStatement(sql1);
ps1.setInt(1, Packetnum);
rs1 = ps1.executeQuery();
while (rs1.next()) {
PreparedStatement ps2 = null;
ResultSet rs2 = null;
String sql2 = "SELECT USERNUM ,USERNAME FROM USERMSG WHERE USERNUM = ?";
ps2 = Conn.prepareStatement(sql2);
ps2.setInt(1, rs1.getInt(1));
rs2 = ps2.executeQuery();
while (rs2.next()) {
UserObject User = new UserObject(rs2.getInt(1), null,
rs2.getString(2), null, null);
Member.add(User);
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Member;
}
public byte GroupState(int Mainnum, int Packetnum) {
byte State = 1;
String sql = "SELECT * FROM USERGROUP WHERE MAINNUM = ? AND GROUPNUM = ?";
Conn = JdbcUntil.getconnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Mainnum);
ps.setInt(2, Packetnum);
rs = ps.executeQuery();
while (rs.next()) {
State = 0;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return State;
}
public List<Integer> GetAllMember(int Mainnum, int Packetnum) {
List<Integer> Membernum = new ArrayList<Integer>();
Conn = JdbcUntil.getconnection();
String sql = "SELECT MEMBERNUM FROM GROUPMEMBERS WHERE GROUPNUM = ? AND MEMBERNUM != ?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Packetnum);
ps.setInt(2, Mainnum);
rs = ps.executeQuery();
while (rs.next()) {
Membernum.add(rs.getInt(1));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Membernum;
}
/**
* 离线消息
* **/
public void SaveChatMsg(int Srcnum, int Destnum, String Srcname, String Msg) {// 存储单聊消息
Conn = JdbcUntil.getconnection();// 连接数据库
String sql = "INSERT INTO SINGLECHATMSG VALUES(?,?,?,?)";
PreparedStatement ps = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Srcnum);
ps.setInt(2, Destnum);
ps.setString(3, Srcname);
ps.setString(4, Msg);
ps.executeQuery();
Conn.commit();
Conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<Object> GetChatMsg(int Destnum) {// 获取用户的离线单聊消息
List<Object> Msg = new ArrayList<Object>();
Conn = JdbcUntil.getconnection();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "SELECT SRCNUM,SRCNAME,OFFLINEMSG FROM SINGLECHATMSG WHERE DESTNUM = ?";
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Destnum);
rs = ps.executeQuery();
while (rs.next()) {
Msg.add(rs.getInt(1));
Msg.add(rs.getString(2));
Msg.add(rs.getString(3));
}
PreparedStatement ps1 = null;
String sql1 = "DELETE SINGLECHATMSG WHERE DESTNUM = ?";
ps1 = Conn.prepareStatement(sql1);
ps1.setInt(1, Destnum);
ps1.executeQuery();
Conn.commit();
Conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Msg;
}
public void SaveGroupMsg(int SRCNUM, String SRCNAME, int DESTNUM,
int PACKETNUM, String PACKETNAME, String OFFLINEMSG) {// 存储群聊消息
Conn = JdbcUntil.getconnection();
String sql = " INSERT INTO GROUPCHATOFFLINEMSG VALUES(?,?,?,?,?,?)";
PreparedStatement ps = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, SRCNUM);
ps.setString(2, SRCNAME);
ps.setInt(3, DESTNUM);
ps.setInt(4, PACKETNUM);
ps.setString(5, PACKETNAME);
ps.setString(6, OFFLINEMSG);
ps.executeQuery();
Conn.commit();
Conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<Object> GetGroupMsg(int Srcnum) {
List<Object> GroupMsg = new ArrayList<Object>();
Conn = JdbcUntil.getconnection();
String sql = "SELECT SRCNUM,SRCNAME,PACKETNUM,PACKETNAME,OFFLINEMSG FROM GROUPCHATOFFLINEMSG WHERE DESTNUM = ?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Srcnum);
rs = ps.executeQuery();
while (rs.next()) {
GroupMsg.add(rs.getInt(1));
GroupMsg.add(rs.getString(2));
GroupMsg.add(rs.getInt(3));
GroupMsg.add(rs.getString(4));
GroupMsg.add(rs.getString(5));
}
String sql1 = "DELETE GROUPCHATOFFLINEMSG WHERE DESTNUM = ?";
PreparedStatement ps1 = null;
ps1 = Conn.prepareStatement(sql1);
ps1.setInt(1, Srcnum);
ps1.executeQuery();
Conn.commit();
Conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return GroupMsg;
}
public void RemoveFriend(int Mainnum, int Friendnum) {// 删除好友
Conn = JdbcUntil.getconnection();
String sql = "DELETE USERFRIENDS WHERE MAINNUM = ? AND FRIENDNUM = ?";
PreparedStatement ps = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Mainnum);
ps.setInt(2, Friendnum);
ps.executeQuery();
Conn.commit();
Conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void RemoveGroup(int Mainnum, int Packetnum) {// 删除群组
Conn = JdbcUntil.getconnection();
String sql = "DELETE GROUPMEMBERS GROUPMEMBERS WHERE MEMBERNUM =? AND GROUPNUM = ?";
PreparedStatement ps = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Mainnum);
ps.setInt(2, Packetnum);
ps.executeQuery();
Conn.commit();
Conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<Integer> GetAllFriend(int Mainnum) {
Conn = JdbcUntil.getconnection();
String sql = "SELECT FRIENDNUM FROM USERFRIENDS WHERE MAINNUM =? AND FRIENDNUM != ?";
List<Integer> Friendnum = new ArrayList<Integer>();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Mainnum);
ps.setInt(2, Mainnum);
rs = ps.executeQuery();
while (rs.next()) {
Friendnum.add(rs.getInt(1));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Friendnum;
}
public void AddFriendOffline(int Srcnum, int Destnum, String SRCNAME,
String SRCSIGN, String MSG) {// 添加好友离线请求消息
Conn = JdbcUntil.getconnection();
String sql = "INSERT INTO ADDFRIENDOFFLINE VALUES(?,?,?,?,?)";
PreparedStatement ps = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Srcnum);
ps.setInt(2, Destnum);
ps.setString(3, SRCNAME);
ps.setString(4, SRCSIGN);
ps.setString(5, MSG);
ps.executeQuery();
Conn.commit();
Conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<Object> GetAddFriendOffline(int Mainnum) {
List<Object> List = new ArrayList<Object>();
Conn = JdbcUntil.getconnection();
String sql = "SELECT SRCNUM,SRCNAME,SRCSIGN,MSG FROM ADDFRIENDOFFLINE WHERE DESTNUM = ?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Mainnum);
rs = ps.executeQuery();
while (rs.next()) {
List.add(rs.getInt(1));
List.add(rs.getString(2));
List.add(rs.getString(3));
List.add(rs.getString(4));
}
PreparedStatement ps1 = null;
String sql1 = "DELETE ADDFRIENDOFFLINE WHERE DESTNUM = ?";
ps1 = Conn.prepareStatement(sql1);
ps1.setInt(1, Mainnum);
ps1.executeQuery();
Conn.commit();
Conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return List;
}
public void SaveAddFriendResp(int SRCNUM, int Destnum, String Name,
String Sign, int State) {
Conn = JdbcUntil.getconnection();
String sql = "INSERT INTO ADDFRIENDOFFLINERESP VALUES(?,?,?,?,?)";
PreparedStatement ps = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, SRCNUM);
ps.setInt(2, Destnum);
ps.setString(3, Name);
ps.setString(4, Sign);
ps.setInt(5, State);
ps.executeQuery();
Conn.commit();
Conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<Object> GetAddFriendResp(int Mainnum) {// 获得添加好友离线应答消息
List<Object> List = new ArrayList<Object>();
Conn = JdbcUntil.getconnection();
String sql = "SELECT SRCNUM,SRCNAME,SRCSIGN,STATE FROM ADDFRIENDOFFLINERESP WHERE DESTNUM = ?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Mainnum);
rs = ps.executeQuery();
while (rs.next()) {
List.add(rs.getInt(1));
List.add(rs.getString(2));
List.add(rs.getString(3));
List.add(rs.getInt(4));
}
PreparedStatement ps1 = null;
String sql1 = "DELETE ADDFRIENDOFFLINERESP WHERE DESTNUM = ?";
ps1 = Conn.prepareStatement(sql1);
ps1.setInt(1, Mainnum);
ps1.executeQuery();
Conn.commit();
Conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return List;
}
public void AddGroup(int Srcnum, int Destnum, int Groupnum,
String Groupname, String NAME, String Msg) {
Conn = JdbcUntil.getconnection();
String sql = "INSERT INTO ADDGROUPOFFLINE VALUES(?,?,?,?,?,?)";
PreparedStatement ps = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Srcnum);
ps.setInt(2, Destnum);
ps.setInt(3, Groupnum);
ps.setString(4, Groupname);
ps.setString(5, NAME);
ps.setString(6, Msg);
ps.executeQuery();
Conn.commit();
Conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<Object> GetAddGroup(int Mainnum) {
List<Object> Msg = new ArrayList<Object>();
Conn = JdbcUntil.getconnection();
String sql = "SELECT SRCNUM,GROUPNUM,GROUPNAME,SRCNAME,MSG FROM ADDGROUPOFFLINE WHERE DESTNUM =?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Mainnum);
rs = ps.executeQuery();
while (rs.next()) {
Msg.add(rs.getInt(1));
Msg.add(rs.getInt(2));
Msg.add(rs.getString(3));
Msg.add(rs.getString(4));
Msg.add(rs.getString(5));
}
PreparedStatement ps1 = null;
String sql1 = "DELETE ADDGROUPOFFLINE WHERE DESTNUM = ?";
ps1 = Conn.prepareStatement(sql1);
ps1.setInt(1, Mainnum);
ps1.executeQuery();
Conn.commit();
Conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Msg;
}
public void PacketFile(int Groupnum, String Filename, String Filepath) {
Conn = JdbcUntil.getconnection();
String sql = "INSERT INTO GROUPFIE VALUES(?,?,?)";
PreparedStatement ps = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Groupnum);
ps.setString(2, Filename);
ps.setString(3, Filepath);
ps.executeQuery();
Conn.commit();
Conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<Object> GetGroupFile(int Groupnum) {// 文件名字
List<Object> File = new ArrayList<Object>();
Conn = JdbcUntil.getconnection();
String sql = "SELECT FILENAME FROM GROUPFIE WHERE PACKETNUM = ?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Groupnum);
rs = ps.executeQuery();
while (rs.next()) {
File.add(rs.getString(1));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return File;
}
public byte[] GetPacketFile(int Packetnum, String Filename) {
byte[] filedata = null;
Conn = JdbcUntil.getconnection();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "SELECT FILEPATH FROM GROUPFIE WHERE PACKETNUM = ? AND FILENAME = ?";
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, Packetnum);
ps.setString(2, Filename);
rs = ps.executeQuery();
while (rs.next()) {
fis5 = new FileInputStream(new File(rs.getString(1).trim()));
filedata = new byte[fis5.available()];
fis5.read(filedata);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return filedata;
}
public void RemoveMember(int num, int Packetnum) {
Conn = JdbcUntil.getconnection();
String sql = "DELETE GROUPMEMBERS WHERE MEMBERNUM = ? AND GROUPNUM = ?";
PreparedStatement ps = null;
try {
ps = Conn.prepareStatement(sql);
ps.setInt(1, num);
ps.setInt(2, Packetnum);
ps.executeQuery();
Conn.commit();
Conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<UserObject> FindaddMember() {
List<UserObject> List = new ArrayList<UserObject>();
Conn = JdbcUntil.getconnection();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "SELECT USERNUM ,USERNAME FROM USERMSG ";
try {
ps = Conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
UserObject User = new UserObject(rs.getInt(1), null,
rs.getString(2), null, null);
List.add(User);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return List;
}
}
二、测试
由于要测试的类太多了,我简单的测试了后台服务器的启动类
package comm.langsin.server;
import static org.junit.Assert.*;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import org.junit.Before;
import org.junit.Test;
import comm.langsin.protocol.Protocol;
public class MainServerTest {
private ServerSocket server;
@Before
public void setUp() throws Exception {
}
@Test
public void testRun() {
testSetServer();
}
@Test
public void testMainServer() {
MainServer Server = new MainServer(Protocol.ServerPort);
Server.start();
}
@Test
public void testSetServer() {
try {
server = new ServerSocket(Protocol.ServerPort);
System.out.println("服务器创建成功!!!!");
while (true) {// 死循环不断接受客户端发来的
Socket Client = server.accept();// 阻塞,不断接受客户端发来的消息
ServerThread ST = new ServerThread(Client);
ST.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testMain() {
MainServer Server = new MainServer(Protocol.ServerPort);
Server.start();
}
}
三、测试结果
客户端运行截图