基于loudmouth的XMPP客户端DEMO
2019独角兽企业重金招聘Python工程师标准>>>
这几天在公司一直在研究XMPP客户端软件DEMO的编写,下面的源代码是基于开源库loudmouth-1.4.3的XMPP客户端源代码。这段代码只是一个Demo,使用一个已经在服务器上注册了的用户登录到XMPP服务器,最后在XMPP服务器上发送iq信息,在客户端获取该IQ信息后解析该信息。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <loudmouth.h>
#define XMPP_SERVER "192.168.175.211"
#define XMPP_USERNAME "tm"
#define XMPP_PASSWORD "12345"
#define XMPP_RESOURCE "AndroidpnClient"
LmHandlerResult iq_handler_message(LmMessageHandler *handler, LmConnection *connection, LmMessage *m, gpointer user_data)
{
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;
}
转载于:https://my.oschina.net/linuxhunter/blog/35370
基于loudmouth的XMPP客户端DEMO相关推荐
- Profanity 0.3.1 发布,XMPP 客户端
Profanity 是一个最小化的基于控制台的 XMPP 客户端,灵感来自于 Irssi.提供桌面提醒.输入提醒和聊天记录.定制 UI和发送短网址的功能. http://static.oschina. ...
- 基于springboot的websocket服务端和客户端demo(简单易上手)
jdk 1.8 1.导入maven依赖 <!-- websocket服务端依赖 --> <dependency><groupId>org.springframewo ...
- 基于开源jabber(XMPP)架设内部即时通讯服务的解决方案
原文地址:基于开源jabber(XMPP)架设内部即时通讯服务的解决方案 作者:高傲的活着 Jabber 是著名的即时通讯服务服务器,它是一个自由开源软件,能让用户自己架即时通讯服务器,可以在Inte ...
- NIO框架入门(一):服务端基于Netty4的UDP双向通信Demo演示
2019独角兽企业重金招聘Python工程师标准>>> 前言 NIO框架的流行,使得开发大并发.高性能的互联网服务端成为可能.这其中最流行的无非就是MINA和Netty了,MINA目 ...
- Android平台基于asmack实现XMPP协议中的PubSub机制
Android平台基于asmack实现XMPP协议中的PubSub机制 本文主要介绍,在Android平台上基于asmack包,实现的PubSub机制,在PubSub中最重要的概念是节点Node,几乎 ...
- 常用的XMPP 客户端和服务端对比
XMPP协议的客户端软件列表: 参考链接 单一协议XMPP客户端 开放源代码软件 名称 平台 说明 Bombus Java ME(MIDP2.0)/WinCE Bombusmod Java ME( ...
- 基于Android微博整合客户端的设计与实现 毕业设计论文开题报告
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! < ...
- 《基于Android微博整合客户端的设计与实现》毕业设计论文开题报告
<基于Android微博整合客户端的设计与实现>毕业设计论文开题报告 说 明 1.开题报告是保证毕业设计(论文)质量的一个重要环节,为规范毕业设计的开题报告,特印发此表. 2.学生应 ...
- java r$_基于javacv的人脸检测Demo
[实例简介] 基于javacv的人脸检测Demo,参考文章:http://blog.csdn.net/viviwen123/article/details/6386302#reply [实例截图] [ ...
最新文章
- 更改Windows Server Core 2008计算机名字和配置网络连接
- idea 设置识别ini文件
- mysql参数优化51cto_超详细MySQL数据库优化
- python flask跨域_Flask配置Cors跨域的实现
- 数组和集合的区别?你还知道这些吗?
- app开发学习需要经历哪些流程
- 计算机广告制作未来发展还行吗,计算机多媒体设计专业和广告设计制作那个好...
- 初学大数据之如何选择机器学习算法
- KubeEdge 1.2.0 部署
- 数据库双机热备(代码实现)
- 线段树扫描线(1---算矩形的总面积)
- php 图片裁剪保存,PHP图片裁剪与缩放示例(无损裁剪图片)
- 节奏大师闪退android,安卓节奏大师进不去的原因以及详细解决办法
- hexo next主题配置
- 垃圾纸盒的叠法-超级实用
- 育碧首款区块链游戏真的来了吗?
- 怎么恢复oracle的包,Oracle的恢复管理器及DBMS_JOB包分析
- Inception 模块作用
- java批量删除日志文件_日志清理与文件批量删除
- .Net深入学习:序列化
热门文章
- 麻省理工开放官方课程项目!
- 深入浅出神经网络的改进方法!
- ELECTRA: 超越BERT, 19年最佳NLP预训练模型
- 1.3MB的超轻YOLO算法!全平台通用,准确率接近YOLOv3,速度快上45%丨开源
- 连续霸榜 Github!又有一个 Linux 神器出现了
- 论文LaTeX、项目README:无脑套用格式、开源模板最高10万赞
- Pandownload 下线了,我花了 30 分钟自己搭建了一个网盘
- PyTorch常用代码段整理合集,建议收藏!
- https://www.exploit-db.com/下载POC比较完善的代码
- 技术17期:近几年崛起的Pytorch究竟是何方神圣?