标签:
公共的接口要求
//SocketProtocol.h #pragma once class SocketIF { public: //客户端初始化 获取handle 上下文信息 virtual int cltSocketInit() = 0; //客户端发报文 virtual int cltSocketSend(unsigned char *buf, int buflen) = 0; //客户端收报文 virtual int cltSocketRev(unsigned char *buf, int *buflen) = 0; //客户端释放资源 virtual int cltSocketDestory() = 0; };厂商1要求的接口原型
//SocketImp1.h #pragma once #include "socketprotocol.h" class SocketImp1 :public SocketIF { public: SocketImp1(void); ~SocketImp1(void); public: int cltSocketInit(); //客户端发报文 int cltSocketSend(unsigned char *buf, int buflen); //客户端收报文 int cltSocketRev(unsigned char *buf, int *buflen); //客户端释放资源 int cltSocketDestory(); private: char *pcommon; int len; };
//SocketImp1.cpp #include <stdio.h> #include <stdlib.h> #include <string.h> #include "SocketImp1.h" SocketImp1::SocketImp1(void) { } SocketImp1::~SocketImp1(void) { } int SocketImp1::cltSocketInit() { return 0; } //客户端发报文 int SocketImp1::cltSocketSend(unsigned char *buf, int buflen) { pcommon = (char *)malloc((buflen + 1)*sizeof(char)); //pcommon[len-1] = 0; len = buflen; memcpy(pcommon, buf, buflen); return 0; } //客户端收报文 int SocketImp1::cltSocketRev(unsigned char *buf, int *buflen) { memcpy(buf, pcommon, len); *buflen = len; return 0; } //客户端释放资源 int SocketImp1::cltSocketDestory() { if (pcommon != NULL) { delete[] pcommon; } return 0; }
//mainclass.cpp #define _CRT_SECURE_NO_WARNINGS #include "iostream" #include "SocketImp1.h" #include "SocketProtocol.h" using namespace std; //主框架 void mainOP01() { SocketIF *sIf = new SocketImp1(); unsigned char buf[1024]; strcpy((char *)buf, "ddddddddddddddsssssssssssssssssssss"); int buflen = 10; unsigned char out[1024]; int outlen = 10; sIf->cltSocketInit(); sIf->cltSocketSend(buf, buflen); sIf->cltSocketRev(out, &outlen); sIf->cltSocketDestory(); } int mainOP02(SocketIF *sIf, unsigned char *in, int inlen, unsigned char *out, int *outlen) { int ret = 0; ret = sIf->cltSocketInit(); ret = sIf->cltSocketSend(in, inlen); ret = sIf->cltSocketRev(out, outlen); ret = sIf->cltSocketDestory(); return ret; } void main() { SocketIF *sIf = new SocketImp1(); unsigned char buf[1024] = { 0 }; strcpy((char *)buf, "ddddddddddddddsssssssssssssssssssss"); int buflen = 10; unsigned char out[1024] = { 0 }; int outlen = 10; mainOP02(sIf, buf, buflen, out, &outlen); printf("out:%s\n", out); delete sIf; system("pause"); }
标签:
原文地址:http://blog.csdn.net/waldmer/article/details/43527413