Bonjour je travaille depuis quelques temps avec les composants TServerSocket et TClientSocket sous BDS 2006 mais sur ma derniere application destinee a la creation d'un jeu j'ai beau chercher je ne trouve pas pourquoi mon client et mon serveur ne "dialoguent" pas.
Le serveur et le client ont l'air de se connecter sans problemes ( les deux booleens Socket.Connected sont a true) mais je ne comprends pas pourquoi seule la fonction du client OnConnect fonctionne les autres ne repondent pas
Voici le Code
Merci d avance pour vos reponses
unit reseau;
interface
uses
ScktComp, Sockets, Classes, Windows;
type TClient = class
Client: TClientSocket;
public
procedure ClientConnect(Sender: Tobject; Socket:TCustomWinsocket);
constructor create();
procedure ClientRead(Sender: Tobject; Socket:TCustomWinsocket);
end;
type TServer = class
Server: TServerSocket;
public
constructor create();
procedure ServeurConnect(Sender:TObject; Socket: TCustomWinSocket);
end;
function init_reseau: boolean;
function connexion_client(ip: string): boolean;
function getIp: string;
var
Server1: TServer;
Client1: Tclient;
implementation
function getIP: string;
begin
With TCustomIpClient.Create(Nil) do
begin
Result:= LocalHostAddr;
Free;
end;
end;
constructor TClient.create;
begin
Client:= TClientSocket.Create(Nil);
Client.Active:= False;
Client.ClientType:= ctNonBlocking;
Client.Port:= 4242;
Client.OnConnect:= ClientConnect;
Client.OnRead:= ClientRead;
end;
constructor TServer.create();
begin
Server:= TServerSocket.Create(Nil);
Server.Active:= False;
Server.ServerType:= stNonBlocking;
Server.Port:= 4242;
Server.OnClientConnect := ServeurConnect;
end;
function init_reseau: boolean;
var
host, mode: char;
Ip: string;
begin
Result:= true;
Writeln('Mode Solo ou Multi (S/M)');
repeat readln(mode)
until (mode='S') or (mode='M');
if mode='M' then
begin
Writeln('Heberger la partie? (O/N)');
repeat readln(host)
until (host='O') or (host='N');
if host='O' then
begin
Server1:= TServer.Create;
Server1.Server.Active:= true;
Writeln('Partie en ligne sur ',getip);
Result:= true;
connexion_client(getip);
end
else
begin
Writeln('Ip de la partie a joindre?');
Readln(ip);
Connexion_client(ip);
end;
end
else
Result:= True;
end;
function connexion_client(ip: string): boolean;
begin
Client1:= TClient.Create;
Client1.Client.Address:= ip;
Client1.Client.Active:= True;
repeat Client1.Client.Socket.Connect(0)
until Client1.Client.Socket.Connected;
Result:= True;
end;
procedure TClient.ClientConnect(Sender: Tobject; Socket:TCustomWinsocket);
begin
Writeln('Connecte');
end;
procedure TServer.ServeurConnect(Sender:TObject; Socket: TCustomWinSocket);
var
i: integer ;
map1: string;
begin
Writeln('client arrive');
end;
procedure TCLient.ClientRead(Sender: Tobject; Socket:TCustomWinsocket);
var
line, subline: string;
i:integer;
begin
line:= Socket.ReceiveText;
Writeln(line);
end;
end.