PJSIP-PJLIB-Socket
As we all know,most our projects are need to use the socket to programme.Use socket we can connect our device to others and our client to the Internet,so it's made our product more powerful.Now,let's begin the key part-pjlib socket.
The date types and functions are too much,if you need some of them,just click the links.
http://www.pjsip.org/docs/latest/pjlib/docs/html/page_pjlib_sock_test.htm
test1:get hostname and host address
//PJLIP I/O
//Socket test->get hostname and hostaddress
//heat nan
#include<pjlib.h>
int main()
{
pj_status_t status;
pj_in_addr hostaddr; //This structure describes Internet address.
// dada fields pj_uint32_t s_addr. The 32bit IP address.
unsigned char *hostaddr_str;
const pj_str_t *hostname;
//pj_init
status=pj_init();
if(status!=PJ_SUCCESS)
{
PJ_LOG(,(" ","init failed!"));
}
//gethostname
hostname=pj_gethostname();
if(!hostname||!hostname->ptr||!hostname->slen)
{
PJ_LOG(,( "gethostname","faild"));
}
else
{
PJ_LOG(,("gethostname","the hostname is %s",hostname->ptr));
}
hostaddr=pj_gethostaddr();
if(hostaddr.s_addr)
{
hostaddr_str=pj_inet_ntoa(hostaddr);//function pj_in_addr -> char *
//Convert an Internet host address given in network byte order to string in standard numbers and dots notation.
PJ_LOG(,("gethostaddress","%s",hostaddr_str));
}
else
{
PJ_LOG(,("gethostaddress","failed"));
} pj_shutdown();
getchar();//show the result before you enter any key
}
get hostname
test2:sendto and recv message use udp;
// PJLIB I/O UDP test
//heat nan
//server
#include<pjlib.h>
#define UDP_PORT 6000
#define ADDRESS "127.0.0.1"
#define N 100
int main( )
{
pj_status_t status;
pj_sockaddr_in addr;
int length=sizeof(addr);
pj_sock_t cs;
pj_sock_t ss;
pj_sockaddr_in daddr;
pj_sockaddr_in saddr;
pj_str_t s;
pj_str_t* IP_Addr;
char recvbuff[N+];
char sendbuff[N+];
pj_ssize_t len1;
status=pj_init();
if(status!=PJ_SUCCESS)
{
PJ_LOG(,("pj_init","failed"));
} //now we creat a socket ss
status=pj_sock_socket(pj_AF_INET(),pj_SOCK_DGRAM(),,&ss);
if(status!=)
{
PJ_LOG(,("creat ss socket","failed"));
}
//now we creat a socket cs
status=pj_sock_socket(pj_AF_INET(),pj_SOCK_DGRAM(),,&cs);
if(status!=)
{
PJ_LOG(,("creat cs socket","failed"));
} pj_bzero(&daddr,sizeof(daddr));
daddr.sin_family=pj_AF_INET();
daddr.sin_port=pj_htons(UDP_PORT);
IP_Addr=pj_cstr(&s,ADDRESS);
daddr.sin_addr=pj_inet_addr(IP_Addr); status=pj_sock_bind(ss,&daddr,sizeof(daddr));
if(status!=)
{
PJ_LOG(,("pj_sock_bind ","bind ss failed"));
} /*
pj_bzero(&saddr,sizeof(saddr));
saddr.sin_family=pj_AF_INET();
saddr.sin_port=pj_htons(UDP_PORT-1);
IP_Addr=pj_cstr(&s,ADDRESS);
saddr.sin_addr=pj_inet_addr(IP_Addr); status=pj_sock_bind(cs,&saddr,sizeof(saddr));
if(status!=0)
{
PJ_LOG(3,("pj_sock_bind ","bind cs failed"));
}
*/
pj_create_random_string(sendbuff, N);
sendbuff[N-] = '\0';
PJ_LOG(,("string","%s",sendbuff));
len1=sizeof(sendbuff); //pj_sock_sendto: Transmit data to the socket to the specified address.
status=pj_sock_sendto(cs,sendbuff,&len1,,&daddr,sizeof(daddr));
if(status!=PJ_SUCCESS)
{
PJ_LOG(,("sendto","failed"));
} pj_bzero(&addr,sizeof(addr));
//pj_sock_recv: Receives data stream or message coming to the specified socket.
status=pj_sock_recv(ss,recvbuff,&len1,);
if(status!=PJ_SUCCESS)
{
PJ_LOG(,("recv","failed"));
}
else
{
PJ_LOG(,("content","%s",recvbuff));
}
pj_shutdown();
getchar(); }
test3:udp test: the client and server
In this part I wanna write a program like the classical UDP socket demo,that is to say a simple demo one person send message and the other receive the message,they do it by turn.
But when I do that I find there are some differences between the formal socket and the pjlib socket.
Some function I used like in C/C++ steps,but not successed,such as recvfrom and send.If someone who knows that please tell me,thank you!
In my project,there still have a problem.That is not only the client need the server's IP,but the server needs the client too.
//UDP test
//server
//heat nan
//notice: the client should send the message first,then the server.And every time the size of the message you send should not too big!
#include<pjlib.h>
#define UDP_PORT 6000
#define ADDRESS "127.0.0.1"
#define N 100
#define M 50
int main()
{
pj_status_t status;
pj_sock_t cs;
pj_sock_t ss;
pj_sockaddr_in daddr,saddr;
pj_str_t s;
pj_str_t* IP_Addr;
char sendbuff[M+],recvbuff[N+];
pj_ssize_t len1,len2;
status=pj_init();
if(status!=PJ_SUCCESS)
{
PJ_LOG(,("pj_init","failed"));
} status=pj_sock_socket(pj_AF_INET(),pj_SOCK_DGRAM(),,&ss);
if(status!=)
{
PJ_LOG(,("creat ss socket","failed"));
} pj_bzero(&daddr,sizeof(daddr));
daddr.sin_family=pj_AF_INET();
daddr.sin_port=pj_htons(UDP_PORT);
IP_Addr=pj_cstr(&s,ADDRESS);
daddr.sin_addr.s_addr=pj_htonl(PJ_INADDR_ANY); status=pj_sock_bind(ss,&daddr,sizeof(daddr));
if(status!=)
{
PJ_LOG(,("pj_sock_bind ","bind ss failed"));
} pj_bzero(&saddr,sizeof(saddr));
saddr.sin_family=pj_AF_INET();
saddr.sin_port=pj_htons(UDP_PORT-);
IP_Addr=pj_cstr(&s,ADDRESS);
saddr.sin_addr=pj_inet_addr(IP_Addr); while(){
len1=N;
status=pj_sock_recv(ss,recvbuff,&len1,);
if(status==PJ_SUCCESS)
{
PJ_LOG(,("recv","success"));
PJ_LOG(,("context","%s",recvbuff));
} else
{
printf("failed\n");
} gets(sendbuff);
len2=sizeof(sendbuff);
// PJ_LOG(3,("string","%s",sendbuff));
status=pj_sock_sendto(ss,sendbuff,&len2,,&saddr,sizeof(saddr));
if(status!=PJ_SUCCESS)
{
PJ_LOG(,("send","failed"));
}
else
{ } } getchar();
return ;
}
server
//UDP test
//Client
//heat nan
#include<pjlib.h>
#define UDP_PORT 6000
#define ADDRESS "127.0.0.1"
#define N 50
#define M 100
int main()
{
pj_status_t status;
pj_ssize_t len1,len2;
pj_sock_t cs,ss;
pj_sockaddr_in daddr,saddr;
pj_str_t s;
pj_str_t* IP_Addr;
char recvbuff[M+],sendbuff[N+];
status=pj_init(); if(status!=PJ_SUCCESS)
{
PJ_LOG(,("pj_init","failed"));
} status=pj_sock_socket(pj_AF_INET(),pj_SOCK_DGRAM(),,&cs);
if(status!=)
{
PJ_LOG(,("creat cs socket","failed"));
} pj_bzero(&daddr,sizeof(daddr));
daddr.sin_family=pj_AF_INET();
daddr.sin_port=pj_htons(UDP_PORT);
IP_Addr=pj_cstr(&s,ADDRESS);
daddr.sin_addr=pj_inet_addr(IP_Addr); pj_bzero(&saddr,sizeof(saddr));
saddr.sin_family=pj_AF_INET();
saddr.sin_port=pj_htons(UDP_PORT-);
IP_Addr=pj_cstr(&s,ADDRESS);
saddr.sin_addr=pj_inet_addr(IP_Addr); status=pj_sock_bind(cs,&saddr,sizeof(saddr));
if(status!=)
{
PJ_LOG(,("pj_sock_bind ","bind ss failed"));
} /*
len1=N;
pj_create_random_string(sendbuff, N);
sendbuff[N-1] = '\0';
*/
while(){
gets(sendbuff);
len1=sizeof(sendbuff);
// PJ_LOG(3,("string","%s",sendbuff));
status=pj_sock_sendto(cs,sendbuff,&len1,,&daddr,sizeof(daddr));
if(status!=PJ_SUCCESS)
{
PJ_LOG(,("sendto","failed"));
} len2=M;
status=pj_sock_recv(cs,recvbuff,&len2,);
if(status!=PJ_SUCCESS)
{
PJ_LOG(,("recvfrom","failed"));
}
else
{
PJ_LOG(,("context","%s",recvbuff));
}
} getchar(); }
client
PJSIP-PJLIB-Socket的更多相关文章
- [pjsip]Pjlib中配置文件config.h解析
config_site.h 这个头文件包含在config.h中,用于引入平台?(site)/用户特定的配置以控制PJLIB的特性,用户需要自己生成这个文件. 譬如说我们要把PJLIB编译成DLL,那么 ...
- [pjsip]Pjlib中的链表结构
Pjlib的链表结构跟常见的链表结构有所区别,如下图所示: 图1:一般链表结构 图2:pjlib中的链表结构 可以看到一般的双向链表是链表节点包含数据域,而pjlib中是数据域包含链表节点.一般的链表 ...
- PJSIP Socket 模型
前些日子为解决项目中问题调试到PJSIP Socket收发数据部分 ,好记性不如烂笔头,记录下 PJSIP 使用的Socket 模型 ,以备后用. 不同平台下PJSIP采用不同的 Socket模型,W ...
- QT开发pjsip的VOIP,A8平台运行
QT开发pjsip的VOIP 开发环境 平台:A8 环境:Linux-3.0.8 实现功能:使用QT开发VOIP进行初始化.拨号.挂起 测试工具:minisipserver服务器 效果 界面: min ...
- exosip 和 pjsip 简介
oSIP oSIP的开发开始于2000年7月,第一个版本在2001年5月发 布,到现在已经发展到3.x了.它采用ANSI C编写,而且结 构简单小巧,所以速度特别快,它并不提供高层的SIP会话 控制 ...
- 介绍一个开源的SIP(VOIP)协议库PJSIP
本文系转载,出处不可考. 假设你对SIP/VoIP技术感兴趣,哪希望你不要错过:),假设你对写出堪称优美的Code感兴趣 ,那么你也不可错过:) 这期间我想分析一下一个实际的协议栈的设计到实现的相关技 ...
- pjlib深入剖析和使用详解
1. PJSIP简介 PJSIP的实现是为了能在嵌入式设备上高效实现SIP/VOIP.其主要特征包括: 1).极具移植性.(Extremely portable) ...
- PJSIP开源库详解
PJSIP是一个包含了SIP.SDP.RTP.RTCP.STUN.ICE等协议实现的开源库.它把基于信令协议SIP的多媒体框架和NAT穿透功能整合成高层次.抽象的多媒体通信API,这套API能够很容易 ...
- PJSIP开发指南
一.通用设计 1.1 架构 1.1.1 通信图 下面的图展示了SIP消息在PJSIP组件间从后端到前端如何传递的. 1.1.2 类图 下面的图显示类视图 1.2 E ...
随机推荐
- vos设置可呼出手机或固话
问题: 默认公司只让呼出手机号码,但有的客户要求能打固话,怎么办? 落地网关——补充设置——落地前缀——落地被叫改写规则 在改写规则里添加固话号段即可 具体案例: 5201——1表示让520号段只能拨 ...
- COGS 2091. Asm.Def的打击序列
★★★ 输入文件:asm_lis.in 输出文件:asm_lis.out 简单对比时间限制:4 s 内存限制:256 MB [题目描述] 白色圆柱形的“蓝翔”号在虚空中逐渐变大,一声沉 ...
- CToolTipCtrl使用详细解说
很多的界面设计都需要有Tip提示,下面描述一下Tip的简单使用方法: 1. 首先要New一个CToolTipCtrl的对象m_pContentTip 2. 调用CToolTipCtrl的create函 ...
- java——类加载机制
类加载机制 JVM把class文件加载的内存,并对数据进行校验.转换解析和初始化,最终形成JVM可以直接使用的Java类型的过程就是加载机制. 类从被加载到虚拟机内存中开始,到卸载出内存为止,它的生命 ...
- linux下composer+laravel随笔
1.composer中文网:https://www.phpcomposer.com/ laravel中文网:https://d.laravel-china.org/ 2.composer是的作用是 ...
- NO.003-2018.02.08《江城子·乙卯正月二十日夜记梦》宋代:苏轼
江城子·乙卯正月二十日夜记梦_古诗文网 江城子·乙卯正月二十日夜记梦 乙卯:公元1075年,即北宋熙宁八年. 宋代:苏轼 十年生死两茫茫,不思量,自难忘.千里孤坟,无处话凄凉.纵使相逢应不识,尘满面, ...
- 如何使用Win32API绘制树
一.这个已经有几年时间了,刚开始学习charlie的<windows程序设计>的时候做的.现在看来,代码很乱,虽然后来还整理过几次,现在这方面没什么兴趣了,有兴趣的可自由下载. 二.绘制二 ...
- robotframework实战二---Jenkins连用
1.下载插件robot Jenkins环境搭建就不用说了,网上有很多帖子,你在使用时,你需要做以下几步 因为目前我已经安装了 2.新建项目 因为有重名的项目,所以会提示以下内容 你需要配置的内容就两处 ...
- javascript中的作用域与作用域链
前几天,在写一段js代码时,出现了一些问题,调了很长时间也没有调通,其原因是,我在处理变量的作用域时错误地沿用了C++的作用域机制.因此我回炉了一次. 如果你使用过C++或java等一系列的面向对象的 ...
- Eclipse 修改默认工作空间
第一次启动Eclipse时会弹出对话框,让你进行Workspace Launcher,也就是设置Eclipse的项目存放路径.但是,当你勾选“Use this as the default and d ...