论文无忧网提供:计算机毕业论文范文|计算机毕业设计|计算机毕业论文
栏目导航 ASP Java Web .NET VB6.0 JAVA VC VF DELPHI PB 计算机网络 计算机科学与技术 PHP 安卓APP 其他 C# 代写论文
当前位置: > 计算机 > 计算机科学与技术 >

一个简单的即时通讯工具的设计与开发(论文+程序)

4.1.2服务器端监听与转发消息代码

下边是服务器端的监听程序,如果发现有socket连接,就创建一个与该连接所对应的一个线程,该线程主要用来处理用户之间的信息交互。 内容来自论文无忧网 www.paper51.com

public void run()

paper51.com

    { 

copyright paper51.com

          try 内容来自www.paper51.com

       {  内容来自论文无忧网 www.paper51.com

             while(true)   //始终监听状态 http://www.paper51.com

           {  

paper51.com

               Socketclient=listen.accept(); 内容来自www.paper51.com

               //创建用户线程 内容来自论文无忧网 www.paper51.com

               PersonThread f=new PersonThread(client); paper51.com

               f.setPriority(Thread.MIN_PRIORITY); 

内容来自www.paper51.com

               f.start();

copyright paper51.com

                 copyright paper51.com

           } http://www.paper51.com

        }

copyright paper51.com

        Catch(IOExceptione)

copyright paper51.com

       { 

paper51.com

             System.err.println("Erro:"+e); http://www.paper51.com

           System.exit(1); paper51.com

        }

paper51.com

    }

http://www.paper51.com

       内容来自www.paper51.com

    //服务器端的消息转发函数 copyright paper51.com

    public static voidbroadcast(String msg,String hisJICQ)

paper51.com

    {   

内容来自论文无忧网 www.paper51.com

          int i;    内容来自www.paper51.com

          PersonThread you; paper51.com

       for(i=0;i<connections.size();i++) 内容来自论文无忧网 www.paper51.com

       { 

内容来自www.paper51.com

             you=(PersonThread)chatserverthree.connections.elementAt(i); 内容来自www.paper51.com

             if(you.JICQ.equals(hisJICQ))

paper51.com

               you.send(msg);                   内容来自www.paper51.com

        } copyright paper51.com

}

内容来自www.paper51.com

4.1.3服务端单线程上线处理代码 copyright paper51.com

下边是服务器端上的单线程的处理代码,主要通过网络流来读取客户端发送的消息,然后将消息分解,用来区分消息的类型,譬如是群消息还是个人消息等。

内容来自论文无忧网 www.paper51.com

       While(true) paper51.com

           { 

http://www.paper51.com

               line=in.readUTF();                                                                                                           System.out.println(line);

内容来自www.paper51.com

              //将读取的信息按照特定格式进行拆分

paper51.com

              StringTokenizer stk=new StringTokenizer(line,"#");             //PEOPLE+"#"+JICQ

内容来自www.paper51.com

               if(stk.hasMoreElements())      key=(String)stk.nextElement(); 内容来自论文无忧网 www.paper51.com

               //用户上线提示         

内容来自www.paper51.com

               if(key.equals("PEOPLE")) paper51.com

               {   

paper51.com

                  try{   StringJNUM=(String)stk.nextElement();

内容来自www.paper51.com

                              this.JICQ=JNUM; http://www.paper51.com

                              for(inti=0;i<chatserverthree.connections.size();i++)

copyright paper51.com

 {PersonThreadtt=(PersonThread)chatserverthree.connections.get(i);                           if(tt.JICQ.equals(this.JICQ)) http://www.paper51.com

                                { 内容来自www.paper51.com

                                                      chatserverthree.broadcast("RELOG#YES",tt.JICQ);

内容来自www.paper51.com

                                       chatserverthree.connections.remove(i); paper51.com

                                       tt.stopThread(); 内容来自www.paper51.com

                                       break;

内容来自www.paper51.com

                                }} 内容来自论文无忧网 www.paper51.com

                     //将该线程加入到线程向量中

内容来自论文无忧网 www.paper51.com

                             chatserverthree.connections.addElement(this); paper51.com

                              Friendsf=new Friends(); copyright paper51.com

                              ResultSetrs=f.selectAddMePerson(JNUM);

copyright paper51.com

                              ArrayListflist=new ArrayList(50); 内容来自论文无忧网 www.paper51.com

                              while(rs.next()) 内容来自论文无忧网 www.paper51.com

                              {

内容来自www.paper51.com

                                     flist.add(rs.getString(1));

copyright paper51.com

                              }

copyright paper51.com

                              for(inti=0; i<chatserverthree.connections.size();i++) 内容来自www.paper51.com

                              {

paper51.com

                              PersonThreadpt=(PersonThread)(chatserverthree.connections.elementAt(i)); paper51.com

                                     if(flist.contains((String)pt.JICQ))             

paper51.com

                                     {

内容来自论文无忧网 www.paper51.com

                                            chatserverthree.broadcast(line,pt.JICQ); 内容来自www.paper51.com

                            }} http://www.paper51.com

                              s_MyDetailssMy=new s_MyDetails();

paper51.com

                      //从信息表中读取离线信息,然后将之删除 内容来自论文无忧网 www.paper51.com

                              ResultSetrsMessage=sMy.isHaveMessage(JNUM); copyright paper51.com

                             if(rsMessage!=null) 内容来自论文无忧网 www.paper51.com

                             while(rsMessage.next())

http://www.paper51.com

                             {

内容来自论文无忧网 www.paper51.com

                                 intFriendJICQ=rsMessage.getInt("FriendJICQ"); paper51.com

                                intMyJICQ=rsMessage.getInt("MyJICQ");

http://www.paper51.com

                         StringmsgHead=rsMessage.getString("MessageHead");

paper51.com

                         StringmsgBody=rsMessage.getString("MessageContent");

内容来自论文无忧网 www.paper51.com

                                intflag=rsMessage.getInt("flag");

paper51.com

                                StringAllmsg=""; copyright paper51.com

                                if(flag==1)

paper51.com

Allmsg="MSGONE#"+FriendJICQ+"#"+MyJICQ+"#"+msgHead+"#"+msgBody;                else Allmsg="ADDFRIEND#"+FriendJICQ+"#"+MyJICQ+"#"+msgHead+"#"+msgBody; copyright paper51.com

                               chatserverthree.broadcast(Allmsg,JNUM);      copyright paper51.com

                             } sMy.delOldMessage(JNUM);                                                       }}

http://www.paper51.com

4.2 系统运行的效果图 4.2.1 登录界面

paper51.com

该界面提供用户输入自己注册号码和相应的密码,然后登录系统,如果还没有注册帐号,可以点“申请”进行帐号的申请。如下图所示:

http://www.paper51.com

内容来自www.paper51.com

图15 登录界面 copyright paper51.com

------分隔线----------------------------
联系方式