L
libra01
Unregistered / Unconfirmed
GUEST, unregistred user!
我的idTcpClient收到的数据是混乱的数据。
怎么解决?
我只能应用流读写,因为包大小是不同的。
麻烦大家看一下,代码比较长。。谢谢了。。。
I'm sorry .The code is long.Thank you ....
The detail :
type definition:
***************************************
The server code:
OnConnect:
//////////////////////////////////////
OnExecute:
*******************************************
The Client Code:
//////////////////////
The "Connect" button OnClick(Sender:TObject);
///////////////////////
The "Send PPTP_Connect" button OnClick(Sender:TObject);
*******************************That's all.thank you ....I'm pleasure you reply and I need your help ..Thank you very much..
怎么解决?
我只能应用流读写,因为包大小是不同的。
麻烦大家看一下,代码比较长。。谢谢了。。。
代码:
I'm sorry .The code is long.Thank you ....
The detail :
type definition:
代码:
T_PPTP_HEAD_tag = packed record
Version: LongWord;
P_Type: LongWord;
Total_Length: LongWord;
Command_ID: LongWord;
Sequence_ID: LongWord;
end;//the packet head record
T_empty=record
end;//empty record
T_PPTP_Connect_tag = TEmpty; //the connect packet body is Empty
T_PPTP_Connect_REP_tag = TEmpty;//the connect reply packet body is empty too,
T_PPTP_Connect=packed record
head:T_PPTP_Head_tag;
body:T_PPTP_Connect_tag;
end;//the connect packet
T_PPTP_Connect_REP=packed record
head:T_PPTP_head_tag;
body:T_PPTP_Connect_REP_tag;
end;//the connect reply packet
The server code:
OnConnect:
代码:
var
pkt: T_PPTP_Connect;
pktREP: T_PPTP_Connect_REP;
begin
AThread.Connection.ReadBuffer(pkt, sizeof(pkt));
if pkt.head.Command_ID = PPTP_Connect then //PPTP_connect is a integer number in my protocol,like PPTP_Connect_REP
begin
pktREp:=createPPTP_Connect_REP();//a function to create a pptp_connect_rep packet .just fill the command_id.
AThread.Connection.WriteBuffer(pktREP, sizeof(pktREP));
//Showmessage('a client connected!');
end;
end;
//////////////////////////////////////
OnExecute:
代码:
var
Adata: TmemoryStream;
iCmd: integer;
Head: T_PPTP_Head_tag;
pktConnectREP:T_PPTP_Connect_REP;
begin
while AThread.Connection.Connected do
begin
Adata := TmemoryStream.Create;
try
Adata.Clear;
Adata.Position := 0;
AThread.Connection.ReadStream(Adata, -1, false);
Adata.Position := 0;
Adata.Read(Head, sizeof(head));//I have many packets in my protocol,so I can't readbuffer..
iCmd := Head.Command_ID;//Get the command_id
Adata.Position := 0;
if iCmd=PPTP_Connect then//PPTP_connect is a integer number in my protocol,like PPTP_Connect_REP
begin
pktConnectREP:=createPPTP_Connect_REP();
AThread.Connection.WriteBuffer(pktConnectREP, sizeof(pktConnectREP), false);
end;
finally
Adata.Free;
end;
end;
end;
*******************************************
The Client Code:
//////////////////////
The "Connect" button OnClick(Sender:TObject);
代码:
var
pktConnect:T_PPTP_Connect;
begin
TcpClient.Connect();
pktConnect := createPPTP_Connect;//create a packet the command_id is PPTP_Connect;
TcpClient.WriteBuffer(pktConnect, sizeof(pktConnect));
TcpClient.ReadBuffer(pktConnectREP, sizeof(pktConnectREP));
if pktConnectREp.head.Command_ID = PPTP_Connect_REP then
Showmessage('Receive Connect REp packet');//////////////////the data is True, I can get the reply and I can
//////////////////know the connection is established and OK.
end;
///////////////////////
The "Send PPTP_Connect" button OnClick(Sender:TObject);
代码:
var
pktConnect, pktTemp: T_PPTP_Connect;
pktConnectREP: T_PPTP_CONNECT_REP;
Adata: TmemoryStream;
Head: T_PPTP_HEAD_tag;
iCmd: LongWord;
begin
if not TCPClient.Connected then exit;//if not connected then exit;
pktConnect := createPPTP_Connect();//create a pptp_connect packet...and the command_id is PPTP_Connect
Adata := TmemoryStream.Create;
try
try
Adata.Position := 0;
Adata.Write(pktConnect, sizeof(pktConnect));
Adata.Position := 0;
TcpClient.WriteStream(Adata, true, true, 0);//write the packet ,
Adata.Position := 0;
////////////////////////////////////////
TCPClient.ReadStream(Adata, -1, false); //////Read a stream....or Readbuffer ...both methods didn't work well.
// tcpClient.ReadBuffer(Head, sizeof(Head));///////////but receive not a packet ,I'm sure that the server send a PPTP_Connect_REP packet but client didn't receive it correctly....
Adata.Position := 0;
Adata.Read(Head, sizeof(head));
iCmd := Head.Command_ID;
Adata.Position := 0;
ShowMessage(format('Protocol:%s%sVersion:%d%sP_Type:%d%sTotal_length:%d%sCommand_id:%d%ssequence_id:%d',
[ResultCommandString(iCmd), Char(13), Head.Version, Char(13), Head.P_Type, Char(13), Head.Total_Length, Char(13), Head.Command_ID, Char(13), Head.Sequence_ID]));//the message is not correct........I 'm puzzled......Help!!!
except
on e: exception do
begin
Showmessage('OnExecute Exception: [' + E.ClassName + ']: ' + E.Message);
end;
end;
finally
Adata.Free;
end;
end;
end;
*******************************That's all.thank you ....I'm pleasure you reply and I need your help ..Thank you very much..