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 ...
随机推荐
- 3.tomcat
1.进入网站http://www.apache.org 2.选择 3.关闭防火墙才可以让别人访问自己
- C++ POD类型
POD( Plain Old Data)概念: Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to m ...
- SIP MGCP和H323的区别
在Windows中内置的NetMeeting就是典型的H.323协议客户端,而比较常见的SIP系统是微软开发的MSN Messenger系统.首先,由用户A向SIP服务器发出呼叫请求,请求的信息包含自 ...
- C#后台unxi时间戳转换为前台JS时间的方法
后台返回的时间是一个格式为 /Date(1530153274362)/ 的unxi时间戳前台转换代码:var matchResult = data.match(/(\d+)/);if (matchRe ...
- 前端高质量知识(五)-JS详细图解全方位解读this
在这之前,我们需要来回顾一下执行上下文. 在前面几篇文章中,我有好几个地方都提到执行上下文的生命周期,为了防止大家没有记住,再次来回顾一下,如下图. 执行上下文生命周期 在执行上下文的创建阶段,会分别 ...
- 轻量级HTTP服务器Nginx(入门与安装篇)
轻量级HTTP服务器Nginx(入门篇) 文章来源于南非蚂蚁 一.什么是Nginx 相信很多读者都对Apache非常熟悉,与Apache类似,Nginx是一款高性能的HTTP和反向代理服务器软 ...
- 2017.9.18 HTMl学习总结----input标签的额type
2.1.3 HTML表单标签与表单设计 (1)表单的组成:文本框(text),密码框(password),多行文本框(Multiline text box). 单选按钮框(Single - rad ...
- iphone应用程序生命周期浅析
做iphone开发有必要知道iphone程序的生命周期,说白点就是当点击程序图标启动程序开始到退出程序整个使用运行过程中底下的代码都发生了什么,只有理解生命周期,有利于我们开发人员开发出更棒的应用 接 ...
- javabeans 内省 introspector BeanUtils
javaBeans 属性的概念 不只是字段,而是其get set 方法 且该get方法有返回值的称为属性,继承Object类的getClass方法 package com.swift.demo1; p ...
- iOS 实时监测网络状态(通过Reachability)
在AppDelegate.m中 @property (nonatomic, strong) Reachability *reach; - (BOOL)application:(UIApplicatio ...