C中Socket模型框架

socketclient.h

#ifndef   _SOCKETCLIENT_H_
#define   _SOCKETCLIENT_H_

//兼容C++编译器
//如果是C++编译器,按c标准编译
#ifdef __cplusplus
extern "C"
{

#endif

	//init environment
	int socketclient_init(void **handle);

	//send message
	int socketclient_send(void *handle, void *buf, int len);

	//receive message
	int socketclient_recv(void *handle, void *buf, int *len);

	//destory resource
	int socketclient_destroy(void *handle);

	//init environment
	int socketclient_init2(void **handle);

	//send message
	int socketclient_send2(void *handle, void *buf, int len);

	//receive message
	int socketclient_recv2(void *handle, void **buf, int *len);

	//free resource
	int socketclient_free2(void **buf);

	//destory resource
	int socketclient_destroy2(void **handle);


#ifdef __cplusplus
}

#endif

#endif


socketclient.c

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct SocketHandle
{
	char ip[100];	//ip
	unsigned int port;	//port
	void *buf;
	int len;
}SocketHandle;


//init environment
int socketclient_init(void **handle)
{

	if (handle == NULL)
	{
		return -1;
	}

	SocketHandle *hd = NULL;
	hd = (SocketHandle *)malloc(sizeof(SocketHandle));
	if (hd == NULL)
	{
		return -2;
	}

	memset(hd, 0, sizeof(SocketHandle)); //初始化为0


	//结构体成员变量赋值
	strcpy(hd->ip, "255.255.255.255");
	hd->port = 8888;
	hd->buf = NULL;
	hd->len = 0;

	//间接赋值,是指针存在最大意义
	*handle = (void *)hd;

	return 0;
}

//send message
int socketclient_send(void *handle, void *buf, int len)
{
	if (handle == NULL || buf == NULL)
	{
		return -1;
	}

	SocketHandle *hd = (SocketHandle *)handle;

	if (hd->buf != NULL) //先把上一次分配的空间释放
	{
		free(hd->buf);
		hd->buf = NULL;
	}

	//给结构体buf分配空间
	hd->buf = (char *)malloc(len);
	if (hd->buf == NULL)
	{
		return -2;
	}

	//memcpy
	memcpy(hd->buf, buf, len);
	hd->len = len;

	return 0;
}

//receive message
int socketclient_recv(void *handle, void *buf, int *len)
{
	if (handle == NULL || buf == NULL)
	{
		return -1;
	}

	SocketHandle *hd = (SocketHandle *)handle;

	if (hd->buf != NULL)
	{
		memcpy(buf, hd->buf, hd->len);
		*len = hd->len;
	}
	else
	{
		return -2;
	}

	return 0;
}

//destory resource
int socketclient_destroy(void *handle)
{
	if (handle == NULL)
	{
		return -1;
	}

	SocketHandle *hd = (SocketHandle *)handle;

	if (hd->buf != NULL)
	{
		free(hd->buf);
		hd->buf = NULL;
	}

	if (hd != NULL)
	{
		free(hd);
		hd = NULL;
	}


	return 0;
}

//init environment
int socketclient_init2(void **handle)
{
	return socketclient_init(handle);
}

//send message
int socketclient_send2(void *handle, void *buf, int len)
{
	return socketclient_send(handle, buf, len);
}

//receive message
int socketclient_recv2(void *handle, void **buf, int *len)
{

	if (handle == NULL || buf == NULL)
	{
		return -1;
	}

	SocketHandle *hd = (SocketHandle *)handle;

	if (hd->buf != NULL)
	{
		char *tmp = NULL;
		tmp = (char *)malloc(hd->len + 1);
		if (tmp == NULL)
		{
			return -3;
		}
		memset(tmp, 0, hd->len + 1);
		//memcpy(tmp, hd->buf, hd->len);
		//hd->buf没有当做字符串处理,没有0结束符,
		//strcpy(tmp, (char *)hd->buf);
		strncpy(tmp, (char*)hd->buf, hd->len);

		//间接赋值
		*buf = (void *)tmp;
		*len = hd->len;
	}
	else
	{
		return -2;
	}

	return 0;
}

