我的idTcpClient收到的都是不对的数据 |
答題得分者是:hagar
|
libra01
一般會員 發表:1 回覆:2 積分:0 註冊:2003-08-30 發送簡訊給我 |
我的idTcpClient收到的数据是混乱的数据。
怎么解决?
我只能应用流读写,因为包大小是不同的。 麻烦大家看一下,代码比较长。。谢谢了。。。 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..
|
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
|
libra01
一般會員 發表:1 回覆:2 積分:0 註冊:2003-08-30 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |