Socket protocol-According to the structure of the package, receive

Source: Internet
Author: User
Tags pconnect

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
{
Dwordiprotover;
Dwordipackagetype;
}head;
function function
typedef struct _STREAMWRBUFFER
{
char* Cbuf;
unsigned int ibufsize;
unsigned int iwpos;
unsigned int irpos;
}swrbuffer,*pswrbuffer;
typedef struct CONNECTPACKAGEDATA
{
dwordiconnectionid;//connection request is 0, returns a connection number, managed by the server
dwordclienttaskid;//Client Task number
CHARROBOTNAME[64];
CHARROBOTPWD[64];
}connect;

typedef struct SECTIONREQUESTPACKAGEDATA
{
Dwordiconnectionid;
dwordclienttaskid;//Client Task number
dwordisectionid;//0, returns a session ID, managed by the server
CHARUSERIDX[64];
CHARUSERMAC[64];
CHARROOMID[64];
}SRP;


typedef struct QAPACKAGEDATA
{
dwordiconnectionid;//Each robot assigns an ID, marks a connection to the robot account, and the robot corresponds to the platform
dwordclientinquireid;//mark the ID of each query when the same client runs multiple client dynamic libraries
Dwordquestionid;
Dwordisectionid;
dwordiinquiretype;//Query Package Type
}QD;


typedef struct DISCONNECTPACKAGEDATA
{
dwordiconnectionid;//connection request is 0, returns a connection number, managed by the server
dwordclienttaskid;//Client Task number
}DP;


typedef struct SECTIONENDPACKAGEDATA
{
Dwordiconnectionid;
dwordisectionid;//non-0 connection ID
Dwordiclienttaskid;
}SEP;

Create 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 ("Failed to create, return error code:%d", ret);
Lr_abort ();
Return
}
Continue
Default
Lr_error_message ("Create socket failed, error code =%d", ret);
Lr_abort ();
Return
}
}
Break
}//end while
}
/*
The string is written to a length
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;
}

*/
Receive
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 get receive buffer error, error code:%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 ("Receive timeout, receive wait time =%d milliseconds", cur-begin);
return ERROR;
}
Continue
Default
Lr_error_message ("recv Receive data error, error code:%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);
Create socket
Create_socket ();


return 0;


}

Action

#include "Lrs.h"


Action ()
{
Sealing Package
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);
Parsing 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

Socket protocol-According to the structure of the package, receive

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.