dll动态库开发-模拟socket通信

测试环境:windows 7  VS2010

模拟socket 通信机制,使用dll动态库。

dll 动态库部分:

  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include<string.h>  
  4.   
  5. typedef struct _SCK_HANDLE  
  6. {  
  7.     char vresion[16];  
  8.     char serverip[16];  
  9.     int servrport;  
  10.     char *buf;  
  11.     int buflen;  
  12.   
  13. }SCK_HANDLE;  
  14.   
  15. /* 
  16. 1.初始化环境 
  17. 2.被调函数分配内存供主调函数使用 
  18. 3.handle 句柄给出资源的内存首地址 
  19. 4.使用void 指针是为了对数据的封装,调用者不知道数据结构,但是底层开发者知道数据结构 
  20. */  
  21.   
  22. __declspec(dllexport)  
  23. int cltSocket_init(void **handle /*out*/)  
  24. {  
  25.     int ret = 0;  
  26.     SCK_HANDLE *sh = NULL;  
  27.     sh = (SCK_HANDLE *)malloc(sizeof(SCK_HANDLE));  
  28.     if (sh == NULL)  
  29.     {  
  30.         ret = -1;  
  31.         printf("cltSocket_init err:%d malloc failure",ret);  
  32.         return ret;  
  33.     }  
  34.   
  35.     *handle = sh;  
  36.     strcpy(sh->vresion,"1.0.1");  
  37.     strcpy(sh->serverip,"192.168.0.1");  
  38.     sh->servrport = 8080;  
  39.     sh->buf = NULL;  
  40.   
  41.     return ret;  
  42. }  
  43.   
  44. //客户端发报文  
  45. __declspec(dllexport)  
  46. int cltSocket_senddata(void *handle/*in */, unsigned char *buf/*in */int buflen/* in*/)  
  47. {  
  48.     int ret = 0;  
  49.     SCK_HANDLE *sh = NULL;  
  50.     if (handle == NULL || buf == NULL)  
  51.     {  
  52.         ret = -1;  
  53.         printf("cltSocket_senddata err:%d",ret);  
  54.         return ret;  
  55.     }  
  56.     sh = (SCK_HANDLE *)handle;  
  57.     sh->buf=(char *)malloc(sizeof(char)*buflen);// 分配内存空间  
  58.     if(sh->buf == NULL)  
  59.     {  
  60.         ret = -2;  
  61.         printf("cltSocket_senddata err %d,buflen ",buflen);  
  62.         return ret;  
  63.     }  
  64.     memcpy(sh->buf,buf,buflen);  
  65.     sh->buflen= buflen;  
  66.   
  67.     return ret;  
  68. }  
  69.   
  70. //客户端收报文  
  71. __declspec(dllexport)  
  72. int cltSocket_resvdata(void *handle/*in */ , unsigned char *buf/*in */int *buflen/*in */)  
  73. {  
  74.     int ret = 0;  
  75.     SCK_HANDLE *sh = NULL;  
  76.     if (handle == NULL || buf == NULL)  
  77.     {  
  78.         ret = -1;  
  79.         printf("cltSocket_resvdata err:%d",ret);  
  80.         return ret;  
  81.     }  
  82.     sh = (SCK_HANDLE *)handle;// 调用者不知道数据类型,底层开发者知道数据类型  
  83.   
  84.     memcpy(buf,sh->buf,sh->buflen);  
  85.     *buflen = sh->buflen;  
  86.   
  87.     return ret;  
  88.   
  89. }  
  90.   
  91. /* 
  92. 1.客户端销毁环境 
  93. 2.根据handle 释放其内存空间 
  94. */  
  95. __declspec(dllexport)  
  96. int cltSocket_destory(void *handle)  
  97. {  
  98.     int ret = 0;  
  99.     SCK_HANDLE *sh = NULL;  
  100.     if ( handle == NULL )  
  101.     {  
  102.         ret = -1;  
  103.         printf("cltSocket_destory err:%d",ret);  
  104.         return ret;  
  105.     }  
  106.     sh = (SCK_HANDLE *)handle;// 调用者不知道数据类型,底层开发者知道数据类型  
  107.   
  108.     if ( sh->buf != NULL )//释放buf的内存  
  109.     {  
  110.         free(sh->buf);  
  111.         sh->buf = NULL;  
  112.     }  
  113.     free(sh);  
  114.   
  115.     return ret;  
  116. }  
  117. #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    typedef struct _SCK_HANDLE
    {
    	char vresion[16];
    	char serverip[16];
    	int servrport;
    	char *buf;
    	int buflen;
    
    }SCK_HANDLE;
    
    /*
    1.初始化环境
    2.被调函数分配内存供主调函数使用
    3.handle 句柄给出资源的内存首地址
    4.使用void 指针是为了对数据的封装,调用者不知道数据结构,但是底层开发者知道数据结构
    */
    
    __declspec(dllexport)
    int cltSocket_init(void **handle /*out*/)
    {
    	int ret = 0;
    	SCK_HANDLE *sh = NULL;
    	sh = (SCK_HANDLE *)malloc(sizeof(SCK_HANDLE));
    	if (sh == NULL)
    	{
    		ret = -1;
    		printf("cltSocket_init err:%d malloc failure",ret);
    		return ret;
    	}
    
    	*handle = sh;
    	strcpy(sh->vresion,"1.0.1");
    	strcpy(sh->serverip,"192.168.0.1");
    	sh->servrport = 8080;
    	sh->buf = NULL;
    
    	return ret;
    }
    
    //客户端发报文
    __declspec(dllexport)
    int cltSocket_senddata(void *handle/*in */, unsigned char *buf/*in */, int buflen/* in*/)
    {
    	int ret = 0;
    	SCK_HANDLE *sh = NULL;
    	if (handle == NULL || buf == NULL)
    	{
    		ret = -1;
    		printf("cltSocket_senddata err:%d",ret);
    		return ret;
    	}
    	sh = (SCK_HANDLE *)handle;
    	sh->buf=(char *)malloc(sizeof(char)*buflen);// 分配内存空间
    	if(sh->buf == NULL)
    	{
    		ret = -2;
    		printf("cltSocket_senddata err %d,buflen ",buflen);
    		return ret;
    	}
    	memcpy(sh->buf,buf,buflen);
    	sh->buflen= buflen;
    
    	return ret;
    }
    
    //客户端收报文
    __declspec(dllexport)
    int cltSocket_resvdata(void *handle/*in */ , unsigned char *buf/*in */, int *buflen/*in */)
    {
    	int ret = 0;
    	SCK_HANDLE *sh = NULL;
    	if (handle == NULL || buf == NULL)
    	{
    		ret = -1;
    		printf("cltSocket_resvdata err:%d",ret);
    		return ret;
    	}
    	sh = (SCK_HANDLE *)handle;// 调用者不知道数据类型,底层开发者知道数据类型
    
    	memcpy(buf,sh->buf,sh->buflen);
    	*buflen = sh->buflen;
    
    	return ret;
    
    }
    
    /*
    1.客户端销毁环境
    2.根据handle 释放其内存空间
    */
    __declspec(dllexport)
    int cltSocket_destory(void *handle)
    {
    	int ret = 0;
    	SCK_HANDLE *sh = NULL;
    	if ( handle == NULL )
    	{
    		ret = -1;
    		printf("cltSocket_destory err:%d",ret);
    		return ret;
    	}
    	sh = (SCK_HANDLE *)handle;// 调用者不知道数据类型,底层开发者知道数据类型
    
    	if ( sh->buf != NULL )//释放buf的内存
    	{
    		free(sh->buf);
    		sh->buf = NULL;
    	}
    	free(sh);
    
    	return ret;
    }
    
