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

最新文章

  1. 《BI那点儿事》数据挖掘初探
  2. Win10中安装ArcObject帮助
  3. 刨根问底U3D---如何退出Play模式后保留数据更改
  4. CSS设置DIV背景色渐变显示
  5. 数据包判断是否丢包 ping+tracert+mtr
  6. 构建高效安全的Nginx Web服务器
  7. Pencil-一个开源免费的UI原型工具,自带ios和android模板
  8. Java实现ajax
  9. 关于NAT穿透的一些理解
  10. 为什么Application_BeginRequest会执行两次
  11. FlexiGrid使用手册
  12. HtmlAgilityPack实战代码
  13. Apache和Nginx的对比
  14. (二)Servlet入门之HelloWorld
  15. 关于Android屏幕的参数
  16. 使用蒲公英路由器 X3 设置为网络中继器
  17. jQuery同步Ajax带来的UI线程阻塞问题
  18. [BZOJ]|[Ural] Formula 1-----插头DP入门
  19. 【SpringBoot】SpringBoot性能优化
  20. 什么是集群(Cluster)技术

热门文章

  1. 西门子触摸屏利用VBScript脚本创建csv文件
  2. UE4工具
  3. IOS 多线程-NSThread 和线程状态
  4. IOS ScrollView的使用 and delegate
  5. iOS MapKit地图
  6. (排班表三)导出列名不固定的Grid表格到Excel
  7. [JZOJ] 5905. 黑暗之魂(darksoul)
  8. django+xadmin在线教育平台(十四)
  9. 统计寄存器AX中1 的个数
  10. python__高级 : Property 的使用