socketclient.h
#ifndef _SOCKETCLIENT_H_
#define _SOCKETCLIENT_H_
#ifdef __cplusplus
extern "C"
{
#endif
int socketclient_init(void **handle);
int socketclient_send(void *handle, void *buf, int len);
int socketclient_recv(void *handle, void *buf, int *len);
int socketclient_destroy(void *handle);
int socketclient_init2(void **handle);
int socketclient_send2(void *handle, void *buf, int len);
int socketclient_recv2(void *handle, void **buf, int *len);
int socketclient_free2(void **buf);
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];
unsigned int port;
void *buf;
int len;
}SocketHandle;
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));
strcpy(hd->ip, "255.255.255.255");
hd->port = 8888;
hd->buf = NULL;
hd->len = 0;
*handle = (void *)hd;
return 0;
}
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;
}
hd->buf = (char *)malloc(len);
if (hd->buf == NULL)
{
return -2;
}
memcpy(hd->buf, buf, len);
hd->len = len;
return 0;
}
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;
}
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;
}
int socketclient_init2(void **handle)
{
return socketclient_init(handle);
}
int socketclient_send2(void *handle, void *buf, int len)
{
return socketclient_send(handle, buf, len);
}
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);
strncpy(tmp, (char*)hd->buf, hd->len);
*buf = (void *)tmp;
*len = hd->len;
}
else
{
return -2;
}
return 0;
}
int socketclient_free2(void **buf)
{
if (buf == NULL)
{
return -1;
}
void *tmp = *buf;
if (tmp != NULL)
{
free(tmp);
*buf = NULL;
}
return 0;
}
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);
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 )
{
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));
strcpy(hdl->ip, "192.168.0.230");
hdl->port = 8126;
*handle = hdl;
return ret;
}
int socket_send(void *handle , unsigned char *buf , int buflen )
{
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 , unsigned char *buf , int *buflen )
{
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)
{
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;
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;
return 0;
}
int demo(void)
{
int ret = 0;
void *handle = NULL;
char buf[128]; /*in*/
int buflen = 3;
char outbuf[128];
int outbuflen = 3;
strcpy(buf, "dsssssssssdswdfafd");
ret = socket_init(&handle );
if (ret != 0)
{
printf("func cltSocketInit() err:%d \n", ret);
return ret;
}
ret = socket_send(handle , buf , buflen );
if (ret != 0)
{
printf("func cltSocketSend() err:%d \n", ret);
return ret;
}
ret = socket_recv(handle , outbuf , &outbuflen );
if (ret != 0)
{
printf("func cltSocketRev() err:%d \n", ret);
return ret;
}
socket_destory(handle);
return ret ;
}
int main(int argc, const char * argv [])
{
int ret = 0;
void *handle = NULL;
char buf[128];
int buflen = 3;
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;
}
socket_free(&pout);
socket_destory2(&handle);
return 0;
}