試圖通過TMemoryStream
編寫簡單的客戶端和服務器接收/發送數據。當我按下客戶端btnTestClick
中的按鈕時,它出現錯誤 Access violation at address 005D5581 in module 'Client.exe'. Write of adress 00000000
。我做錯了什麼?訪問衝突到德爾福的服務器
順便說一句客戶端服務器連接工作好吧我細細cuz IdTCPClient1Connected
功能正在寫'Client Connected!'
。
客戶端代碼
procedure TForm1.btnTestClick(Sender: TObject);
var
msRecInfo: TMemoryStream;
arrOf: array of Integer; i:integer;
begin
for i := 0 to 10 do
arrOf[i]:=random(100);
msRecInfo:= TMemoryStream.Create;
try
msRecInfo.Write(arrOf, SizeOf(arrOf));
idTCPClient1.IOHandler.Write(msRecInfo);
finally
msRecInfo.Free;
end;
end;
end
Server代碼
procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
msRecInfo: TMemoryStream;
arrOf: array of Integer; i:integer;
begin
msRecInfo:= TMemoryStream.Create;
try
AContext.Connection.IOHandler.ReadStream(msRecInfo, -1, False);
msRecInfo.Position := 0;
msRecInfo.Read(arrof, SizeOf(arrof));
finally
memo1.Lines.Add(IntToStr(arrOf[1]));
msRecInfo.Free;
end;
//AContext.Connection.IOHandler.ReadStream(msRecInfo, SizeOf(arrOf));
//msRecInfo.Read(arrOf, sizeOf(arrOf));
end;
首先看你沒有設置你的arrOf的長度,還需要更正SizeOf(arrOf)。 –
爲什麼你包含這麼多不相關的代碼?你已經知道這個問題與服務器無關,不是嗎?你知道這個問題僅限於btnTestClick方法,所以你應該包含在這裏。 –
@RobKennedy對不起,我只是用ctrl + a cuz它更快 – DanilGholtsman