?? chatserverframe.java~176~
字號(hào):
{
sendClients(message);
String now=gettime();
jTextArea1.append("系統(tǒng)消息: "+now+"/"+fromcast+"->"+"所用用戶"+info+"\n");
try{
int rsupdate=stmt.executeUpdate("INSERT INTO sc(St,Sf,Sb,Content)"+
"VALUES ('"+now+"','"+fromcast+"','所有用戶','"+info+"');");
}
catch(SQLException ex){
}
}
else
{
sendtoClient(message,broadcast);
String now=gettime();
jTextArea1.append("系統(tǒng)消息: "+now+"/"+fromcast+"->"+broadcast+info+"\n");
try{
int rsupdate=stmt.executeUpdate("INSERT INTO sc(St,Sf,Sb,Content)"+
"VALUES ('"+now+"','"+fromcast+"','"+broadcast+"','"+info+"');");
}
catch(SQLException ex){
}
}
}
else if(keyword.equalsIgnoreCase("QUIT"))
{
((DefaultListModel)jList1.getModel()).removeAllElements();
disconnect(this);
for(int i=0;i<clients.size();i++)
{
Client listdata1=(Client)clients.elementAt(i);
jList1.setModel(dld);
dld.addElement(listdata1.name+":"+listdata1.ip+"已經(jīng)連接");
}
notifyRoom();
this.stop() ;
}
else if(keyword.equalsIgnoreCase("SOU"))
{
StringBuffer message=new StringBuffer("SOU:");
message.append(broadcast+st.nextToken("\0"));
sendtoClient(message,broadcast);
}
}
}
public void send(StringBuffer msg){
ps.println(msg);
ps.flush();
}
}
public static synchronized void sendClients(StringBuffer msg)
{
for(int i=0;i<clients.size();i++){
Client c=(Client)clients.elementAt(i);
c.send(msg);
}
}
public synchronized void sendtoClient(StringBuffer msg,String name)
{
for(int i=0;i<clients.size();i++){
Client c=(Client)clients.elementAt(i);
if(c.name.equalsIgnoreCase(name))
{
c.send(msg);
break;
}
}
}
public static void notifyRoom()
{
StringBuffer people=new StringBuffer("PEOPLE");
for(int i=0;i<clients.size();i++){
Client c=(Client)clients.elementAt(i);
people.append(":"+c.name);
}
sendClients(people);
}
public static String gettime(){
Date now=new Date();
//獲取系統(tǒng)當(dāng)前時(shí)間并賦值給變量
String time=String.valueOf(now.getYear()+1900)+"."+String.valueOf(now.getMonth()+1)+"."+String.valueOf(now.getDate())+"["+String.valueOf(now.getHours())+":"+String.valueOf(now.getMinutes())+":"+String.valueOf(now.getSeconds()+"]");
return(time);
}
public static 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 synchronized void disconnect(Client c)
{
try{
String qn=c.name;
jTextArea1.append("系統(tǒng)消息: "+qn+"斷開(kāi)連接\n");
active_connects--;
c.socket.close();
clients.removeElement(c);
sendClients(new StringBuffer("QUIT"+":"+qn));
jTextField1.setText("目前已經(jīng)有"+clients.size()+"用戶連接");
c.socket.close();
}
catch(IOException e)
{
jTextArea1.append("系統(tǒng)消息:"+"Error"+e+"\n");
}
}
public void closeAll()
{
while(clients.size()>0)
{
Client c=(Client)clients.firstElement();
try{
c.socket.close();
}
catch(IOException e){
jTextArea1.append("系統(tǒng)消息:"+"聊天服務(wù)器"+"Error"+e+"\n");
}
finally{
clients.removeElement(c);
}
}
}
void jButton1_actionPerformed(ActionEvent e) {
StringBuffer info=new StringBuffer("MSG:");
if(jRadioButton1.isSelected()){
info.append(":"+"BROAD:"+"SERVER"+":");
info.append(jTextField2.getText());
sendClients(info);
String now=gettime();
jTextArea1.append("系統(tǒng)消息: "+now+"/SERVER"+"->"+"所用用戶:"+jTextField2.getText()+"\n");
try{
int rsupdate=stmt.executeUpdate("INSERT INTO sc(St,Sf,Sb,Content)"+
"VALUES ('"+now+"','SERVER','所用用戶','"+jTextField2.getText()+"');");
}
catch(SQLException ex){}
}
else{
info.append(":"+"SERVER:"+"SERVER"+":");
info.append(jTextField2.getText());
StringTokenizer st=new StringTokenizer((String)jList1.getSelectedValue(),":");
String qn=st.nextToken();
sendtoClient(info,qn);
String now=gettime();
jTextArea1.append("系統(tǒng)消息: "+now+"/SERVER"+"->"+qn+":"+jTextField2.getText()+"\n");
try{
int rsupdate=stmt.executeUpdate("INSERT INTO sc(St,Sf,Sb,Content)"+
"VALUES ('"+now+"','SERVER','"+qn+"','"+jTextField2.getText()+"');");
}
catch(SQLException ex){}
}
jTextField2.setText("");
}
void jButton4_actionPerformed(ActionEvent e) {
sendClients(new StringBuffer("QUIT:"+"SERVER"));
closeAll();
System.exit(0);
}
void jButton2_actionPerformed(ActionEvent e) {
StringTokenizer st=new StringTokenizer((String)jList1.getSelectedValue(),":");
String qn=st.nextToken();
for(int i=0;i<clients.size();i++){
Client c=(Client)clients.elementAt(i);
if(c.name.equalsIgnoreCase(qn))
{
c.send(new StringBuffer("QUIT:"+"DISC"));
break;
}
}
}
void this_windowClosing(WindowEvent e) {
sendClients(new StringBuffer("QUIT:"+"SERVER"));
closeAll();
System.exit(0);
}
}
class linkData {
public static Statement getStatement() {
try {
//注冊(cè)驅(qū)動(dòng)
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String URL =
"jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=Server.mdb";
Connection conn = DriverManager.getConnection(URL);
return conn.createStatement();
}
catch (SQLException ex) {
ex.printStackTrace();
return null;
}
catch (ClassNotFoundException ex) {
ex.printStackTrace();
return null;
}
}
}
class chatServerFrame_jButton3_actionAdapter implements java.awt.event.ActionListener {
chatServerFrame adaptee;
chatServerFrame_jButton3_actionAdapter(chatServerFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}
class chatServerFrame_jButton1_actionAdapter implements java.awt.event.ActionListener {
chatServerFrame adaptee;
chatServerFrame_jButton1_actionAdapter(chatServerFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class chatServerFrame_jButton4_actionAdapter implements java.awt.event.ActionListener {
chatServerFrame adaptee;
chatServerFrame_jButton4_actionAdapter(chatServerFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton4_actionPerformed(e);
}
}
class chatServerFrame_jButton2_actionAdapter implements java.awt.event.ActionListener {
chatServerFrame adaptee;
chatServerFrame_jButton2_actionAdapter(chatServerFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class chatServerFrame_this_windowAdapter extends java.awt.event.WindowAdapter {
chatServerFrame adaptee;
chatServerFrame_this_windowAdapter(chatServerFrame adaptee) {
this.adaptee = adaptee;
}
public void windowClosing(WindowEvent e) {
adaptee.this_windowClosing(e);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -