⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qqliao.txt

📁 聊天程序,是我从别的地方找来的,望大家好好的发扬
💻 TXT
字号:
百度首页 | 百度空间 | 登录 运分享美好人生,共创锦绣前程! 主页博客相册|个人档案 |好友   查看文章    
QQ聊天室服务器端源代码2007-09-10 18:59QQ聊天室服务器端源代码2007-01-29 08:48//package liaotian; 
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.*;

//用户登陆信息序列化
class Login1 extends Object implements Serializable
{
String logName;
String logPassword;
}
//用户注册信息序列化
class Register1 extends Object implements Serializable
{
String userName;
String userPassword;
}
//用于发送聊天和在线用户的信息  
class Message1 implements Serializable
{
    Vector userOnLine;
    Vector chat;
}
//用户退出信息序列化
class Exit1 extends Object implements Serializable
{
String exitName;
}
//聊天信息序列化
class Chat1 implements Serializable
{
String chatUser;
String chatMessage;
String chatToUser;
String whisper;
}  


///////////////应用程序开始//////////////////
public class server extends Thread
{
ServerSocket serverSocket;                 //申明套接字
static Vector user = new Vector(1,1);      //保存在线用户信息
static Vector chat = new Vector(1,1);      //保存聊天信息

public server()
{
   //将在线用户信息及聊天信息初始化
   user.addElement("所有人");
   chat.addElement(" ");
  
   try
   {
    serverSocket = new ServerSocket(1001);    //定义端口号为1001
    //获取服务器的主机名和IP地址
    InetAddress address = InetAddress.getLocalHost();      
           System.out.println(address);
           System.out.println("程序获取的主机名称:"+address.getHostName());
           System.out.println("程序获取的IP地址:"+address.getHostAddress());
    //服务器启动时的输出
    System.out.println("☆★☆★☆★ 寻梦聊天室   ☆★☆★☆★");
    System.out.println("   ");
    System.out.println("服务器启动........");
   }
  
   catch(IOException e)
   {
    fail(e,"无法启动服务器!");
   }
  
   //调用 run() 方法
   this.start();
}

//定义异常输出格式
public static void fail(Exception e,String str)
{
   System.out.println(str + "." + e);
}

public void run()
{
   try
   {
    while(true)
    {
     //使用serverSocket类的accept()方法接收此连接
     Socket client = serverSocket.accept();
            
     //传递参数给 Connection 类的对象 
     Connection con = new Connection(client,user,chat);  
    }
   }
  
   catch(IOException e)
   {
    fail(e,"Not listening");
   }
}

public static void main(String arg[])
{
   new server();
}
}


////////////线程类///////////////
class Connection extends Thread
{
//定义各类变量
Socket netClient;
Vector onLine;
    Vector chatMessage;
    ObjectInputStream fromClient;
    ObjectOutputStream toClient;
    PrintStream strToClient;
   
    FileInputStream fileIn;
    ObjectInputStream objIn;
   
    FileOutputStream fileOut;
    ObjectOutputStream objOut;
   
    //定义关键字
    protected String strLogin = "Login1";
    protected String strRegister = "Register1";
    protected String strMessage = "Message1";
    protected String strExit = "Exit1";
    protected String strChat = "Chat1";
   
    Object obj;
   
  
    public Connection(Socket client,Vector u,Vector c)
    {
     netClient = client;
     onLine = u;
       chatMessage = c;
       try
       {
        //实现双向通信
        fromClient = new ObjectInputStream(netClient.getInputStream());
          strToClient=new PrintStream(netClient.getOutputStream());
       }
      
       catch(IOException e)
       {
        try
        {
         netClient.close();     //如果产生错误,则关闭线程
        }
       
        catch(IOException ex)
        {
         System.out.println("不能传输流!" + ex);
         return;
        }
       }
      
       //调用run()方法启动线程
       this.start();     
    }
   
    public void run()
    {
     try
     {
        obj = fromClient.readObject();      //读取从客户端传来的对象流
         
       
      if(obj == null)
      return;
     
      //根据所传递来的消息类名来选择相应的方法
      if(obj.getClass().getName().compareTo(strLogin) == 0)
      {
       userLogin();
      }
     
      if(obj.getClass().getName().compareTo(strRegister) == 0)
      {
       userRegister();
      }
     
     if(obj.getClass().getName().compareTo(strMessage) == 0)
     {
       userMessage();
     }
     
      if(obj.getClass().getName().compareTo(strExit) == 0)
      {
       userExit();
      }
     
      if(obj.getClass().getName().compareTo(strChat) == 0)
      {
       addChat();
      }
     }
     catch(IOException e)
     {
     }
       catch(ClassNotFoundException e)
       {
       }
    }

   
    ///////////////用户登陆验证///////////////
    public void userLogin()
    {
     try
     {
      //读文件中的信息
      Vector temp;                                //定义一个临时矢量
      fileIn = new FileInputStream("user.txt");   //使用文件读取流读取注册记录文本文档到fileIn
      objIn = new ObjectInputStream(fileIn);      //使用对象流将fileIn读取到objIn      
      temp = (Vector)objIn.readObject();          //将对象赋给矢量类 
     
      Login1 logObj = (Login1)obj;    
       
      //判断登陆信息的是否正确
      for(int i = 1; i < temp.size();i ++)
      {
       Register1 login_temp = (Register1)temp.elementAt(i);  
             
       //比较用户名和密码是否匹配
       if(logObj.logName.compareTo(login_temp.userName)==0)
       if(logObj.logPassword.compareTo(login_temp.userPassword)==0)
       {
        //匹配成功的话发送"ok"信息到客户端
        strToClient.println("ok");        
       
        //将登陆成功的用户名填加到在线用户矢量中      
        onLine.addElement(logObj.logName);  
       
        //服务器端输出显示
        String strmessage = "用户["+ logObj.logName +"] 登陆成功!\n";
        System.out.println(strmessage);
        break;
       }     
       else
       {
        //匹配不成功的话发送"no"信息到客户端
        strToClient.println("no");
        break;
       }
      
       if(i == (temp.size() - 1))
       {
        strToClient.println("no");
        System.out.println("用户尝试登陆未成功!");
       }
      }
       
      fileIn.close();
             objIn.close();
             fromClient.close();
     
     }
     catch(ClassNotFoundException e)
     {
      System.out.println(e);
     }
     catch(IOException e)
     {
      System.out.println(e);
     }
    }    
   
   
    ////////////////////用户注册//////////////////////////   
    public void userRegister()
    {
     try
     {
      Vector temp;                                //定义一个临时矢量
     
      fileIn = new FileInputStream("user.txt");   //使用文件读取流读取注册记录文本文档到fileIn
      objIn = new ObjectInputStream(fileIn);      //使用对象流将fileIn读取到objIn
      temp = (Vector)objIn.readObject();          //将对象赋给矢量类
     
      Register1 regObj = (Register1)obj;          //将注册信息序列化类实例化
     
      //验证用户名是否已有人使用
       for(int i = 1; i < temp.size(); i++)
       {
        Register1 reg_temp = (Register1)temp.elementAt(i);
                        if(regObj.userName.compareTo(reg_temp.userName)==0)
       {
         toClient.writeObject("no");
         break;
       }
      }
     
      //添加注册信息到临时向量
      temp.addElement(regObj);                   
      
      //将用户注册信息写入文本文档
      fileOut = new FileOutputStream("user.txt");
      objOut = new ObjectOutputStream(fileOut);
      objOut.writeObject(temp);
      strToClient.println("ok"); 
     
      //服务器端输出显示
      String strmessage = "用户[" + regObj.userName + "] 注册成功!";
      System.out.println(strmessage);
     
      //将流及线程关闭
      fileIn.close();
      objIn.close();
      fileOut.close();
      objOut.close();
      fromClient.close();
     }
     catch(ClassNotFoundException e)
     {
      System.out.println(e);
     }
     catch(IOException e)
     {
      try
      {
       Vector temp = new Vector(1,1);
       Register1 regObj = new Register1();
       temp.addElement(regObj);
       fileOut = new FileOutputStream("user.txt");
       objOut = new ObjectOutputStream(fileOut);
       objOut.writeObject(temp);
      
       strToClient.println("ok");
      
       fileOut.close();
               objOut.close();
           }
      catch(IOException ex)
      {
       System.out.println(e + "2");
      }  
     }
    }
   
    //发送聊天和用户在线信息到客户端   
    public void userMessage()
    {
     try
     {
     //新定义一个对象流
     toClient = new ObjectOutputStream(netClient.getOutputStream());
     
   //将矢量类的值赋给序列化对象
       Message1 messObj = new Message1();
       messObj.userOnLine = onLine;
       messObj.chat = chatMessage;
          
       //写出对象流
       toClient.writeObject(messObj);
          
       fromClient.close();
       toClient.close();
       }
       catch(IOException e)
       {
       }
    }
   
   
    //用户退出信息
    public void userExit()
    {
      Exit1 eObj = new Exit1();
      eObj = (Exit1)obj;
    
      if(eObj.exitName != null)
       {
              for(int i = 0; i < onLine.size(); i ++)
     {
      if(onLine.elementAt(i).equals(eObj.exitName))
       {
         onLine.removeElementAt(i);
         System.out.println("用户 ["+ eObj.exitName +"]退出聊天室! \n");
         break;
        }
       }
         }
       strToClient.close();
      }
   
    //增加聊天信息
    public void addChat()
    {
     //将接收到的对象值赋给聊天信息的序列化对象
     Chat1 cObj = new Chat1();
     cObj = (Chat1)obj;
    
     //将聊天信息的序列化对象填加到保存聊天信息的矢量中
     chatMessage.addElement((Chat1)cObj);
     return;
    }

 
 

类别:默认分类 | 添加到搜藏 | 浏览(27) | 评论 (0)  上一篇:用js操作xml- [xml]    下一篇:如何使用spy ++ (How to use... 最近读者: 登录后,您就出现在这里。   
    177654144  
 网友评论:    发表评论:姓 名:     注册 | 登录 *姓名最长为50字节 
   
网址或邮箱:  (选填) 
   
内 容:   
   
验证码:  请输入下图中的四位验证码,字母不区分大小写。
 看不清?  
   

      

?2007 Baidu 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -