package lxl聊天系统;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTabbedPane;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.border.BevelBorder;
import java.awt.Color;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class MainFrame extends JFrame {
JPanel contentPane;
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField();
JButton jButton1 = new JButton();
JTabbedPane jTabbedPane1 = new JTabbedPane();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JScrollPane jScrollPane2 = new JScrollPane();
JScrollPane jScrollPane3 = new JScrollPane();
Border border1 = BorderFactory.createBevelBorder(BevelBorder.RAISED,
Color.white, Color.white, new Color(115, 114, 105),
new Color(165, 163, 151));
Border border2 = BorderFactory.createEtchedBorder(EtchedBorder.RAISED,
Color.white, new Color(165, 163, 151));
JTextArea jTextArea2 = new JTextArea();
JTextArea jTextArea3 = new JTextArea();
JScrollPane jScrollPane1 = new JScrollPane();
Border border3 = BorderFactory.createEtchedBorder(Color.white,
new Color(165, 163, 151));
JTextArea jTextArea1 = new JTextArea();
ServerSocket server=null;
// ConnectSocket connect=null;
static Vector clients=new Vector();
class ConnectSocket extends Thread{
Socket socket;
public void run(){
while(true){
try {
socket = server.accept();
} catch (IOException ex) {
jTextArea1.append("客户连接失败!\n");
}
Client c=new Client(socket);
clients.addElement(c);
if(checkName(c)){
c.start();
notifyRoom();
}
else {
disconnect(c);
}
}
}
}
class Client extends Thread{
String name,pa;
BufferedReader dis;
PrintStream ps;
Socket socket;
public Client(Socket s){
socket=s;
try {
dis = new BufferedReader(new InputStreamReader(socket.
getInputStream()));
ps=new PrintStream(socket.getOutputStream());
String info=dis.readLine();
StringTokenizer stinfo=new StringTokenizer(info,":");
String head=stinfo.nextToken();
name=stinfo.nextToken();
pa=stinfo.nextToken();
//加入数据
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("Jdbc:Odbc:MyLXL","","");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from LXLuser where name='"+name+"'and pa='"+pa+"'");
if(rs.next()){
String str="YES:";
ResultSet rs1=st.executeQuery("select name from LXLFriend where username='"+name+"'");
while(rs1.next())
str+=rs1+":";
ps.println(str);
}else{
String str1 = "NO:"+"密码不正确";
ps.println(str1);
}
} catch (Exception ex1) {
}
// jTextArea1.append("系统提示:"+name+"已经连接\n");
//jTextArea1.append("ddd"+pa);
} catch (IOException ex) {
jTextArea1.append("系统消息:用户连接出错\n");
}
}
public void send(String msg){
ps.println(msg);
ps.flush();
}
public void run(){
while(true){
String line=null;
try {
line = dis.readLine();
} catch (IOException ex) {
jTextArea1.append("系统提示:读客户信息出错");
disconnect(this);
notifyRoom();
return;
}
StringTokenizer st=new StringTokenizer(line,":");
String keyword=st.nextToken();
if(keyword.equalsIgnoreCase("msg")){
sendClients(line);
}
else if(keyword.equalsIgnoreCase("quit")){
send("quit");
disconnect(this);
notifyRoom();
this.stop();
}
}
}
}
public boolean checkName(Client newClient){
for(int i=0;i<clients.size();i++){
Client c=(Client)clients.elementAt(i);
if((c!=newClient)&&(c.name).equals(newClient.name)){
return false;
}
}
return true;
}
public void notifyRoom(){
String people="PEOPLE";
for(int i=0;i<clients.size();i++){
Client c=(Client)clients.elementAt(i);
people+=":"+c.name;
}
sendClients(people);
}
public void sendClients(String msg){
for(int i=0;i<clients.size();i++){
Client c=(Client)clients.elementAt(i);
c.send(msg);
}
}
public void disconnect(Client c){
try {
jTextArea1.append(c.name + "断开连接\n");
c.send("quit");
clients.removeElement(c);
c.socket.close();
} catch (IOException ex) {
jTextArea1.append("服务器断开出错\n");
}
}
public MainFrame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
border1 = BorderFactory.createEtchedBorder(EtchedBorder.RAISED,
Color.white, new Color(165, 163, 151));
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(420, 350));
setTitle("LXL聊天系统服务器");
jLabel1.setText("服务器");
jLabel1.setBounds(new Rectangle(18, 17, 84, 36));
jTextField1.setEditable(false);
jTextField1.setText("6544");
jTextField1.setBounds(new Rectangle(108, 15, 152, 37));
jButton1.setBounds(new Rectangle(266, 15, 115, 36));
jButton1.setText("启动系统");
jButton1.addActionListener(new MainFrame_jButton1_actionAdapter(this));
jTabbedPane1.setBounds(new Rectangle(12, 67, 377, 223));
jPanel1.setLayout(null);
jPanel2.setLayout(null);
jScrollPane2.setBorder(border2);
jScrollPane2.setBounds(new Rectangle(4, 7, 363, 93));
jScrollPane3.setBorder(border1);
jScrollPane3.setBounds(new Rectangle(5, 110, 363, 80));
jTextArea2.setText("jTextArea2");
jTextArea3.setText("jTextArea3");
jScrollPane1.setBorder(border3);
jScrollPane1.setBounds(new Rectangle(4, 9, 363, 182));
contentPane.add(jLabel1);
contentPane.add(jTextField1);
contentPane.add(jButton1);
contentPane.add(jTabbedPane1);
jTabbed