Chinaunix首页 | 论坛 | 博客
  • 博客访问: 562839
  • 博文数量: 104
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1559
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-21 00:58
个人简介

锻炼精神,首先要锻炼肉体

文章分类

全部博文(104)

文章存档

2018年(1)

2016年(1)

2015年(101)

2014年(1)

我的朋友

分类: C/C++

2015-03-28 23:24:18

linux 系统编程中多半是使用 C 语言以过程式的编程方法来完成整套流程的工作的,
这对于习惯使用面向对象编程的我来说有很多地方不是很习惯。

比如说,编写面向对象代码的时候,总是把类的声明和类中的成员变量和成员函数声明
在头文件中实现,然后在.cpp 文件中只要对头文件中类中声明的成员函数进行实现便可以了。
如果某个工程中需要这个类中的方法,只要引入该类的头文件便可以了。

然而过程式编程中,这种方法并不能够仅仅通过引入头文件的方式就可以将头文件中定义的方法
作为公共方法来使用。

在过程是编程中,往往在头文件中对函数进行声明,然后在.cpp 文件中写出声明的函数的过程实现,
然后将该 .cpp 文件编译成动态库或是静态库,才能够使 .cpp 文件中的函数被其他程序通过引入头文件所使用。

本篇文章中会编写一个应用与 UDP 协议的通信客户端和服务器端,不同于前几篇文章中的是,
为了尽量减少代码的冗余,会将公共的网络通信代码编写到指定的 .cpp 文件中,然后通过不同的编译方法,
将其编译成动态库和静态库,将其引入到客户端和服务器端的程序中进行调用。




现有头文件 udpnet.h , .cpp 文件 udpnet.cpp

1. 创建静态链接库:

通过下面的的命令来创建一个名为 libudpnet.a 的静态链接库,(注意这个地方的命名规则 lib+源文件的名称 .a)
并将该静态链接库拷贝到系统库的搜索路径下面 /usr/lib/ , /usr/lib64/ , /lib/

1.1
[命令] 首先将 udpnet.cpp 编译生成 udpnet.o 目标文件(object)

g++ -o udpnet.o -c udpnet.cpp

[命令执行结果] 在当前的路径下生成 udpnet.o 文件

1.2
[命令] 将 udpnet.o 目标文件转换为静态库文件
ar src libudpnet.a udpnet.o
[命令执行结果] 在当前的路径下生成 libudpnet.a 静态库文件

1.3
[命令] 将生成的静态库文件,拷贝到系统库的目录下面
cp libudpnet.a   /lib/libudpnet.a
cp libudpnet.a  /usr/lib64/libudpnet.a
cp libudpnet.a  /usr/lib/libudpnet.a

使用静态库
现有程序 client.cpp 和 server.cpp
在编译的时候能够通过如下命令来将静态库加载到编译生成的对象中

[命令] 编译生成 server 对象
g++ server.cpp -ludpnet.a -o server

[命令] 编译 client.cpp 代码生成 client 对象
g++ client.cpp -ludpnet.a -o client



实例代码
1. udpnet.h

点击(此处)折叠或打开

  1. // udpnet.h

  2. #ifndef UDPNET_H_
  3. #define DUPNET_H_

  4. int getSocketDone () ;

  5. int getServerBindDone ( int sock_fd ) ;

  6. int SendMsgTo ( int sock_fd , const char *msg , int msg_len ,
  7.     const char *to_addr , short port ) ;

  8. int RecvMsgFrom ( int sock_fd , char *buffer , int *buf_len ,
  9.     char *from_addr , short *port ) ;

  10. #endif

2. udpnet.cpp