测试端:在编写测试程序的时候需要将上面生存的.lib .dll拷贝测试程序目录下 在项目属性->配置属性->输入->附加依赖项中添加.lib 文件全名

socketclientdll.h 文件

  1. #ifndef _CLIENT_SOCKET_H_  
  2. #define _CLIENT_SOCKET_H_  
  3.   
  4. #ifdef __cplusplus    
  5. extern "C" {    
  6. #endif    
  7.     //客户端初始化环境  
  8. int cltSocket_init(void **handle); //5day  
  9.   
  10. //客户端发报文  
  11. int cltSocket_senddata(void *handle, unsigned char *buf, int buflen);  
  12.   
  13. //客户端收报文  
  14. int cltSocket_resvdata(void *hanle , unsigned char *buf, int *buflen);  
  15.   
  16. //4 客户端销毁环境  
  17. int cltSocket_destory(void *handle);  
  18.   
  19. #ifdef  __cplusplus    
  20. }    
  21. #endif  
  22.   
  23. #endif  
#ifndef _CLIENT_SOCKET_H_
#define _CLIENT_SOCKET_H_

#ifdef __cplusplus  
extern "C" {  
#endif  
	//客户端初始化环境
int cltSocket_init(void **handle); //5day

//客户端发报文
int cltSocket_senddata(void *handle, unsigned char *buf, int buflen);

//客户端收报文
int cltSocket_resvdata(void *hanle , unsigned char *buf, int *buflen);

//4 客户端销毁环境
int cltSocket_destory(void *handle);

#ifdef  __cplusplus  
}  
#endif

#endif
测试主函数

  1. #include"socketclientdll.h"  
  2. #include<stdio.h>  
  3. #include<stdlib.h>  
  4. #include<string.h>  
  5.   
  6. int main()  
  7. {  
  8.     int ret = 0, buflen = 5, outbuflen = 0;  
  9.     void *handle = NULL;  
  10.     unsigned char buf[100] = "124141";  
  11.     unsigned char outbuf[100];  
  12.   
  13.     ret = cltSocket_init(&handle);  
  14.     if(ret !=0 )  
  15.     {  
  16.         printf("cltSocket_init err: %d",ret);  
  17.         goto End;  
  18.     }  
  19.   
  20.     ret = cltSocket_senddata(handle,buf,buflen);  
  21.     if(ret != 0)  
  22.     {  
  23.         printf("cltSocket_senddata err: %d",ret);  
  24.         goto End;  
  25.     }  
  26.   
  27.     cltSocket_resvdata(handle,outbuf,&outbuflen);  
  28.     if(ret != 0)  
  29.     {  
  30.         printf("cltSocket_resvdata err: %d",ret);  
  31.         goto End;  
  32.   
  33.     }  
  34.     printf("%d",outbuflen);  
  35.     printf("%s",outbuf);  
  36.   
  37. End:  
  38.     if (handle != NULL)  
  39.     {  
  40.         cltSocket_destory(handle);  
  41.         handle = NULL;  
  42.           
  43.     }  
  44.     system("pause");  
  45.     return ret;  
  46. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值