Board index » delphi » Problem with writing the network game

Problem with writing the network game


2007-11-17 10:58:54 PM
delphi239
I write the network game.
I use the IdTCPClient, IdTCPServer and IdThreadMgrDefault
Each client sends on server from 10 to 20 messages at second.
On log of the server is seen that he haves time to all are accepted and all
answer.
Practically all messages server it is necessary to copy on all client.
In a certain time, but not immediately, clients cease to take information
from server. (First one, in time other client)
What exactly occurs in this moment and how this fix?
Thank you!
Excuse me for my bad english :)
 
 

Re:Problem with writing the network game

"UWork" <XXXX@XXXXX.COM>writes
Quote
What exactly occurs in this moment and how this fix?
Without showing your code, there is no way of knowing.
If you want an answer, show the relevant code.
 

Re:Problem with writing the network game

This is a part of code with comments:
while fMain.IdTCPClient1.Connected do begin
fMain.Label2.Caption:=fMain.Label2.Caption+'+';// for 'mini-log' :)
fMain.IdTCPClient1.ReadStrings(UpdateInfo); // When performing
mini-log shows that program does not pass hereinafter this line
fMain.Label2.Caption:=fMain.Label2.Caption+'=';// for 'mini-log' :)
Synchronize(ServerOnUpdate);
fMain.Label2.Caption:=fMain.Label2.Caption+':';// for 'mini-log' :)
UpdateInfo.Clear;
if length(fMain.Label2.Caption)>20 then begin
fMain.Label2.Caption:='';
end;
end;
This is full code of the thread which realizes receiving
information from server (please copy-past to pas-ifle - for best viewing -
attached files not resolved on this server :( )
unit UClientThread;
interface
uses
Classes, IdTCPClient;
const
stConnected=1;
stNotConnected=2;
stDisconnected=3;
type
TClientThread = class(TThread)
State:integer;
UpdateInfo:TStrings;
procedure SetText(iText:string);
procedure OnConnected;
procedure OnNotConnected;
procedure OnDisconnected;
procedure ServerOnUpdate;
private
{ Private declarations }
protected
procedure Execute; override;
end;
var
ClientThread:TClientThread;
procedure MakeConnection;
procedure Disconnect;
procedure SendString(iString:string);
procedure SendStrings(iStrings:TStrings);
implementation
uses Main, UWorld, IdTCPConnection, SysUtils;
procedure MakeConnection;
begin
if Assigned(ClientThread) then begin
Exit;
end;
ClientThread := TClientThread.Create(True);
ClientThread.FreeOnTerminate := True;
ClientThread.Priority := tpNormal;
ClientThread.Resume;
end;
procedure Disconnect;
begin
if Assigned(ClientThread) then begin
fMain.IdTCPClient1.DisconnectSocket;
ClientThread.OnDisconnected;
ClientThread.Terminate;
end;
end;
procedure SendString(iString:string);
var
Command:TStrings;
begin
if ClientThread.State<>stConnected then begin
Exit;
end;
Command:=TStringList.Create;
Command.Add(iString);
fMain.IdTCPClient1.WriteStrings(Command,true);
Command.Free;
//fMain.IdTCPClient1.WriteLn(iString);
end;
procedure SendStrings(iStrings:TStrings);
begin
if ClientThread.State<>stConnected then begin
Exit;
end;
fMain.IdTCPClient1.WriteStrings(iStrings,true);
end;
{ Important: Methods and properties of objects in visual components can only
be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TClientThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ TClientThread }
procedure TClientThread.Execute;
begin
{ Place thread code here }
try
fMain.IdTCPClient1.Connect(); // Подключаемся ?сервер?
except
Synchronize(OnNotConnected);
Exit;
end;
Synchronize(OnConnected);
UpdateInfo:=TStringList.Create;
with fMain do begin
while fMain.IdTCPClient1.Connected do begin
fMain.Label2.Caption:=fMain.Label2.Caption+'+';
fMain.IdTCPClient1.ReadStrings(UpdateInfo); // Принимае?данные ?
сервер?
fMain.Label2.Caption:=fMain.Label2.Caption+'=';
Synchronize(ServerOnUpdate);
fMain.Label2.Caption:=fMain.Label2.Caption+':';
UpdateInfo.Clear;
if length(fMain.Label2.Caption)>20 then begin
fMain.Label2.Caption:='';
end;
end;
end;
Synchronize(OnDisconnected);
{repeat
until Terminated;}
end;
procedure TClientThread.ServerOnUpdate;
begin
World.UpdateFromServer(UpdateInfo);
end;
procedure TClientThread.SetText(iText: string);
begin
fMain.Label1.Caption:=iText;
end;
procedure TClientThread.OnConnected;
begin
State:=stConnected;
SetText('Подключени?прошло успешн?');
end;
procedure TClientThread.OnNotConnected;
begin
State:=stNotConnected;
SetText('Подключиться не удалос?');
end;
procedure TClientThread.OnDisconnected;
begin
State:=stDisconnected;
SetText('Соединение завершен?');
end;
end.
 

Re:Problem with writing the network game

And one more info:
I use Delphi 7 and standart Indy components for this version of Delphi (9.0
If I do not mistaken)
 

Re:Problem with writing the network game

"UWork" <XXXX@XXXXX.COM>writes
Quote
This is a part of code with comments:
You did not show your server code, which is where the real problem is likely
at.
Quote
fMain.Label2.Caption:=fMain.Label2.Caption+'+';// for 'mini-log' :)
That is not thread-safe. You must use Synchronize() in order to access all
UI controls safely.
Quote
fMain.IdTCPClient1.ReadStrings(UpdateInfo);
When performing mini-log shows that program does not pass hereinafter this
line
Then the server has likely stopped sending data to the client. Which is all
the more reason why you need to show the actual server code
Gambit
 

Re:Problem with writing the network game

Upload Server-code to rapidshare.
In project use GLScene and GmLib - librarys.
But all code understanding without their connections.
rapidshare.com/files/70776442/Server.zip.html