10.动态库
第一套API函数
动态库代码:DJSocketClient.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable:4996) //客户端初始化 获取handle上下 __declspec(dllexport) // 把这个函数以动态库的形式导出来 typedef struct __SCK_HANDLE { char version[16]; char serverip[16]; int serverport; unsigned char *buf; int buflen; }SCK_HANDLE; // handle 运行的一块上下文环境 __declspec(dllexport) int cltSocketInit(void **handle /*out*/) { SCK_HANDLE *sk = NULL; sk = (SCK_HANDLE *)malloc(sizeof(SCK_HANDLE)); int ret = -1; if (sk == NULL) { printf("cltSocketInit err, malloc error\n"); return ret; } memset(sk, 0, sizeof(SCK_HANDLE)); strcpy(sk->version,"1.0.0"); strcpy(sk->serverip,"192.168.42.1"); sk->serverport = 8081; *handle = sk; return 0; } //客户端发报文 __declspec(dllexport) int cltSocketSend(void *handle /*in*/, unsigned char *buf /*in*/, int buflen /*in*/) { int ret = -1; SCK_HANDLE *sh = NULL; if (handle == NULL || buf == NULL) { printf("cltSocketSend err, argus err\n"); return -2; } sh = (SCK_HANDLE *)handle; sh->buf = (char *)malloc(buflen); // 这里要注意结构体的浅拷贝问题,不要再创建一个临时变量 if (sh->buf == NULL) { printf("cltSocketSend err, sh->buf malloc err"); return -3; } memcpy(sh->buf, buf, buflen); sh->buflen = buflen; return 0; } //客户端收报文 __declspec(dllexport) int cltSocketRev(void *handle /*in*/, unsigned char *buf /*in*/, int *buflen /*in out*/) { SCK_HANDLE *sh = NULL; int ret = -1; if (handle == NULL || buf == NULL || buflen == NULL) { printf("cltSocketRev err, arugs err\n"); return -1; } sh = (SCK_HANDLE *)handle; memcpy(buf,sh->buf,sh->buflen); *buflen = sh->buflen; return 0; } //客户端释放资源 __declspec(dllexport) int cltSocketDestory(void *handle/*in*/) { SCK_HANDLE *sh = NULL; int ret = -1; if (handle == NULL) { printf("cltSocketDestory err, arugs err\n"); return -1; } sh = (SCK_HANDLE*)handle; if (sh->buf != NULL) { free(sh->buf); sh->buf = NULL; sh->buflen = 0; } if (sh != NULL) { free(sh); } return 0; }
使用该动态库
#include <stdio.h> #include <stdlib.h> #include "socketclientdll.h" int main() { int ret = 0; void *handle = NULL; unsigned char sendbuf[1024] = "123456789eejioofajiofjaiofjalfjla"; int send_buf_len = 12; unsigned char receivebuf[1024] = {0}; int receive_buf_len = 1024; //客户端初始化 获取handle上下 ret = cltSocketInit(&handle /*out*/); if (ret != 0) { printf("%s\n","Socket初始化失败"); return; } // 客户端发报文 ret = cltSocketSend(handle /*in*/, sendbuf /*in*/, send_buf_len /*in*/); if (ret != 0) { printf("%s\n", "数据发送失败"); return; } //客户端收报文 ret = cltSocketRev(handle /*in*/, receivebuf /*in*/, &receive_buf_len /*in out*/); if (ret != 0) { printf("%s\n","数据接收失败"); return; } else { printf("数据接收成功,接收到的数据为:%s\n",receivebuf); } //客户端释放资源 ret = cltSocketDestory(handle/*in*/); if (ret != 0) { printf("Socket释放失败"); return; } system("pause"); return 0; }
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
分类:
C语言基础提高
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库