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
1 //PJLIP I/O 2 //Socket test->get hostname and hostaddress 3 //heat nan 4 #include<pjlib.h> 5 int main() 6 { 7 pj_status_t status; 8 pj_in_addr hostaddr; //This structure describes Internet address. 9 // dada fields pj_uint32_t s_addr. The 32bit IP address. 10 unsigned char *hostaddr_str; 11 const pj_str_t *hostname; 12 //pj_init 13 status=pj_init(); 14 if(status!=PJ_SUCCESS) 15 { 16 PJ_LOG(3,(" ","init failed!")); 17 } 18 //gethostname 19 hostname=pj_gethostname(); 20 if(!hostname||!hostname->ptr||!hostname->slen) 21 { 22 PJ_LOG(3,( "gethostname","faild")); 23 } 24 else 25 { 26 PJ_LOG(3,("gethostname","the hostname is %s",hostname->ptr)); 27 } 28 hostaddr=pj_gethostaddr(); 29 if(hostaddr.s_addr) 30 { 31 hostaddr_str=pj_inet_ntoa(hostaddr);//function pj_in_addr -> char * 32 //Convert an Internet host address given in network byte order to string in standard numbers and dots notation. 33 PJ_LOG(3,("gethostaddress","%s",hostaddr_str)); 34 } 35 else 36 { 37 PJ_LOG(3,("gethostaddress","failed")); 38 } 39 40 pj_shutdown(); 41 getchar();//show the result before you enter any key 42 }
test2:sendto and recv message use udp;
1 // PJLIB I/O UDP test 2 //heat nan 3 //server 4 #include<pjlib.h> 5 #define UDP_PORT 6000 6 #define ADDRESS "127.0.0.1" 7 #define N 100 8 int main( ) 9 { 10 pj_status_t status; 11 pj_sockaddr_in addr; 12 int length=sizeof(addr); 13 pj_sock_t cs; 14 pj_sock_t ss; 15 pj_sockaddr_in daddr; 16 pj_sockaddr_in saddr; 17 pj_str_t s; 18 pj_str_t* IP_Addr; 19 char recvbuff[N+5]; 20 char sendbuff[N+5]; 21 pj_ssize_t len1; 22 status=pj_init(); 23 if(status!=PJ_SUCCESS) 24 { 25 PJ_LOG(3,("pj_init","failed")); 26 } 27 28 //now we creat a socket ss 29 status=pj_sock_socket(pj_AF_INET(),pj_SOCK_DGRAM(),0,&ss); 30 if(status!=0) 31 { 32 PJ_LOG(3,("creat ss socket","failed")); 33 } 34 //now we creat a socket cs 35 status=pj_sock_socket(pj_AF_INET(),pj_SOCK_DGRAM(),0,&cs); 36 if(status!=0) 37 { 38 PJ_LOG(3,("creat cs socket","failed")); 39 } 40 41 42 pj_bzero(&daddr,sizeof(daddr)); 43 daddr.sin_family=pj_AF_INET(); 44 daddr.sin_port=pj_htons(UDP_PORT); 45 IP_Addr=pj_cstr(&s,ADDRESS); 46 daddr.sin_addr=pj_inet_addr(IP_Addr); 47 48 status=pj_sock_bind(ss,&daddr,sizeof(daddr)); 49 if(status!=0) 50 { 51 PJ_LOG(3,("pj_sock_bind ","bind ss failed")); 52 } 53 54 55 /* 56 pj_bzero(&saddr,sizeof(saddr)); 57 saddr.sin_family=pj_AF_INET(); 58 saddr.sin_port=pj_htons(UDP_PORT-1); 59 IP_Addr=pj_cstr(&s,ADDRESS); 60 saddr.sin_addr=pj_inet_addr(IP_Addr); 61 62 status=pj_sock_bind(cs,&saddr,sizeof(saddr)); 63 if(status!=0) 64 { 65 PJ_LOG(3,("pj_sock_bind ","bind cs failed")); 66 } 67 */ 68 pj_create_random_string(sendbuff, N); 69 sendbuff[N-1] = '