#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#define _CRT_SECURE_NO_WARNINGS
typedef struct SOCKET_CLIENT {
int port;
char ip[128];
char* pbuf;
int buflen;
}SOCKET_CLIENT;
//模拟socket,void *handle表示在被调函数中分配内存,并且不对外暴露指针所代表的数据类型
int clientsocket_init(void **handle/*out*/) {
//由于void *handle是空类型,因此需要转一次数据类型,申请内存并且将其中的一些数据赋值初始化
//hadl = (SOCKET_CLIENT*)handle;
int ret = 0;
//申请内存
SOCKET_CLIENT* hadl = (SOCKET_CLIENT*)malloc(sizeof(SOCKET_CLIENT));
if (hadl==NULL) {
ret = -1;
printf("func clientsocket_init() err: code->SOCKET_CLIENT* hadl = (SOCKET_CLIENT*)malloc(sizeof(SOCKET_CLIENT))");
return ret;
}
//写这一步是个很好的习惯
memset(hadl, 0, sizeof(SOCKET_CLIENT));
//赋值
hadl->port = 8080;
strcpy_s(hadl->ip,128, "192.168.0.1");
//再利用二级指针将分配的内存和初始化的数据传递出去
*handle = hadl;
printf("func clientsocket_init()success %d\n", ret);
printf("hadl->ip:%s\n", hadl->ip);
return ret;
}
//消息发送函数
int clientsocket_send(void *handle, char *pmessage, int sendlen) {
int ret = 0;
//先判断传递过来的handle是不是空指针
if (handle==NULL) {
ret = -1;
printf("handle is NULL please check");
return ret;
}
if (sendlen==0) {
printf("cant send len(message) equal zero");
ret = -2;
return ret;
}
//由于形参是一个void类型,因此需要在函数中重新申请一个SOCKET_CLIENT变量
SOCKET_CLIENT* hadl = NULL;
//然后将handle传递给hadl
hadl = (SOCKET_CLIENT*)handle;
//然后需要对handle中的指针做内存申请
hadl->pbuf = (char*)malloc(sizeof(char) * sendlen);
if (hadl->pbuf==NULL) {
ret = -1;
printf("hadl->pbuf==NULL");
return ret;
}
memset(hadl->pbuf, 0, sendlen);
//然后将buf中的数据拷贝到handle中
//strcpy_s(hadl->pbuf, sendlen, pmessage);
memcpy(hadl->pbuf, pmessage, sendlen);
//for (int i = 0; i < sendlen;i++) {
//hadl->pbuf[i] = pmessage[i];
//}
hadl->buflen = sendlen;
printf("send success%s\n", hadl->pbuf);
return ret;
}
int clientsocket_recv(void *handle, char *recvbuf/*in*/, int recvlen) {
int ret = 0;
if (handle==NULL) {
ret = -1;
printf("handle is NULL please check");
return ret;
}
SOCKET_CLIENT* hadl = NULL;
//然后将handle传递给hadl
hadl = (SOCKET_CLIENT*)handle;
//将hadl->pbuf中的值拷贝到recvbuf中
//strcpy_s(recvbuf, recvlen, hadl->pbuf);
for (int i = 0; i < recvlen;i++) {
recvbuf[i] = hadl->pbuf[i] ;
}
return ret;
}
//析构函数
int clientsocket_destory(void **handle) {
int ret = 0;
if (*handle==NULL) {
ret = -1;
return ret;
}
if (handle == NULL) {
ret = -2;
return ret;
}
SOCKET_CLIENT* hadl = NULL;
hadl = (SOCKET_CLIENT*)(*handle);
if (hadl->pbuf!=NULL) {
free(hadl->pbuf);
hadl->pbuf = NULL;
printf("释放内存hadl->pbuf\n");
}
free(hadl);
return ret;
}
int main() {
void* handle = NULL;
int ret = 0;
char pbuf[] = "haseas";
int sendlen = strlen(pbuf);
int recvlen = 3;
char recvbuf[6];
ret = clientsocket_init(&handle);
if (ret!=0) {
return ret;
}
ret = clientsocket_send(handle, pbuf, sendlen);
if (ret !=0) {
return ret;
}
//printf("%p\n", handle);
ret = clientsocket_recv(handle, recvbuf, recvlen);
printf("%s\n", recvbuf);
ret = clientsocket_destory(&handle);
printf("ret:%d\n", ret);
return 0;
}
至于如何生成动态库可以参照我的另一篇文章:
https://blog.csdn.net/weixin_43728225/article/details/119079285?spm=1001.2014.3001.5501