//free resource
int socketclient_free2(void **buf)
{

	if (buf == NULL)
	{
		return -1;
	}

	void *tmp = *buf;
	if (tmp != NULL)
	{
		free(tmp);
		*buf = NULL;
	}

	return 0;
}

//destory resource
int socketclient_destroy2(void **handle)
{
	int ret = socketclient_destroy(*handle);
	if (ret != 0)
	{
		return ret;
	}

	*handle = NULL;

	return 0;
}


main.c

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "socketclient.h"

int demo(void)
{
	void *handle = NULL;
	int ret = 0;

	ret = socketclient_init(&handle);
	if (ret != 0)
	{
		printf("socketclient_init err:%d\n", ret);
		system("pause");
		return ret;
	}

	char buf[100] = "abcdefgdsagdsgsdg";
	ret = socketclient_send(handle, buf, strlen(buf) );
	if (ret != 0)
	{
		printf("socketclient_send err:%d\n", ret);
		system("pause");
		return ret;
	}

	printf("数据发送成功\n");

	memset(buf, 0, sizeof(buf));

	int len = 0;
	ret = socketclient_recv(handle, buf, &len);
	if (ret != 0)
	{
		printf("socketclient_recv err:%d\n", ret);
		system("pause");
		return ret;
	}

	printf("buf = %s, len = %d\n", buf, len);

	ret = socketclient_destroy(handle);
	if (ret != 0)
	{
		printf("socketclient_destroy err:%d\n", ret);
		system("pause");
		return ret;
	}

	handle = NULL;

	return 0;
}

int main(void)
{
	void *handle = NULL;
	int ret = 0;

	ret = socketclient_init2(&handle);
	if (ret != 0)
	{
		printf("socketclient_init err:%d\n", ret);
		system("pause");
		return ret;
	}

	char buf[100] = "abcdefgdsagdsgsdg";
	ret = socketclient_send2(handle, buf, strlen(buf));
	if (ret != 0)
	{
		printf("socketclient_send err:%d\n", ret);
		system("pause");
		return ret;
	}

	printf("数据发送成功\n");

	void *str = NULL;

	int len = 0;
	ret = socketclient_recv2(handle, &str, &len); //str是在函数内部分配空间
	if (ret != 0)
	{
		printf("socketclient_recv err:%d\n", ret);
		system("pause");
		return ret;
	}

	printf("buf = %s, len = %d\n", (char *)str, len);

	socketclient_free2(&str);

	ret = socketclient_destroy2(&handle);
	if (ret != 0)
	{
		printf("socketclient_destroy err:%d\n", ret);
		system("pause");
		return ret;
	}

	return 0;
}

模板3

 #include <stdlib.h>
#include <string.h>
#include <stdio.h>

typedef struct _SCK_HANDLE
{
	char	version[64];
	char	ip[128];
	int		port;
	unsigned char	*p;
	int		plen;
}SCK_HANDLE;  

int socket_init(void **handle /*out*/)
{
	int		ret     = 0;
	SCK_HANDLE *hdl = NULL;

	hdl = (SCK_HANDLE *)malloc(sizeof(SCK_HANDLE));
	if(hdl == NULL)
	{
		ret = -1;
		return ret;
	}
	memset(hdl, 0, sizeof(SCK_HANDLE)); //把指针所指向的内存空间 赋值成 0;

	strcpy(hdl->ip, "192.168.0.230");
	hdl->port = 8126;
	*handle = hdl;

	return ret;
}

//客户端发报文
int socket_send(void *handle /*in*/, unsigned char *buf /*in*/,  int buflen /*in*/)
{
	int		ret     = 0;
	SCK_HANDLE *hdl = NULL;

	if (handle==NULL || buf==NULL )
	{
		ret = -1;
		return ret;
	}
	
	hdl = (SCK_HANDLE *)handle;

	hdl->p = (unsigned char *)malloc(buflen *sizeof(unsigned char));
	if (hdl->p == NULL)
	{
		ret = -2;
		return ret;
	}
	memcpy(hdl->p, buf, buflen);
	hdl->plen = buflen;

	return 0;
}

