indy10下以字节形式发送结构无法收到
客户端以字节数组形式发送一个结构以后,服务器无法正确读出内容,不知道问题出在哪里
客户端发送
userInfo aUser;
aUser.name=Edit1->Text;
aUser.passWord=Edit2->Text;
TBytes buffer;
buffer=RawToBytes(&aUser, sizeof(userInfo));
IdTCPClient1->IOHandler->Write(buffer);
服务器端接收
userInfo aUser;
TBytes buffer;
try
{
AContext->Connection->IOHandler->ReadBytes(buffer, sizeof(userInfo), false);
BytesToRaw(buffer, &aUser, sizeof(userInfo));
Memo1->Lines->Add("用户名:"+aUser.name);
Memo1->Lines->Add("密码:"+aUser.passWord);
}
catch (Exception &e)
{
Memo1->Lines->Add("读取登录信息异常");
return;
}
[解决办法]
//---------------------------------------#include <vcl.h>#pragma hdrstop#include "Unit1.h"#include <TCHAR.h>//---------------------------------------#pragma package(smart_init)#pragma link "IdBaseComponent"#pragma link "IdComponent"#pragma link "IdTCPClient"#pragma link "IdTCPConnection"#pragma link "IdContext"#pragma link "IdCustomTCPServer"#pragma link "IdTCPServer"#pragma resource "*.dfm"TForm1 *Form1;struct userInfo{ TCHAR name[20]; TCHAR passWord[20] ;};//---------------------------------------__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner){}//---------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender){ userInfo aUser ; _tcscpy(aUser.name,"张三") ; _tcscpy(aUser.passWord,"123") ; TBytes buffer; buffer=RawToBytes(&aUser, sizeof(userInfo)); IdTCPClient1->Port = 8000; IdTCPClient1->Host = "127.0.0.1"; IdTCPClient1->Connect(); IdTCPClient1->IOHandler->Write(buffer); IdTCPClient1->Disconnect(); //、delete aUser ;}//---------------------------------------void __fastcall TForm1::IdTCPServer1Execute(TIdContext *AContext){ userInfo aUser; TBytes buffer; try { AContext->Connection->IOHandler->ReadBytes(buffer, sizeof(userInfo), false); BytesToRaw(buffer, &aUser, sizeof(userInfo)); Memo1->Lines->Add("用户名:"+AnsiString(aUser.name)); Memo1->Lines->Add("密码:"+AnsiString(aUser.passWord)); } catch (Exception &e) { Memo1->Lines->Add("读取登录信息异常"); }}//---------------------------------------void __fastcall TForm1::FormShow(TObject *Sender){ IdTCPServer1->DefaultPort = 8000; IdTCPServer1->Active = true ;}//---------------------------------------