点击(此处)折叠或打开

  1. // udpnet.cpp

  2. #include <stdio.h> // perror
  3. #include <string.h> // memset

  4. #include <sys/types.h> // AF_INET:IP , SOCK_STREAM:TCP , SOCK_DGRAM:UDP
  5. #include <sys/socket.h> // socket , bind , sendto , recvfrom
  6. #include <netinet/in.h> // sockaddr_in , sockaddr, htons , htonl , ntohs ,ntohl
  7. #include <arpa/inet.h> // inet_aton(2) ,inet_ntoa(1)
  8. #include <unistd.h> // unix standard library

  9. #include "udpnet.h"

  10. #define SERVER_IP "10.0.2.15"
  11. #define COMMON_PORT 1027

  12. int getSocketDone ()
  13. {
  14.   int ret = socket( AF_INET , SOCK_DGRAM , 0 ) ;
  15.    
  16.   if ( ret < 0 )
  17.     perror ("socket error") ;
  18.   
  19.   return ret ;
  20. }

  21. // the following method only be called by server
  22. int getServerBindDone ( int sock_fd )
  23. {
  24.  int ret ;
  25.  struct sockaddr_in serv_addr ;
  26.  
  27.  memset ( &serv_addr , 0 , sizeof ( struct sockaddr_in ) ) ;
  28.  serv_addr.sin_family = AF_INET ;
  29.  serv_addr.sin_port = htons (COMMON_PORT) ;
  30.  serv_addr.sin_addr.s_addr = htonl (INADDR_ANY) ;
  31.  
  32.  if ( ( ret = bind ( sock_fd , (struct sockaddr*)&serv_addr ,
  33.         sizeof(struct sockaddr_in ))) < 0 )
  34.       perror ("bind error ") ;

  35.  return ret ;
  36.    
  37. }

  38. int SendMsgTo ( int sock_fd , const char *msg , int msg_len ,
  39.         const char *to_ip , short to_port )
  40. {
  41.       struct sockaddr_in to_addr ;
  42.       int ret ;
  43.      
  44.       memset ( &to_addr , 0 , sizeof(struct sockaddr_in )) ;
  45.       to_addr.sin_family = AF_INET ;
  46.       to_addr.sin_port = htons (to_port) ;

  47.       if ( ( ret = inet_aton ( to_ip , &to_addr.sin_addr) ) < 0 )
  48.       {
  49.      perror ("inet_aton error ") ;
  50.      return ret ;
  51.       }
  52.     
  53.       ret = sendto ( sock_fd , msg , msg_len , 0 ,
  54.         (struct sockaddr*)&to_addr , sizeof(struct sockaddr_in )) ;
  55.    
  56.       if ( ret < 0 )
  57.           perror ( "sendto error ") ;
  58.      
  59.       return ret ;
  60. }

  61. int RecvMsgFrom ( int sock_fd , char *msg , int *msg_len ,
  62.     char *from_ip , short *from_port )
  63. {
  64.    int ret , from_len ;
  65.    struct sockaddr_in from_addr ;
  66.   
  67.    memset ( &from_addr , 0 , sizeof ( struct sockaddr_in ) ) ;
  68.    from_len = sizeof ( struct sockaddr_in ) ;
  69.   
  70.    ret = recvfrom ( sock_fd , msg , (size_t)*msg_len , 0 ,
  71.     (struct sockaddr*)&from_addr ,(socklen_t*)&from_len ) ;
  72.    
  73.    if ( ret < 0 )
  74.    {
  75.       perror ( "recvfrom error") ;
  76.       return ret ;
  77.    }
  78.   
  79.    *msg_len = strlen ( msg ) ;
  80.    
  81.    *from_port = ntohs( from_addr.sin_port ) ;
  82.     
  83.    from_ip = inet_ntoa ( from_addr.sin_addr) ;

  84.    if ( !from_ip ) // from_ip == NULL
  85.    {
  86.     perror ("inet_ntoa error") ;
  87.     return -1 ;
  88.    }
  89.   
  90.    return ret ;
  91. }

3. client.cpp

