正文

中央空调模拟器(服务器端)2006-05-24 19:14:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/hofy/14802.html

分享到:

//中央空调模拟器的服务器端,最简模拟客户端为c.java import javax.swing.*;import javax.swing.event.*;import javax.swing.Timer;import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;import java.util.*;class dialog extends JDialog implements ActionListener{ JLabel label; JPanel p1,p2; JButton button1,button2; JFrame f; boolean flag=false; dialog(JFrame f,String t,String s,boolean m) {  super(f,t,m);  this.f=f;  label=new JLabel(s);  p1=new JPanel();  p2=new JPanel();  button1=new JButton("确定");  button2=new JButton("取消");  p1.add(label);  p2.add(button1);  button1.addActionListener(this);  p2.add(button2);  button2.addActionListener(this);  this.getContentPane().add(p1,BorderLayout.CENTER);  this.getContentPane().add(p2,BorderLayout.SOUTH);  this.getContentPane().setVisible(true);  this.setModal(m);  this.setBounds(f.getX()+100,f.getY()+100,300,150);  this.setResizable(false);  this.show();  this.addWindowFocusListener(new WindowAdapter()     {      public void windowClosing(WindowEvent e)      {       dialog.this.flag=true;      }     }); } public void actionPerformed(ActionEvent de) {  if(de.getSource()==button1)  {   dialog.this.flag=true;   this.setVisible(false);   return;  }  else if(de.getSource()==button2)  {   dialog.this.flag=false;   this.setVisible(false);   dialog.this.f.setVisible(true);   return;  } }}class wd_dialog extends JDialog implements ActionListener{ JLabel label=new JLabel("请输入今天的温度:"); JTextField text=new JTextField(4); JButton button1=new JButton("确定"); JButton button2=new JButton("重置"); boolean b; server_frame f=null; JFrame jf=new JFrame(); wd_dialog(server_frame f,String s,boolean m,boolean b) {  super(f,s,m);  this.b=b;  this.f=f;  Container c=this.getContentPane();  c.setLayout(null);  c.add(label);  c.add(text);  c.add(button1);  c.add(button2);  text.addActionListener(this);  button1.addActionListener(this);  button2.addActionListener(this);  label.setBounds(30,20,140,20);  text.setBounds(150,20,30,20);  button1.setBounds(20,50,60,20);  button2.setBounds(130,50,60,20);    c.setVisible(true);  this.setBounds(f.getX()+100,f.getY()+100,210,120);  addWindowListener(new WindowAdapter()     {      public void windowClosing(WindowEvent we)      {       if(wd_dialog.this.b)       {        System.exit(0);       }       else{wd_dialog.this.dispose();}      }     }); } public void actionPerformed(ActionEvent ae) {  if((ae.getSource())==button1||(ae.getSource()==text))  {   try   {    f.text.setText(wd_dialog.this.text.getText().trim());    if((Integer.parseInt(text.getText().trim()))>15)    {f.chf="吹冷风";f.b2.setEnabled(false);f.repaint();}    else{f.chf="吹热风";f.b1.setEnabled(false);f.repaint();}    this.dispose();   }   catch(Exception e){}  }  else if(ae.getSource()==button2)  {   wd_dialog.this.text.setText("");  } }}class server_frame extends JFrame implements ActionListener{ int khs=0; JLabel kl=new JLabel("连接到当前服务器的客户数为:"+khs); JButton b1=new JButton("吹暖风"); JButton b2=new JButton("吹凉风"); JButton b3=new JButton("关闭服务器"); JPanel p1,p2; JLabel label=new JLabel("今天温度"); JTextField text=new JTextField(5); JButton b4=new JButton("调节"); Object a[][]; Object columnName[]={"房间号","目标温度","当前温度","空调状态"}; JTable table; Vector c_list=new Vector(); String chf=null; Timer timer_1; public server_frame() {  super("中央空调服务器端");  Container c=this.getContentPane();    p1=new JPanel();  p2=new JPanel();    p1.setLayout(new GridLayout(1,1));  p1.add(kl);    p2.add(b1);  p2.add(b2);  p2.add(label);  p2.add(text);  text.setEditable(false);  p2.add(b4);  p2.add(b3);    b1.addActionListener(this);  b2.addActionListener(this);  b3.addActionListener(this);  b4.addActionListener(this);    c.add(p1,BorderLayout.NORTH);  c.add(p2,BorderLayout.SOUTH);  a=new Object[100][4];  table=new JTable(a,columnName);  table.setEnabled(false);  c.add(new JScrollPane(table),BorderLayout.CENTER);    timer_1=new Timer(1000,this);  timer_1.start();      this.setBounds(300,100,500,400);  this.setVisible(true);  this.setResizable(false);  this.addWindowListener(new WindowAdapter()      {       public void windowClosing(WindowEvent we)       {        server_frame.this.closing();       }      }); } public void actionPerformed(ActionEvent e) {  if(e.getSource()==b1)  {   dialog msg=new dialog(this,"警告","哇!都"+server_frame.this.text.getText()+"度了,还要送热风啊!",true);   if(msg.flag){this.chf="吹热风";server_frame.this.b1.setEnabled(false);   server_frame.this.b2.setEnabled(true);}   msg.dispose();     }  else if(e.getSource()==b2)  {   dialog msg=new dialog(this,"警告","现在温度"+server_frame.this.text.getText()+",想冻死人啊!",true);   if(msg.flag){this.chf="吹冷风";;server_frame.this.b2.setEnabled(false);   server_frame.this.b1.setEnabled(true);}   msg.dispose();     }  else if(e.getSource()==b3)  {   server_frame.this.closing();  }  else if(e.getSource()==b4)  {   new wd_dialog(this,"调节温度",true,false).setVisible(true);     }  else if(e.getSource()==timer_1)  {   kl.setText("连接到当前服务器的客户数为:"+khs);   /*int ks=0;   for(int i=0;!(a[i][0].equals(null));i++)   {    if(!(a[i][3].equals("关闭")))    {ks++;}   }   if(ks==0)   {chf=null;}*/  } }  public void closing() {  if(khs!=0)  {   dialog msg=new dialog(this,"关闭服务器","当前有"+khs+"个客户连接,真的要关闭服务器吗?",true);   if(msg.flag)   {    Vector v=server_frame.this.c_list;    for(int i=0;i<v.size();i++)    {     server_thread temp=(server_thread)v.elementAt(i);     try     {      temp.out.writeUTF("#CLOSE#");               }     catch(Exception es){}       }    System.exit(0);       }     else{msg.dispose();server_frame.this.setVisible(true);}  }  else{System.exit(0);} }}public class KTServer { public static void main (String args[]) {  ServerSocket ss=null;  Socket sc;  try  {   ss=new ServerSocket(9898);   System.out.println("Construction is compelet...");  }  catch(Exception e){}  server_frame sf=new server_frame();  wd_dialog wd=new wd_dialog(sf,"输入今天温度",true,true);  wd.setVisible(true);  while(true)  {   try   {    sc=ss.accept();    //DataOutputStream out=new DataOutputStream(sc.getOutputStream());    //DataInputStream in=new DataInputStream(sc.getInputStream());    //System.out.println(in.readUTF());    server_thread st=new server_thread(sf,sc);    sf.c_list.add(st);    st.start();   }   catch(Exception e){}     } }}class server_thread extends Thread{ Socket sc; DataInputStream in=null; DataOutputStream out=null; boolean flag=true; server_frame sf; server_thread(server_frame sf,Socket sc) {  this.sc=sc;  this.sf=sf;  try  {   in=new DataInputStream(sc.getInputStream());   out=new DataOutputStream(sc.getOutputStream());  }  catch(Exception e){}   } public void run() {  System.out.println("yunxing thread...");  while(flag)  {   String s=null;   try   {    s=in.readUTF();    System.out.println(s);   }   catch(Exception e){}   if (s!=null)   {    //#ROOM#---------------------    if (s.startsWith("#ROOM#"))    {     String temp_s=s.substring(6).trim();     boolean dup_n=false;     String b[]=new String[4];     StringTokenizer temp_st=new StringTokenizer(temp_s,",");     int i=0;     while(temp_st.hasMoreElements())     {      b[i]=(String)temp_st.nextElement();      System.out.println("b["+i+"]"+"="+b[i]);      i++;           }     for(int j=0;j<sf.c_list.size();j++)     {      String temp_n=((server_thread)this.sf.c_list.elementAt(j)).getName().trim();      System.out.println(temp_n);      if(b[0].equals(temp_n.trim()))      {       dup_n=true;       this.sf.c_list.remove(this);       break;      }     }     if(dup_n)     {      try      {       out.writeUTF("#ERROR#"+"房间名重复");      }      catch(Exception e){}      this.flag=false;      break;     }     if((Integer.parseInt(b[1])-Integer.parseInt(b[2]))>0)     {      b[3]="吹热风";     }     else {b[3]="吹冷风";}     if(!(b[3].equals(sf.chf.trim())))     {      try      {       out.writeUTF("#ERROR#"+"与服务器冲突,请求拒绝");       this.sf.c_list.remove(this);       System.out.println("拒绝请求..."+this.sf.chf);       break;      }      catch(Exception e){}     }     this.setName(b[0].trim());     this.sf.khs++;          System.out.println(this.sf.khs);     for(int j=0;j<100;j++)     {      if(this.sf.a[j][0]==null)      {       this.sf.a[j][0]=b[0].trim();       this.sf.a[j][1]=b[1].trim();       this.sf.a[j][2]=b[2].trim();       this.sf.a[j][3]=b[3].trim();              sf.table.repaint();       break;      }     }     try     {      out.writeUTF("#RIGHT#"+sf.text.getText().trim());     }     catch(Exception e){}    }    //#ROOM#---------------------    //#TEM#---------------------    if(s.startsWith("#TEM#"))    {          String temp_r=s.substring(5,s.indexOf(","));     String temp_tem=s.substring(s.indexOf(",")+1);     System.out.println(temp_r);     for(int i=0;i<sf.c_list.size();i++)     {      server_thread temp_t=(server_thread)sf.c_list.elementAt(i);      System.out.println(temp_t.getName()+"stop");      if(temp_t.getName().trim().equals(temp_r.trim()))      {       for(int j=0;j<100;j++)       {        if(sf.a[j][0].toString().trim().equals(temp_r.trim()))        {         sf.a[j][2]=temp_tem;         sf.table.repaint();         break;        }       }       break;      }     }     sf.c_list.remove(this);    }    //#TEM#---------------------    //#CHANGETEM#---------------------    if(s.startsWith("#CHANGETEM#"))    {     String temp_s=s.substring(11).trim();     String b[]=new String[3];     StringTokenizer temp_st=new StringTokenizer(temp_s,",");     int n=0;     while(temp_st.hasMoreElements())     {      b[n]=(String)temp_st.nextElement();      System.out.println("b["+n+"]"+"="+b[n]);      n++;           }     for(int i=0;i<sf.c_list.size();i++)     {      server_thread temp_t=(server_thread)sf.c_list.elementAt(i);      System.out.println(temp_t.getName()+"stop");      if(temp_t.getName().trim().equals(b[0].toString().trim()))      {       for(int j=0;j<100;j++)       {        if(sf.a[j][0].toString().trim().equals(b[0].toString().trim()))        {         if((Integer.parseInt(b[1])-Integer.parseInt(sf.a[j][2].toString().trim()))>0)         {          b[2]="吹热风";         }         else{b[2]="吹冷风";}         if(!(b[2].equals(sf.chf.trim())))         {          try          {           out.writeUTF("#ERROR#"+"与服务器冲突,请求拒绝");           this.sf.c_list.remove(this);           System.out.println("拒绝请求..."+this.sf.chf);           return;          }          catch(Exception e){}         }         sf.a[j][1]=b[1];         sf.table.repaint();         break;        }       }       break;      }     }     sf.c_list.remove(this);    }    //#CHANGETEM#---------------------    //#LEAVE#---------------------    if(s.startsWith("#LEAVE#"))    {     String temp_l=s.substring(7);     for(int i=0;i<sf.c_list.size();i++)     {      server_thread temp_t=(server_thread)sf.c_list.elementAt(i);      System.out.println(temp_t.getName()+"stop...leave");      if(temp_t.getName().trim().equals(temp_l.trim()))      {       for(int j=0;j<100;j++)       {        if(sf.a[j][0].toString().trim().equals(temp_l.trim()))        {         System.out.println("find..."+sf.a[j][0]);         for(int n=j;sf.a[n][0]!=null;n++)         {          sf.a[n][0]=sf.a[n+1][0];          sf.a[n][1]=sf.a[n+1][1];          sf.a[n][2]=sf.a[n+1][2];          sf.a[n][3]=sf.a[n+1][3];         }         break;        }       }       sf.c_list.remove(temp_t);       sf.c_list.remove(this);       sf.khs--;       sf.table.repaint();       break;      }     }    }    //#LEAVE#---------------------   }  } }}

阅读(2808) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册