c语言编程回复广播报文,c语言实现模拟发送报文

#include "stdlib.h"

#include "stdio.h"

#include "string.h"

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;

}

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");

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值