码迷,mamicode.com
首页 > 其他好文 > 详细

libstrophe发送图片

时间:2014-08-15 17:53:49      阅读:418      评论:0      收藏:0      [点我收藏+]

标签:des   style   os   io   for   ar   cti   line   

今天用libstrophe做了一个发送图片的,自己用了三个方法进行实现:感觉可能还是这个最好吧!之前两个CTO不想增加项目大小,被否决。这是用于将windows上的图片发送到ios手机项目中,所以需要自己去实现下。用过libevent、gloox库。下面是libstrophe库的实现。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "base64.h"
#include <strophe.h>
#include <iostream>
#include <fstream>
struct msgdata
{
char* from;
char* to;
//long length;
char* msg;
};
/** Send a single text message.
 *  This function sends a simple text message to a given recipient.
 *
 *  @param conn a Strophe connection object
 *  @param to the JID of the recipient
 *  @param message the text of the message to send
 *
 *  @ingroup Connections
 */
void xmpp_send_simple_message(xmpp_conn_t* conn,const char* const from,const char* const to,const char* const message)
{
xmpp_stanza_t *body,*text,*command;
xmpp_ctx_t *ctx = xmpp_conn_get_context(conn);
char* rePath = "aaa.jpg";
command = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(command,"message");
xmpp_stanza_set_type(command,"file");
xmpp_stanza_set_attribute(command,"from",from);
xmpp_stanza_set_attribute(command,"to",to);
xmpp_stanza_set_attribute(command,"destin","/");

body = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(body,"body");


xmpp_stanza_t *item =xmpp_stanza_new(ctx);
xmpp_stanza_set_name(item,"item");
    xmpp_stanza_set_attribute(item, "path", rePath);



text = xmpp_stanza_new(ctx);
    xmpp_stanza_set_text(text,message);
//xmpp_stanza_add_child(body,text);
xmpp_stanza_add_child(item,text);
xmpp_stanza_add_child(body,item);


xmpp_stanza_add_child(command,body);


xmpp_send(conn,command);
xmpp_stanza_release(command);
}
/*define a handler for connection events*/
void conn_handler(xmpp_conn_t* const conn,const xmpp_conn_event_t status,const int error,xmpp_stream_error_t* const stream_error,void* const userdata)
{
xmpp_ctx_t *ctx = xmpp_conn_get_context(conn);
struct msgdata* msg = (struct msgdata*)userdata;


if(status ==XMPP_CONN_CONNECT)
{
xmpp_stanza_t* pres;
fprintf(stderr,"DEBUG:connected\n");


/*send initial <presence/> so that we appear online to contacts*/
pres = xmpp_stanza_new(ctx);
//xmpp_handler_add(conn,presence_handler, NULL, "presence", NULL, ctx);
xmpp_stanza_set_name(pres,"presence");
xmpp_send(conn,pres);
xmpp_stanza_release(pres);


/*send a message*/
xmpp_send_simple_message(conn,msg->from,msg->to,msg->msg);
free(msg);


/*cause the conn_handler to be executed,where the event loop gets shut down*/
xmpp_disconnect(conn);
}else
{
fprintf(stderr,"DEBUG:disconnect\n");
xmpp_stop(ctx);
}
}
int main(int argc,char** argv)
{
xmpp_ctx_t *ctx;
xmpp_conn_t *conn;
xmpp_log_t *log;
char *jid,*pass;
struct msgdata* msg = (struct msgdata*)malloc(sizeof(struct msgdata));
msg->to=  "test1@192.168.11.206/";
msg->from= "test@192.168.11.206/";
jid =msg->from;
pass = "test";


/*initalize library*/
xmpp_initialize();


/*create a context*/
log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG);
ctx = xmpp_ctx_new(NULL,log);


/* create a connection*/
conn = xmpp_conn_new(ctx);


/* setup authentication information*/
xmpp_conn_set_jid(conn,jid);
xmpp_conn_set_pass(conn,pass);


/* prepare the message for sending*/


std::ifstream file;
file.open("C:/Users/Administrator/Desktop/2.jpg",std::ios_base::in|std::ios_base::binary|std::ios_base::ate);
file.seekg(0,std::ios::end);
char *buffer ;
long size;
size = file.tellg();
buffer = new char [size];
file.seekg(0,std::ios::beg);
file.read (buffer, size);


std::string picture = base64_encode(reinterpret_cast<const unsigned char*>(buffer), size);
msg->msg = const_cast<char*>(picture.c_str());

/* initialize connection*/
xmpp_connect_client(conn,NULL,0,conn_handler,msg);


/* enter the event loop - our connect handler will trigger an exit*/
xmpp_run(ctx);
/*release our connection and contex*/
xmpp_conn_release(conn);
xmpp_ctx_free(ctx);


/*final shutdown of the libraty*/
xmpp_shutdown();
return 0;
}

libstrophe发送图片,布布扣,bubuko.com

libstrophe发送图片

标签:des   style   os   io   for   ar   cti   line   

原文地址:http://blog.csdn.net/fuyuehua22/article/details/38587627

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!