eXosip 学习案例
一个用eXosip实现的UAC和UAS的例子
经过一段时间的学习,对sip总算有了一点认识,在学习过程中,遇到了太多的问题,郁闷过,惆怅过,但是一咬牙,还是过来了。令我感动的是,在网上遇到一些很热心的朋友,不厌其烦地给我以解惑,感谢他们,尤其是友善的大狗,呵呵,希望将来有一天他能看到这篇文章。
我是利用eXosip协议栈进行开发的,网上有一篇<一个简单的sip呼叫例子>,写的不错,但是好像有一些问题,而对于初学者来说,能拿到一个好的例子,对sip的理解可以到达事半功倍的效果。于是便把自己的写的例子拿出来,让大家参考一下,若有问题,欢迎指正。 只需把里面的IP地址改正、编译即可使用。
/****************************************** 编译方法:
gcc xxx.c -o xxx -leXosip2
****************************************/
/*******************UAS***************************************************** 本文可以任意转载,但必须保留出处 作者:rainfish
网址:http://blog.csdn.net/bat603/ 测试环境:eXosip3.0.1/redhat AS 4
***************************************************************************/ #include
main (int argc, char *argv[]) {
eXosip_event_t *je = NULL; osip_message_t *ack = NULL; osip_message_t *invite = NULL; osip_message_t *answer = NULL; sdp_message_t *remote_sdp = NULL;
int call_id, dialog_id; int i,j; int id;
char *sour_call = \
char *dest_call = \
char command;
eXosip 学习案例
char tmp[4096]; char localip[128];
int pos = 0;
//初始化sip i = eXosip_init (); if (i != 0) {
printf (\ return -1; } else {
printf (\ }
i = eXosip_listen_addr (IPPROTO_UDP, NULL, 5060, AF_INET, 0); if (i != 0) {
eXosip_quit ();
fprintf (stderr, \ }
for(;;) {
//侦听是否有消息到来
je = eXosip_event_wait (0,50);
//协议栈带有此语句,具体作用未知 eXosip_lock ();
eXosip_default_action (je); eXosip_automatic_refresh (); eXosip_unlock ();
if (je == NULL)//没有接收到消息 continue;
// printf (\ switch (je->type) {
case EXOSIP_MESSAGE_NEW://新的消息到来 printf (\
if (MSG_IS_MESSAGE (je->request))//如果接受到的消息类型是MESSAGE {
eXosip 学习案例
{
osip_body_t *body;
osip_message_get_body (je->request, 0, &body); printf (\
//printf (\ }
//按照规则,需要回复200 OK信息
eXosip_message_build_answer (je->tid, 200,&answer); eXosip_message_send_answer (je->tid, 200,answer); } break;
case EXOSIP_CALL_INVITE:
//得到接收到消息的具体信息
printf (\a INVITE msg from %s:%s, UserName is %s, password is %s\\n\ je->request->req_uri->port, je->request->req_uri->username, je->request->req_uri->password);
//得到消息体,认为该消息就是SDP格式.
remote_sdp = eXosip_get_remote_sdp (je->did); call_id = je->cid; dialog_id = je->did;
eXosip_lock ();
eXosip_call_send_answer (je->tid, 180, NULL);
i = eXosip_call_build_answer (je->tid, 200, &answer); if (i != 0) {
printf (\ eXosip_call_send_answer (je->tid, 400, NULL); } else {
snprintf (tmp, 4096, \
\ \
\ \
//设置回复的SDP消息体,下一步计划分析消息体
//没有分析消息体,直接回复原来的消息,这一块做的不好。 osip_message_set_body (answer, tmp, strlen(tmp));
osip_message_set_content_type (answer, \
eXosip 学习案例
eXosip_call_send_answer (je->tid, 200, answer); printf (\ }
eXosip_unlock ();
//显示出在sdp消息体中的 attribute 的内容,里面计划存放我们的信息 printf (\
while (!osip_list_eol (remote_sdp->a_attributes, pos)) {
sdp_attribute_t *at;
at = (sdp_attribute_t *) osip_list_get (remote_sdp->a_attributes, pos);
printf (\这里解释了为什么在SDP消息体中属性a里面存放必须是两列
pos ++; } break;
case EXOSIP_CALL_ACK: printf (\
// printf (\ break;
case EXOSIP_CALL_CLOSED:
printf (\ // eXosip_call_build_ack(dialog_id, &ack); //eXosip_call_send_ack(dialog_id, ack);
i = eXosip_call_build_answer (je->tid, 200, &answer); if (i != 0) {
printf (\ eXosip_call_send_answer (je->tid, 400, NULL); } else {
eXosip_call_send_answer (je->tid, 200, answer); printf (\ } break;
case EXOSIP_CALL_MESSAGE_NEW://至于该类型和EXOSIP_MESSAGE_NEW的区别,源代码这么解释的 /*
/* request related events within calls (except INVITE) */
相关推荐: