百度空间 | 百度首页 
 
查看文章
 
ChatServer
2007-08-18 13:24

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;
import java.util.StringTokenizer;
import java.sql.*;
import java.io.*;
import java.net.*;
public class ChatServer
{
MyFrame f;
ServerSocket ss;
Socket mysocket;
   Vector myvector;
   boolean kongwei;
   Vector mainvector;
public ChatServer()
{
   f=new MyFrame();
   myvector=new Vector();
   mainvector=new Vector();
   initialInformation();
   try
{
   ss=new ServerSocket(12345);
}catch(Exception e){e.printStackTrace();}
   new Thread(new Runnable()
{
   public void run()
   {
    while(true)
    {
     try
     {
     mysocket=ss.accept();
     ReceiveSocket rs=new ReceiveSocket(mysocket,f,mainvector,myvector);
     if(rs.islive)
     {
     mainvector.add(rs);
     rs.start();   
     }
     else
     {
     rs.stop();
     }
     }catch(Exception e){e.printStackTrace();}
    }
   }
   }).start();
}

public void initialInformation()
   {
    Connection con=null;Statement sql;ResultSet rs;
try{
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(Exception e){e.printStackTrace();}
try
{
   con=DriverManager.getConnection("jdbc:odbc:ooo","lukun","369369");
   con.setAutoCommit(false);
   sql=con.createStatement();
   String getall="Select * From participant order by ID";
   rs=sql.executeQuery(getall);
   while(rs.next())
   {
    Participant myparticipant=new Participant();
    myparticipant.setName(rs.getString("name"));
    myparticipant.setID(rs.getInt("ID"));
    myparticipant.setPassword(rs.getString("password"));
     myparticipant.setLovelyname(rs.getString("lovelyname"));
     myparticipant.setMotto(rs.getString("motto"));
     myparticipant.setMessage(rs.getString("message"));
     myparticipant.setHeadphoto(rs.getString("headphoto"));
     myparticipant.setIsonline(rs.getString("isonline"));
     myparticipant.setFriendlist(rs.getString("friendlist"));
     System.out.println(myparticipant.getName()+"   "+myparticipant.getPassword()
     +"   "+myparticipant.getMotto()+"   "+myparticipant.getLovelyname()+"   "+myparticipant.getHeadphoto()
     +"   "+myparticipant.getMessage()+"   "+myparticipant.getIsonline()+"   "+myparticipant.getID());
    myvector.add(myparticipant);
   }
   con.close();
}catch(Exception e){}
   }
public static void main(String agrag[])
{
   new ChatServer();
}
}

class ReceiveSocket extends Thread
{
Socket s;
   DataOutputStream dos;
   DataInputStream dis;
   Vector mainvector;
   Vector myvector;
   MyFrame f;
   Participant yourparticipant;
   boolean islive;
   ReceiveSocket rsocket=null;
public ReceiveSocket(Socket s,MyFrame f,Vector mainvector,Vector myvector)
{
   this.s=s;
   this.f=f;
   this.mainvector=mainvector;
   this.myvector=myvector;
    try
   {
   dos=new DataOutputStream(s.getOutputStream());
   dis=new DataInputStream(s.getInputStream());
}catch(Exception e){e.printStackTrace();}
  
   try{
   String str=dis.readUTF();
   StringTokenizer sr=new StringTokenizer(str,"#");
   if(lookNameAndPassword(sr.nextToken().toString(),sr.nextToken().toString()))
   {
    dos.writeUTF("OK");
    yourparticipant.setIsonline("1");
    dos.writeUTF(String.valueOf(yourparticipant.getID()));
    dos.writeUTF(yourparticipant.getName());
    dos.writeUTF(yourparticipant.getLovelyname());
    dos.writeUTF(yourparticipant.getIsonline());
    dos.writeUTF(yourparticipant.getMotto());
    dos.writeUTF(yourparticipant.getHeadphoto());
    dos.writeUTF(yourparticipant.getMessage());
    dos.writeUTF(yourparticipant.getFriendlist());
    f.area1.append(yourparticipant.getLovelyname()+"上线了"+"\n");
    islive=true;
   }
   else
   {
    dos.writeUTF("NO");
    islive=false;
   }
}catch(Exception e){e.printStackTrace();}
}
public void run()
{
   try{
   while(islive)
   {  
    try
    {
   if(isHasFriend(yourparticipant.getFriendlist()))
   {
    StringTokenizer stkr=new StringTokenizer(yourparticipant.getFriendlist());   
     while(stkr.hasMoreElements())
     {
     int z=Integer.parseInt(stkr.nextToken());
      Participant ptt=returnParticipant(z);
      writeInfo(ptt);
     }
     dos.writeUTF("over");
       if(isHasFriend(yourparticipant.getFriendlist()))
   {
    StringTokenizer stkrr=new StringTokenizer(yourparticipant.getFriendlist());   
     while(stkrr.hasMoreElements())
     {
     int z=Integer.parseInt(stkrr.nextToken());
      ReceiveSocket ppt=returnReceiveSocket(z);
       if(ppt!=null)
        ppt.dos.writeUTF(6+"#"+"a"+"#"+yourparticipant.getID());
    }
   }
     break;
    }
    //这里是当对方下线后进行处理 ;
     }catch(Exception e){System.out.println("over one");return;}
   }
      while(true)
    {
     System.out.println(2);
     try{
     divideInfo(dis.readUTF());
   }catch(Exception e){e.printStackTrace();}
    }
}catch(Exception ee){ee.printStackTrace();}
}
public boolean isHasFriend(String str)
{
   boolean bln=false;
   if(str!=null)
   {
   bln=true;
   }
   return bln;
}
public void divideInfo(String clientinfo)throws Exception
{
   StringTokenizer stk=new StringTokenizer(clientinfo,"#");
   String str=stk.nextToken();
if(str.equals("1"))
   {
    yourparticipant.setPassword(stk.nextToken());
   }
   else if(str.equals("2"))
   {
   yourparticipant.setName(stk.nextToken());
   }
     else if(str.equals("3"))
   {
   yourparticipant.setLovelyname(stk.nextToken());
   }
     else if(str.equals("4"))
   {
   yourparticipant.setHeadphoto(stk.nextToken());
   }
     else if(str.equals("5"))
   {
   yourparticipant.setMotto(stk.nextToken());
   }
     else if(str.equals("6"))
   {
    String strg=stk.nextToken();
    if(strg.equals("0"))
     {
    
     for(int i=0;i<mainvector.size();i++)
     {
      String stkk=stk.nextToken();
      ((ReceiveSocket)(mainvector.get(i))).dos.writeUTF(clientinfo);
      f.area2.append("对大家"+"\n"+stkk);
     }
     }
     else
     {
       System.out.println(6);
      String strkk=stk.nextToken();
      ReceiveSocket ptt=returnReceiveSocket(Integer.parseInt(strg));
      ptt.dos.writeUTF(6+"#"+yourparticipant.getID()+"#"+strkk);
      f.area2.append("对"+ptt.yourparticipant.getLovelyname()+strkk);
     }
   }
}
public void executesql(String strsql)
{
   Connection con=null;Statement sql=null;
try{
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(Exception e){e.printStackTrace();}
try
{
   con=DriverManager.getConnection("jdbc:odbc:ooo","lukun","369369");
   con.setAutoCommit(false);
   sql=con.createStatement();
   sql.executeUpdate(strsql);

   con.close();
}catch(Exception e){}
}
public int getParticipantID()
{
   return yourparticipant.getID();
}
public ReceiveSocket returnReceiveSocket(int ID)
{
ReceiveSocket ptt=null;
for(int i=0;i<mainvector.size();i++)
{
   if(((ReceiveSocket)mainvector.get(i)).getParticipantID()==ID)
   {
   ptt=(ReceiveSocket)mainvector.get(i);
   break;
   }
}
   return ptt;
}
public Participant returnParticipant(int ID)
{
   Participant ptt=null;
for(int i=0;i<myvector.size();i++)
{
   if(((Participant)myvector.get(i)).getID()==ID)
   {
   ptt=(Participant)myvector.get(i);
   break;
   }
}
   return ptt;
}
public void writeInfo(Participant oneparticipant)throws Exception
{
   dos.writeUTF(8+"#"+1+"#"+String.valueOf(oneparticipant.getID()));
   dos.writeUTF(8+"#"+2+"#"+oneparticipant.getName());
   dos.writeUTF(8+"#"+3+"#"+oneparticipant.getLovelyname());
   dos.writeUTF(8+"#"+4+"#"+oneparticipant.getMotto());
   dos.writeUTF(8+"#"+5+"#"+oneparticipant.getIsonline());
   dos.writeUTF(8+"#"+6+"#"+oneparticipant.getHeadphoto());
   dos.writeUTF(8+"#"+7);
}
   public boolean lookNameAndPassword(String s1,String s2)
   {
    boolean bln=false;
     for(int i=0;i<myvector.size();i++)
      if(((Participant)myvector.get(i)).getName().equals(s1.trim())&&((Participant)myvector.get(i)).getPassword().equals(s2.trim()))
        {
          bln=true;
          yourparticipant=(Participant)myvector.get(i);
          break;
        }  
        return bln;
}
}

class MyFrame extends JFrame implements ActionListener
{
JTextArea area1;
JTextArea area2;
JButton stop;
public MyFrame()
{
   super("GameServer");
   area1=new JTextArea();
   area1.setPreferredSize(new Dimension(180,150));
   area1.setForeground(Color.green);
   area2=new JTextArea();
   area2.setPreferredSize(new Dimension(180,150));
   area2.setBackground(Color.white);
   area2.setForeground(Color.blue);
   stop=new JButton("stop");
   stop.setPreferredSize(new Dimension(80,40));
   stop.addActionListener(this);
   getContentPane().setLayout(new BorderLayout(10,10));
   getContentPane().add(area1,"West");
   getContentPane().add(area2,"Center");
   getContentPane().add(stop,"South");
   setSize(400,400);
   setVisible(true);
   setLocation(500,50);
   setDefaultCloseOperation(3);
}

public void actionPerformed(ActionEvent e)
{
   System.exit(0);
}
}  


类别:默认分类 | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu