這幾天在公司一直在研究XMPP客戶端軟件DEMO的編寫,下面的源代碼是基於開源庫loudmouth-1.4.3的XMPP客戶端源代碼。這段代碼只是一個Demo,使用一個已經在服務器上註冊了的用戶登陸到XMPP服務器,最後在XMPP服務器上發送iq信息,在客戶端獲取該IQ信息後解析該信息。node
#include <stdio.h>服務器
#include <stdlib.h>socket
#include <unistd.h>oop
#include <loudmouth.h>spa
#define XMPP_SERVER "192.168.175.211"server
#define XMPP_USERNAME "tm"ssl
#define XMPP_PASSWORD "12345"ci
#define XMPP_RESOURCE "AndroidpnClient"get
LmHandlerResult iq_handler_message(LmMessageHandler *handler, LmConnection *connection, LmMessage *m, gpointer user_data)string
{
LmMessageNode *root_node, *node;
printf("[RECEIVED]:%s\n", lm_message_node_to_string(lm_message_get_node(m)));
root_node = lm_message_get_node(m);
node = lm_message_node_find_child(root_node, "title");
printf("title = %s\n", lm_message_node_get_value(node));
node = lm_message_node_find_child(root_node, "message");
printf("message = %s\n", lm_message_node_get_value(node));
node = lm_message_node_find_child(root_node, "uri");
printf("uri = %s\n", lm_message_node_get_value(node));
lm_message_node_unref(node);
lm_message_node_unref(root_node);
return LM_HANDLER_RESULT_REMOVE_MESSAGE;
}
int main(int argc, char **argv)
{
LmConnection *connection;
LmSSL *ssl;
GError *error = NULL;
LmMessage *m, *child_m, *response_m;
LmMessageNode *m_node, *child_node, *child;
gboolean use_sasl;
int i;
LmMessageHandler *iq_handler;
GMainLoop *main_loop;
//create a new closed connection
connection = lm_connection_new(XMPP_SERVER);
if (connection == NULL) {
printf("lm_connection_new(%s) error.\n", XMPP_SERVER);
return -1;
}
//connection uses starttls
ssl = lm_ssl_new(NULL, NULL, NULL, NULL);
lm_ssl_use_starttls(ssl, TRUE, FALSE);
lm_connection_set_ssl(connection, ssl);
//register iq message handler to the connection
iq_handler = lm_message_handler_new(iq_handler_message, NULL, NULL);
lm_connection_register_message_handler (connection, iq_handler, LM_MESSAGE_TYPE_IQ, LM_HANDLER_PRIORITY_NORMAL);
lm_message_handler_unref(iq_handler);
//open the connection to create socket with server
if (!lm_connection_open_and_block(connection, &error)) {
g_error("Failed to open:%s\n", error->message);
}
//check the open state of connection
if (lm_connection_is_open(connection)) {
printf("connection to %s [OPENED].\n", XMPP_SERVER);
} else {
printf("connection to %s [FAILED].\n", XMPP_SERVER);
}
//authenticate the specific user/password/resource to the server
if (!lm_connection_authenticate_and_block(connection, XMPP_USERNAME, XMPP_PASSWORD, XMPP_RESOURCE, &error)) {
g_error("Failed to authenticate:%s\n", error->message);
}
//check the authentication state of connection
if (lm_connection_is_authenticated(connection)) {
printf("[%s/%s/%s] is AUTHENTICATION.\n", XMPP_USERNAME, XMPP_PASSWORD, XMPP_RESOURCE);
} else {
printf("[%s/%s/%s] is AUTHENTICATION FAILED.\n", XMPP_USERNAME, XMPP_PASSWORD, XMPP_RESOURCE);
}
//send presence message to server to indicate online
m=lm_message_new(NULL, LM_MESSAGE_TYPE_PRESENCE);
if (!lm_connection_send(connection, m, &error)) {
printf("Failed to send:%s\n", error->message);
}
lm_message_unref(m);
//gogot glib main loop to receive server push message
main_loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (main_loop);
//close connection
lm_connection_close(connection, NULL);
lm_connection_unref(connection);
return 0;
}