项目基于vs2013
一、新建dll库项目
新建项目-->win32-->win32项目-->输入项目名-->选择Dll-->选择空项目-->完成
头文件如下
/*
下面定义了一套socket客户端发送报文接受报文的api接口
请写出这套接口api的调用方法
*/
#ifndef _INC_Demo01_H
#define _INC_Demo01_H
#ifdef __cplusplus
extern "C" {
#endif
//客户端初始化 获取handle上下
int cltSocketInit(void **handle /*out*/);
//客户端发报文
int cltSocketSend(void *handle /*in*/, unsigned char *buf /*in*/, int buflen /*in*/);
//客户端收报文
int cltSocketRev(void *handle /*in*/, unsigned char *buf /*in*/, int *buflen /*in out*/);
//客户端释放资源
int cltSocketDestory(void *handle/*in*/);
#ifdef __cplusplus
}
#endif
#endif /* _INC_Demo01_H */
c文件:
#include
#include
#include
typedef struct _SCK_HANDLE {
charversion[16];
char serverip[16];
intserverport;
unsigned char *buf ;
intbuflen;
}SCK_HANDLE;
//客户端初始化 获取handle上下
__declspec(dllexport)
int cltSocketInit(void **handle /*out*/)
{
int ret = 0;
SCK_HANDLE *sh = NULL;
sh = (SCK_HANDLE *)malloc(sizeof(SCK_HANDLE));
if (sh == NULL)
{
ret = -1;
printf("func cltSocketInit() err: %d, malloc err....", ret);
return ret;
}
memset(sh, 0, sizeof(SCK_HANDLE));
strcpy(sh->serverip, "192.168.0.128");
sh->serverport= 88;
*handle = sh;
return ret;
}
//客户端发报文
__declspec(dllexport)
int cltSocketSend(void *handle /*in*/, unsigned char *buf /*in*/, int buflen /*in*/)
{
intret = 0;
SCK_HANDLE *sh = NULL;
if (handle==NULL || buf==NULL)
{
ret = -1;
printf("func cltSocketSend() err: %d, (handle==NULL || buf==NULL)", ret);
return ret;
}
sh = (SCK_HANDLE *)handle ;
sh->buf = (char *)malloc(buflen);
if (sh->buf == NULL)
{
ret = -2;
printf("func cltSocketSend() err: %d, (buflen:%d)", ret, buflen);
return ret;
}
memcpy(sh->buf, buf, buflen);
sh->buflen = buflen;
return ret;
}
//客户端收报文
__declspec(dllexport)
int cltSocketRev(void *handle /*in*/, unsigned char *buf /*in*/, int *buflen /*in out*/)
{
int ret = 0;
SCK_HANDLE *sh = NULL;
if (handle==NULL || buf==NULL || buflen==NULL)
{
ret = -1;
printf("func cltSocketSend() err: %d, ((handle==NULL || buf==NULL || buflen==NULL))", ret);
return ret;
}
sh = (SCK_HANDLE *)handle;
memcpy(buf, sh->buf, sh->buflen);
*buflen = sh->buflen;
if (sh->buf != NULL)
{
free(sh->buf);
sh->buf = NULL; //把状态回到原始
sh->buflen = 0;
}
return ret;
}
//客户端释放资源
__declspec(dllexport)
int cltSocketDestory(void *handle/*in*/)
{
int ret = 0;
SCK_HANDLE *sh = NULL;
if (handle==NULL )
{
ret = -1;
printf("func cltSocketSend() err: %d, ((handle==NULL )", ret);
return ret;
}
sh = (SCK_HANDLE *)handle;
if (sh->buf != NULL)
{
free(sh->buf);
sh->buf = NULL;
sh->buflen = 0;
}
free(sh);
return ret;
}
客户端:
#include
#include
#include
#include "socketclientdll.h"
void main() { int ret = 0; void *handle = NULL; unsigned char buf[1024]; /*in*/ int buflen = 10; unsigned char out[1024] = {0}; /*in*/ int outlen = 0; strcpy(buf, "aadd3456789012233ddddddddddddddaaaaaaaaaaa"); //客户端初始化 获取handle上下 //运行的上下文环境 ret = cltSocketInit(&handle/*out*/); if (ret !=0) { printf("func cltSocketInit() err :%d \n", ret); return ; } // //客户端发报文 ret = cltSocketSend(handle /*in*/, buf /*in*/,buflen /*in*/); if (ret !=0) { printf("func cltSocketSend() err :%d \n", ret); return ; } // // //客户端收报文 ret = cltSocketRev(handle /*in*/, out /*in*/, &outlen /*in out*/); if (ret != 0) { printf("func cltSocketRev() err :%d \n", ret); return ; } printf("out: %s \n", out); // // //客户端释放资源 ret = cltSocketDestory(handle/*in*/); if (ret != 0) { printf("func cltSocketDestory() err :%d \n", ret); return ; } system("pause"); }