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/group__PJ__BASIC.htm#ga5ccc87de27d1236bc31ae3673d153984

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的更多相关文章

  1. [pjsip]Pjlib中配置文件config.h解析

    config_site.h 这个头文件包含在config.h中,用于引入平台?(site)/用户特定的配置以控制PJLIB的特性,用户需要自己生成这个文件. 譬如说我们要把PJLIB编译成DLL,那么 ...

  2. [pjsip]Pjlib中的链表结构

    Pjlib的链表结构跟常见的链表结构有所区别,如下图所示: 图1:一般链表结构 图2:pjlib中的链表结构 可以看到一般的双向链表是链表节点包含数据域,而pjlib中是数据域包含链表节点.一般的链表 ...

  3. PJSIP Socket 模型

    前些日子为解决项目中问题调试到PJSIP Socket收发数据部分 ,好记性不如烂笔头,记录下 PJSIP 使用的Socket 模型 ,以备后用. 不同平台下PJSIP采用不同的 Socket模型,W ...

  4. QT开发pjsip的VOIP,A8平台运行

    QT开发pjsip的VOIP 开发环境 平台:A8 环境:Linux-3.0.8 实现功能:使用QT开发VOIP进行初始化.拨号.挂起 测试工具:minisipserver服务器 效果 界面: min ...

  5. exosip 和 pjsip 简介

     oSIP oSIP的开发开始于2000年7月,第一个版本在2001年5月发 布,到现在已经发展到3.x了.它采用ANSI C编写,而且结 构简单小巧,所以速度特别快,它并不提供高层的SIP会话 控制 ...

  6. 介绍一个开源的SIP(VOIP)协议库PJSIP

    本文系转载,出处不可考. 假设你对SIP/VoIP技术感兴趣,哪希望你不要错过:),假设你对写出堪称优美的Code感兴趣 ,那么你也不可错过:) 这期间我想分析一下一个实际的协议栈的设计到实现的相关技 ...

  7. pjlib深入剖析和使用详解

    1. PJSIP简介 PJSIP的实现是为了能在嵌入式设备上高效实现SIP/VOIP.其主要特征包括:    1).极具移植性.(Extremely portable)                 ...

  8. PJSIP开源库详解

    PJSIP是一个包含了SIP.SDP.RTP.RTCP.STUN.ICE等协议实现的开源库.它把基于信令协议SIP的多媒体框架和NAT穿透功能整合成高层次.抽象的多媒体通信API,这套API能够很容易 ...

  9. PJSIP开发指南

    一.通用设计 1.1   架构 1.1.1        通信图 下面的图展示了SIP消息在PJSIP组件间从后端到前端如何传递的. 1.1.2        类图 下面的图显示类视图 1.2   E ...

随机推荐

  1. Centos 7.0_64bit 下安装 Zabbix server 3.0服务器的安装

    一.关闭selinux   修改配置文件/ etc / selinux / config,将SELINU置为禁用(disabled)   vim /etc/selinux/config  # This ...

  2. vos2009如何设置客户自助密码

    1.  VOS2009 账户管理——>网关管理——>密码:就是客户的自助登陆密码 2.  VOS3000 Vos3000里配置密码和自助密码分开 3.  登陆测试 浏览器输入http:// ...

  3. 笨办法学Python(零)

    py走起!!! 习题0:准备工作 Windows平台 1. 用浏览器打开 http://learnpythonthehardway.org/exercise0.html 下载并安装 gedit 文本编 ...

  4. April 11 2017 Week 15 Tuesday

    Love is hard to get into, but harder to get out of. 相爱不易,相忘更难. The past are hurt, but I think we can ...

  5. 初学React:定义一个组件

    接着聊React,今天说说如何创建一个组件类. <!DOCTYPE html> <html lang="en"> <head> <meta ...

  6. 在ABAP里取得一个数据库表记录数的两种方法

    方法1:使用函数EM_GET_NUMBER_OF_ENTRIES 这个函数使用起来很简单,只需要将想查询的数据库表名称维护进输入参数IT_TABLES: 上图说明这个函数支持批量操作,我查询的两张表名 ...

  7. P1567 统计天数

    题目背景 统计天数 题目描述 炎热的夏日,KC非常的不爽.他宁可忍受北极的寒冷,也不愿忍受厦门的夏天.最近,他开始研究天气的变化.他希望用研究的结果预测未来的天气. 经历千辛万苦,他收集了连续N(1& ...

  8. AI-Info-Micron-Insight:5G、人工智能和即将到来的移动革命

    ylbtech-AI-Info-Micron-Insight:5G.人工智能和即将到来的移动革命 1.返回顶部 1. 5G.人工智能和即将到来的移动革命 人们都说自己的手机“智能”,但究竟有多智能?凡 ...

  9. 动态生成的DOM做点击事件无效

    有时候我们的标签都是从后台获取的数据,然后利用JS添加到页面上,当我们写生成的标签的点击事件(click)时没有效果. 例如: <section> 测试动态生成的DOM点击事件 <b ...

  10. vue 城市列表与字母表联动

    实现两个联动 一是点击右侧字母的时候,城市列表出现相应首字母下的城市 二是鼠标在字母表上滑动的时候,城市列表实时跟着变化 一.点击字母出现相应的列表,给每个字母设置handleLetterClick事 ...