socket协议-根据结构体写包,接收

init:

#include "lrs.h"

#define PROTOVER 351
enum RET_ERROR
{
SUCCESS=0,
ERROR,
WHILE
};
enum errorCode
{
ERROR_NULL=0,
ERROR_NO_SIZE,
ERROR_BUFFER_POINT,
ERROR_POINT,
ERROR_ALLOC_MEM,
ERROR_WRITE_OVERFLOW,
ERROR_READ_OVERFLOW,
ERROR_NO_MOVE,
ERROR_ADDRESS_OVERFLOW,
ERROR_PREPLACE_POINT,
ERROR_OT_OVERFLOW,
ERROR_OVERFLOW,
ERROR_PACKETSIZE_OVERFLOW,
ERROR_NO_FOUND,
ERROR_NO_COUNT,
};
typedef struct PackageHead
{
DWORD iProtoVer;
DWORD iPackageType;
}Head;
//功能函数
typedef struct _StreamWRBuffer
{
char* cBuf;
unsigned int iBufSize;
unsigned int iWPos;
unsigned int iRPos;
}SWRBUFFER,*PSWRBUFFER;
typedef struct ConnectPackageData
{
DWORD iConnectionID;//连接请求时为0,返回一个连接号,由服务器管理
DWORD ClientTaskID;//客户端任务编号
char RobotName[64];
char RobotPWD[64];
}Connect;

typedef struct SectionRequestPackageData
{
DWORD iConnectionID;
DWORD ClientTaskID;//客户端任务编号
DWORD iSectionID;//0,返回一个会话ID,由服务器管理
char UserIDX[64];
char UserMAC[64];
char RoomID[64];
}SRP;


typedef struct QAPackageData
{
DWORD iConnectionID;//每个机器人分配一个id,标记该机器人帐号的一个连接,机器人与平台对应
DWORD ClientInquireID;//当同一台客户机运行多个客户动态库的时候标记每个查询的ID
DWORD QuestionID;
DWORD iSectionID;
DWORD iInquireType;//查询包类型
}QD;


typedef struct DisconnectPackageData
{
DWORD iConnectionID;//连接请求时为0,返回一个连接号,由服务器管理
DWORD ClientTaskID;//客户端任务编号
}DP;


typedef struct SectionEndPackageData
{
DWORD iConnectionID;
DWORD iSectionID;//非0连接ID
DWORD iClientTaskID;
}SEP;

//创建socket
void Create_socket()
{
int ret=0;
int errorcount=0;
while(1)
{
ret=lrs_create_socket("socket0", "TCP","RemoteHost=192.168.0.53:10010", LrsLastArg);
if(ret)
{
switch(ret)
{
case LRS_CREATE_SOCK_FAILED:
case LRS_RECV_MISMATCH:
errorcount++;
if(errorcount>3){
lr_error_message("创建失败, 返回错误码为:%d",ret);
lr_abort ();
return;
}
continue;
default:
lr_error_message("创建socket失败,错误码=%d",ret);
lr_abort ();
return;
}
}
break;
}//end while
}
/*
//字符串写入有长度的
DWORD writeToBufferStringL(PSWRBUFFER buffer,char*str,unsigned int ssize)
{
if(!buffer||!(buffer->cBuf))
{
return ERROR_POINT;
}
if((buffer->iWPos+ssize)>=(buffer->iBufSize))
{
return ERROR_WRITE_OVERFLOW;
}
memcpy(buffer->cBuf+buffer->iWPos,str,ssize);
buffer->iWPos+=ssize;
return ERROR_NULL;
}

*/
//接收
DWORD RecvBuf(char* cRecvBuf,char* cSocketName,char* cBufName)
{
int ret=0;
char * cRecvdata=0;
int iRecvdatalen=0;
DWORD begin=0,cur=0;
begin=clock();
while(1)
{
ret=lrs_receive(cSocketName,cBufName,LrsLastArg);
switch(ret)
{
case 0:
case LRS_RECV_MISMATCH :
ret=lrs_get_last_received_buffer(cSocketName,&cRecvdata,&iRecvdatalen);
if(ret)
{
lr_error_message("last_recv获取接收缓冲区出错,错误码:%d",ret);
lrs_free_buffer(cRecvdata);
return ERROR;
}
if(iRecvdatalen)
{
memcpy(cRecvBuf,cRecvdata,iRecvdatalen);
lrs_free_buffer(cRecvdata);
return SUCCESS;
}
cur=clock();
if((cur-begin)>=10000)
{
lr_error_message ("接收超时,接收等待时间=%d毫秒",cur-begin);
return ERROR;
}
continue;
default:
lr_error_message ("recv接收数据出错,错误码:%d",ret);
return ERROR;
}//end switch
}//end while
return SUCCESS;
}

Head * pHead=0;
Connect * pConnect=0;
char cSendBuf[4096];
char cRecvBuf[4096];

//PSWRBUFFER recvBuf=0;
vuser_init()
{
int ret=0;
lrs_set_recv_timeout(100,0);
lrs_set_recv_timeout2(0,10);
lrs_startup(257);
//创建socket
Create_socket();


return 0;


}

 

action:

#include "lrs.h"


Action()
{
//封包
int ret=0;
pHead=(Head *)cSendBuf;
pHead->iProtoVer=PROTOVER;
pHead->iPackageType=12;

pConnect=(Connect *)(cSendBuf+sizeof(Head));
//pConnect->iConnectionID=;
//pConnect->ClientTaskID=;
sprintf(pConnect->RobotName,"paopaoba");
sprintf(pConnect->RobotPWD,"paopaoba");
ret=lrs_save_param_ex("socket0","user",cSendBuf,0,sizeof(Head)+sizeof(Connect),"ebcdic","Connect");

lr_start_transaction("send");

ret=lrs_send("socket0","buf1",LrsLastArg);
if(ret)
{
lr_error_message("send ERROR!ret=%d",ret);

}

lr_end_transaction("send", LR_AUTO);

lr_error_message("send succes");
//lrs_receive("socket0","buf10",LrsLastArg);
//lr_error_message("get return %s",buf10);
//char* recbuf;
ret=RecvBuf(cRecvBuf,"socket0","buf10");
lr_error_message("get string :%s",cRecvBuf);
pHead=(Head *)cRecvBuf;
lr_error_message("get string :%d",pHead->iPackageType);
//解析cRecvBuf
pConnect=(Connect *)(cSendBuf+sizeof(Head));
lr_error_message("get string :%s",pConnect->RobotPWD);
return 0;
}

 data:

;WSRData 2 1


send buf1 "<Connect>"


recv buf10 12

 


-1

posted @ 2015-07-09 10:41  耀yao  阅读(751)  评论(0编辑  收藏  举报