点击(此处)折叠或打开

  1. // client.cpp

  2. #include <stdio.h> // perror
  3. #include <string.h> // memset

  4. #include <sys/types.h> // AF_INET:IP , SOCK_STREAM:TCP , SOCK_DGRAM:UDP
  5. #include <sys/socket.h> // socket , bind , sendto , recvfrom
  6. #include <netinet/in.h> // htons , htonl , ntohs , ntohl , sockaddr_in , sockaddr
  7. #include <arpa/inet.h> // inet_aton , inet_ntoa
  8. #include <unistd.h>

  9. #include "udpnet.h"

  10. #define BUFFER_LEN 1024
  11. #define IP_LEN 128
  12. #define SERVER_IP "10.0.2.15"
  13. #define COMMON_PORT 1027


  14. int main ( int c , char **v )
  15. {
  16.   short port ;
  17.   int ret , sock_fd ;
  18.   char msg[BUFFER_LEN] , ip_addr[IP_LEN] ;
  19.    
  20.   if ( ( sock_fd = getSocketDone ()) > 0 )
  21.        printf ("socket correct fd : %d\n" , sock_fd) ;
  22.   else
  23.        goto error ;
  24.   
  25.   port = COMMON_PORT ;
  26.  
  27.   memset ( &ip_addr , 0 , sizeof( ip_addr )) ;
  28.  
  29.   // set server ip address
  30.   strcpy (ip_addr , SERVER_IP ) ;
  31.   
  32.   printf ("input message sent to server \n") ;
  33.   scanf ("%s" , msg) ;
  34.   
  35.   ret = SendMsgTo ( sock_fd , msg , strlen( msg ) , ip_addr , port ) ;
  36.  
  37.   if ( ret >= 0 )
  38.   {
  39.     printf ("client has sent %d bytes message to server\n" , ret) ;
  40.     goto success ;
  41.   }
  42.   
  43.   else
  44.     goto error ;

  45. error:
  46.    printf("main error") ;
  47.    goto success ;
  48. success:
  49.   return 0 ;
  50. }
4. server.cpp

点击(此处)折叠或打开

  1. // server.cpp

  2. #include <stdio.h> // perror
  3. #include <string.h> // memset

  4. #include <sys/types.h> // AF_INET:IP , SOCK_STREMA:TCP , SOCK_DGRAM:UDP
  5. #include <sys/socket.h> // socket , bind , sendto , recvfrom
  6. #include <netinet/in.h> // sockaddr_in sockaddr , htons ,htonl , ntohs , ntohl
  7. #include <arpa/inet.h> // inet_aton(2) , inet_ntoa(1)
  8. #include <unistd.h> // unix standard library

  9. #include "udpnet.h"

  10. #define COMMON_PORT 1027
  11. #define BUFFER_LEN 1024
  12. #define IP_LEN 128

  13. int main ( int c , char **v )
  14. {
  15.   short port ;
  16.   char msg[BUFFER_LEN], ip_addr[IP_LEN] ;
  17.   int ret , sock_fd , msg_len ;
  18.   
  19.   sock_fd = getSocketDone () ;
  20.  
  21.   if ( sock_fd > 0 )
  22.     printf ("socket correct fd : %d \n", sock_fd) ;
  23.   else
  24.     goto error ;
  25.   
  26.   if ( !getServerBindDone ( sock_fd) )
  27.         printf ("bind correct\n") ;
  28.   else
  29.     goto error ;
  30.  
  31.   memset ( msg , 0 , sizeof(msg)) ;
  32.   memset ( ip_addr, 0 , sizeof(ip_addr) ) ;
  33.   msg_len = sizeof(msg) ;
  34.  
  35.   printf ("server is watting for message from client\n ") ;
  36.   while ( ( ret = RecvMsgFrom ( sock_fd , msg , &msg_len ,
  37.         ip_addr , &port ) ) <0 ) ;
  38.   
  39.   printf ("end waitting \n") ;
  40.   if ( msg_len )
  41.   {
  42.     printf ("server receive %d bytes from client \n" , msg_len ) ;
  43.     printf ( "message content [%s]\n" , msg ) ;
  44.     goto success ;
  45.   }
  46.  else
  47.     goto error ;
  48.   
  49.  error:
  50.    printf ("main error") ;
  51.    goto success ;
  52.  success:
  53.    return 0 ;
  54. }



运行程序
首先在 terminal 中启动 server 程序
./server
显示信息如下:
socket correct fd : 3 
bind correct
server is watting for message from client
随后打开一个新的 terminal ,在该 terminal 中启动 client 程序
./client 
显示信息如下
socket correct fd : 3
input message sent to server

根据提示信息,输入要发送到 server 的消息,回车结束
socket correct fd : 3
input message sent to server 
hello_inuyasha (用户输入信息, 回车结束)

显示信息如下, client 工作结束
socket correct fd : 3
input message sent to server 
hello_inuyasha
client has sent 14 bytes message to server

切回到 server 所运行的 terminal 中 ,发现显示的信息如下
socket correct fd : 3 
bind correct
server is watting for message from client
 end waitting 
server receive 14 bytes from client 
message content [hello_inuyasha]


阅读(1795) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~