XMPP客户端开发(1)--连接和登录

Smack可用于XMPP客户端的开发,下载Smack,将相关jar文件导入后,即可以开始XMPP客户端的开发。

以下代码实现了客户端连接Tigase服务器,并根据用户名和密码登录。

package Xmpp;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

/**
 * XMPP Client
 * @author    HZ
 * @since    2014-09-01
 * @version    1.0.0
 */
public class Client {
    
    public static XMPPConnection xmpp_conn;
    public static ConnectionConfiguration xmpp_conf;
    
    // 服务器IP
    public static String xmpp_ip = new String("10.3.93.213");
    // 服务器名
    public static String xmpp_host = new String("tsung213");
    // 服务器端口
    public static int xmpp_port = 5222;
    
    // 用户名和密码
    public static String user = new String("hz_12");
    public static String pass = new String("123456");

    public static void main(String[] args) {        
        conn();
    }
    
    // 连接服务器
    public static void conn(){
        
        try{
            // 配置连接 
            xmpp_conf = new ConnectionConfiguration(xmpp_ip, xmpp_port, xmpp_host);
            xmpp_conf.setReconnectionAllowed(true);      
            xmpp_conf.setSecurityMode(SecurityMode.disabled);       
            xmpp_conf.setSASLAuthenticationEnabled(false);
            xmpp_conf.setCompressionEnabled(false);
        
            // 连接,并根据用户名和密码登录
            xmpp_conn = new XMPPConnection(xmpp_conf);
            xmpp_conn.DEBUG_ENABLED = true;
            xmpp_conn.connect();
            xmpp_conn.login(user, pass);
            
            // 获取相关变量
            String tmp;
            tmp = xmpp_conn.getConnectionID();
            System.out.println("ConnectionID:" + tmp);
            tmp = xmpp_conn.getHost();
            System.out.println("Host:" + tmp);
            tmp = xmpp_conn.getServiceName();
            System.out.println("ServiceName:" + tmp);
            tmp = xmpp_conn.getUser();
            System.out.println("User:" + tmp);
        }
        catch (XMPPException e){
            System.out.println("Error:" + e.toString());
        }
        
    }

}

运行结果:

posted @ 2014-09-01 16:19  MSTK  阅读(4081)  评论(1编辑  收藏  举报