//客户端收报文
int socket_recv(void *handle /*in*/, unsigned char *buf /*in*/, int *buflen /*in out*/)
{
	int		ret     = 0;
	SCK_HANDLE *hdl = NULL;

	if (handle==NULL || buf==NULL  ||buflen==NULL)
	{
		ret = -1;
		return ret;
	}
	hdl = (SCK_HANDLE *)handle;

	memcpy(buf, hdl->p, hdl->plen);
	*buflen =  hdl->plen;

	return ret;
}

//客户端释放资源
int socket_destory(void *handle/*in*/)
{
	int		ret      = 0;
	SCK_HANDLE *hdl  = NULL;

	if (handle==NULL )
	{
		ret = -1;
		return ret;
	}
	hdl = (SCK_HANDLE *)handle;

	if (hdl->p)
	{
		free(hdl->p);
	}
	free(hdl);

	return ret;
}


int socket_init2(void **handle)
{
	return socket_init(handle);
}

//客户端发报文
int socket_send2(void *handle, unsigned char *buf,  int buflen)
{
	return socket_send(handle, buf, buflen);
}

//客户端收报文
int socket_recv2(void *handle, unsigned char **buf, int *buflen)
{
	int		ret              = 0;
	SCK_HANDLE *hdl          = NULL;
	unsigned char		*tmp = NULL;

	if (handle==NULL || buf==NULL  ||buflen==NULL)
	{
		ret = -1;
		return ret;
	}
	hdl = (SCK_HANDLE *)handle;

	tmp = (unsigned char *)malloc(hdl->plen);
	if (tmp == NULL)
	{
		ret = -2;
		return ret;
	}

	memcpy(tmp, hdl->p, hdl->plen);
	*buflen =  hdl->plen;

	*buf = tmp; //间接赋值
	return ret;
}

int socket_free(unsigned char **buf)
{
	if (buf == NULL)
	{
		return -1;
	}
	if (*buf != NULL)
	{
		free(*buf);
	}
	*buf = NULL; //*实参的地址  去间接的修改实参的值  重新初始化NULL
	return 0;
}

//客户端释放资源
int socket_destory2(void **handle)
{
	SCK_HANDLE *tmp = NULL;
	if (handle==NULL)
	{
		return -1;
	}
	tmp = *handle; 
	if (tmp != NULL)
	{
		if (tmp->p)
		{
			free(tmp->p);
			tmp->p = NULL;
		}
		free(tmp);
	}
	*handle = NULL; //*实参的地址  去间接的修改实参的值  重新初始化NULL

	return 0;
}

int demo(void)
{
	int		ret     = 0;
	void	*handle = NULL;

	char buf[128]; /*in*/
	int buflen = 3;/*in*/

	char outbuf[128]; /*in*/
	int outbuflen = 3;/*in*/
	strcpy(buf, "dsssssssssdswdfafd");

	ret = socket_init(&handle /*out*/); 
	if (ret != 0)
	{
		printf("func cltSocketInit() err:%d \n", ret);
		return ret;
	}

	//客户端发报文
	ret =  socket_send(handle /*in*/, buf /*in*/, buflen /*in*/);
	if (ret != 0)
	{
		printf("func cltSocketSend() err:%d \n", ret);
		return ret;
	}

	//客户端收报文
	ret =  socket_recv(handle /*in*/, outbuf /*in*/, &outbuflen /*in out*/);
	if (ret != 0)
	{
		printf("func cltSocketRev() err:%d \n", ret);
		return ret;
	}

	//客户端释放资源
	 socket_destory(handle/*in*/);

	return ret ;
}

int main(int argc, const char * argv [])
{
	int		ret = 0;
	void	*handle = NULL;

	char buf[128]; /*in*/
	int buflen = 3;/*in*/

	unsigned char	*pout = NULL;
	int		poutlen = 0;
	strcpy(buf, "dsssssssssdswdfafd");

	ret = socket_init2(&handle); 

	//客户端发报文
	ret = socket_send2(handle, buf,  buflen);
	if (ret != 0)
	{
		return ret;
	}

	//客户端收报文
	ret = socket_recv2(handle, &pout, &poutlen); 
	if (ret != 0)
	{
		return ret;
	}
	/*
	if (pout != NULL)
	{
		free(pout);
	}
	*/
	socket_free(&pout); //避免野指针  把outbuf所指向的内存释放,同时把outbuf变量赋值NULL

	socket_destory2(&handle);
    
	return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值