I am making a Matchmaking server for my custom Battleship game. I am a beginner at network coding, so I am sorry in advance for dumb questions. :P
Now closer to the topic. I use default Delphi Server/Client Socket component. I have my array declared in the public section (clients: array of TCustomWinSocket;
), then on the main frame startup I set the length to zero (setLength(clients, 0);
). Now the confusing part for me: whenever I try to access any element of array program throws an Access Violation exception. I have checked in any known way, program NEVER exceeds the length of the array.
Here is code example which throws exception:
procedure Tmain.Button1Click(Sender: TObject);
var
i: integer;
begin
for i := 0 to length(clients) do begin
if assigned(clients[i]) then begin
showmessage(IntToStr(i));
showmessage(IntToStr(i) + ': ' + clients[i].RemoteAddress);
end;
end;
end;
Another example:
procedure Tmain.serverClientConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
addLog('(' + Socket.RemoteAddress + ':' + IntToStr(Socket.RemotePort) + ') Клиент подключился');
if length(clients) <> 0 then begin
showmessage(IntToStr(length(clients)));
setLength(clients, length(clients) + 1);
showmessage(IntToStr(length(clients)));
clients[length(clients)] := Socket;
end
else if length(clients) = 0 then begin
showmessage(IntToStr(length(clients)));
clients[0] := Socket;
end;
end;
Basically, every time I use this array it gives an access violation. I can't get it, I limit the code to stay in the array length, but it does not. Or, perhaps, it is as usual my stupidity that caused it?
Any way, I could really use some help.
Thanks in advance! :)