[fpc-pascal]Sockets Unit - Multiple Clients (Long)
ishmael at mylocalmusic.com
ishmael at mylocalmusic.com
Thu Sep 28 22:15:01 CEST 2000
Can someone help me out with the sockets unit. I've successfully created
a multi-client server using the NetDemo sample, but I'd like some further
details on how the SSOCKETS unit is intended to be used...
The following simple program works, but the server automatically STOPS
listening as soon a client connects. Where should the clients be managed?
During the OnConnect event? Perhaps there should be a list of clients and
each time one connects, it gets added to the list in the OnConnect event.
I'm just a little unclear as to how the connection is maintained. Each
new client should be able to send to the server and the server should
acknowledge and/or respond to each one.
If anyone can shed some light on the intentions of the SSOCKETS unit, it
would be greatly appreciated.
Thank you (and sorry for the long post)...
program ssockserv;
uses ssockets;
type
TSockServerApp = class(TObject)
private
FServer: TInetServer;
protected
procedure Listen;
public
procedure Run;
procedure OnConnectQuery(Sender: TObject; ASocket: Longint; Var Allow:
Boolean);
procedure OnConnect(Sender: TObject; Data: TSocketStream);
constructor Create(port: word);
end;
constructor TSockServerApp.Create(port: word);
begin
FServer := TInetServer.Create(port);
FServer.OnConnectQuery := @OnConnectQuery;
FServer.OnConnect := @OnConnect;
end;
procedure TSockServerApp.Listen;
begin
FServer.SetNonBlocking;
FServer.StartAccepting;
end;
procedure TSockServerApp.OnConnectQuery(Sender: TObject; ASocket:
Longint; Var Allow: Boolean);
begin
Writeln('OnConnectQuery Event.');
end;
procedure TSockServerApp.OnConnect(Sender: TObject; Data:
TSocketStream);
begin
Writeln('OnConnect Event.');
end;
procedure TSockServerApp.Run;
begin
Self.Listen;
end;
const
DEFAULT_PORT = 6543;
var
MyApplication: TSockServerApp;
begin
MyApplication := TSockServerApp.Create(DEFAULT_PORT);
MyApplication.Run;
MyApplication.Free;
end.
More information about the fpc-pascal
mailing list