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/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

PJSIP-PJLIB-Socket
 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 }
get hostname

test2:sendto and recv message use udp;

PJSIP-PJLIB-Socket
 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] = '\0';

70     PJ_LOG(3,("string","%s",sendbuff));

71    len1=sizeof(sendbuff);

72 

73 

74    //pj_sock_sendto: Transmit data to the socket to the specified address.

75    status=pj_sock_sendto(cs,sendbuff,&len1,0,&daddr,sizeof(daddr));

76    if(status!=PJ_SUCCESS)

77    {

78        PJ_LOG(3,("sendto","failed"));

79    }

80 

81     pj_bzero(&addr,sizeof(addr));

82     //pj_sock_recv: Receives data stream or message coming to the specified socket.

83     status=pj_sock_recv(ss,recvbuff,&len1,0);

84     if(status!=PJ_SUCCESS)

85     {

86         PJ_LOG(3,("recv","failed"));

87     }

88     else

89     {

90         PJ_LOG(3,("content","%s",recvbuff));

91     }

92     pj_shutdown();

93     getchar();

94 

95 }
View Code

 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.

PJSIP-PJLIB-Socket
 1 //UDP test

 2 //server   

 3 //heat nan

 4 //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!

 5 #include<pjlib.h>

 6 #define UDP_PORT 6000

 7 #define ADDRESS "127.0.0.1"

 8 #define N 100

 9 #define M 50

10 int main()

11 {   

12     pj_status_t status;

13     pj_sock_t cs;

14     pj_sock_t ss;

15     pj_sockaddr_in daddr,saddr;

16     pj_str_t s;

17     pj_str_t* IP_Addr;

18     char sendbuff[M+5],recvbuff[N+5];

19     pj_ssize_t len1,len2;

20     status=pj_init();

21     if(status!=PJ_SUCCESS)

22     {

23         PJ_LOG(3,("pj_init","failed"));

24     }

25 

26     status=pj_sock_socket(pj_AF_INET(),pj_SOCK_DGRAM(),0,&ss);

27     if(status!=0)

28     {

29         PJ_LOG(3,("creat ss socket","failed"));

30     }

31     

32     pj_bzero(&daddr,sizeof(daddr));

33     daddr.sin_family=pj_AF_INET();

34     daddr.sin_port=pj_htons(UDP_PORT);

35     IP_Addr=pj_cstr(&s,ADDRESS);

36     daddr.sin_addr.s_addr=pj_htonl(PJ_INADDR_ANY);

37 

38     status=pj_sock_bind(ss,&daddr,sizeof(daddr));

39     if(status!=0)

40     {

41         PJ_LOG(3,("pj_sock_bind ","bind ss failed"));

42     }

43 

44 

45     pj_bzero(&saddr,sizeof(saddr));

46     saddr.sin_family=pj_AF_INET();

47     saddr.sin_port=pj_htons(UDP_PORT-1);

48     IP_Addr=pj_cstr(&s,ADDRESS);

49     saddr.sin_addr=pj_inet_addr(IP_Addr);

50 

51     while(1){

52         len1=N;

53         status=pj_sock_recv(ss,recvbuff,&len1,0);

54         if(status==PJ_SUCCESS)

55         {

56              PJ_LOG(3,("recv","success"));

57              PJ_LOG(3,("context","%s",recvbuff));

58         }

59         

60         else

61         {

62             printf("failed\n");

63         }

64    

65 

66         gets(sendbuff);

67         len2=sizeof(sendbuff);

68 //        PJ_LOG(3,("string","%s",sendbuff));

69         status=pj_sock_sendto(ss,sendbuff,&len2,0,&saddr,sizeof(saddr));

70         if(status!=PJ_SUCCESS)

71         {

72             PJ_LOG(3,("send","failed"));

73         }

74         else

75         { 

76 

77         }

78     

79     }    

80 

81     

82     getchar();

83     return 0;

84 }
server
PJSIP-PJLIB-Socket
 1 //UDP test

 2 //Client 

 3 //heat nan

 4 #include<pjlib.h>

 5 #define UDP_PORT 6000

 6 #define ADDRESS "127.0.0.1" 

 7 #define N 50

 8 #define M 100

 9 int main()

10 {

11     pj_status_t status;

12     pj_ssize_t len1,len2;

13     pj_sock_t cs,ss;

14     pj_sockaddr_in daddr,saddr;

15     pj_str_t s;

16     pj_str_t* IP_Addr;

17     char recvbuff[M+5],sendbuff[N+5];

18     status=pj_init();

19     

20     if(status!=PJ_SUCCESS)

21     {

22         PJ_LOG(3,("pj_init","failed"));

23     }

24     

25     status=pj_sock_socket(pj_AF_INET(),pj_SOCK_DGRAM(),0,&cs);

26     if(status!=0)

27     {

28         PJ_LOG(3,("creat cs socket","failed"));

29     }

30     

31     pj_bzero(&daddr,sizeof(daddr));

32     daddr.sin_family=pj_AF_INET();

33     daddr.sin_port=pj_htons(UDP_PORT);

34     IP_Addr=pj_cstr(&s,ADDRESS);

35     daddr.sin_addr=pj_inet_addr(IP_Addr);

36 

37     pj_bzero(&saddr,sizeof(saddr));

38     saddr.sin_family=pj_AF_INET();

39     saddr.sin_port=pj_htons(UDP_PORT-1);

40     IP_Addr=pj_cstr(&s,ADDRESS);

41     saddr.sin_addr=pj_inet_addr(IP_Addr);

42 

43 

44    status=pj_sock_bind(cs,&saddr,sizeof(saddr));

45     if(status!=0)

46     {

47         PJ_LOG(3,("pj_sock_bind ","bind ss failed"));

48     }

49 

50 /*

51     len1=N;

52     pj_create_random_string(sendbuff, N);

53     sendbuff[N-1] = '\0';

54 */

55     while(1){

56     gets(sendbuff);

57     len1=sizeof(sendbuff);

58 //    PJ_LOG(3,("string","%s",sendbuff));

59     status=pj_sock_sendto(cs,sendbuff,&len1,0,&daddr,sizeof(daddr));

60     if(status!=PJ_SUCCESS)

61     {

62         PJ_LOG(3,("sendto","failed"));

63     }

64 

65 

66     len2=M;

67     status=pj_sock_recv(cs,recvbuff,&len2,0);

68     if(status!=PJ_SUCCESS)

69     {

70         PJ_LOG(3,("recvfrom","failed"));

71     }

72     else

73     {

74         PJ_LOG(3,("context","%s",recvbuff));

75     }

76   }

77  

78     getchar();

79     

80 }
client

 

 

 

 

 

 

你可能感兴趣的:(socket)