TServerSocket继承了TCustomSocket的Event方法,而这个方法就是派发各个事件的动态方法,通过override覆盖这个方法,可以给自己的类增加新的行为,下面是TCustomSocket的Event方法源码,供你参考:
procedure TCustomSocket.Event(Socket: TCustomWinSocket; SocketEvent: TSocketEvent);
begin
case SocketEvent of
seLookup: if Assigned(FOnLookup) then FOnLookup(Self, Socket);
seConnecting: if Assigned(FOnConnecting) then FOnConnecting(Self, Socket);
seConnect:
begin
FActive := True;
if Assigned(FOnConnect) then FOnConnect(Self, Socket);
end;
seListen:
begin
FActive := True;
if Assigned(FOnListen) then FOnListen(Self, Socket);
end;
seDisconnect:
begin
FActive := False;
if Assigned(FOnDisconnect) then FOnDisconnect(Self, Socket);
end;
seAccept: if Assigned(FOnAccept) then FOnAccept(Self, Socket);
seRead: if Assigned(FOnRead) then FOnRead(Self, Socket);
seWrite: if Assigned(FOnWrite) then FOnWrite(Self, Socket);
end;
end;
你只要声明一个TMyServerSocket的类,覆盖这个方法即可
type
TMySeverSocket = class (TServerSocket)
protedted
procedure Event(Socket: TCustomWinSocket; SocketEvent: TSocketEvent); override;
end;
实现部分如下写即可:
procedure TMyServerSocket.Event(Socket: TCustomWinSocket; SocketEvent: TSocketEvent);
begin
case SocketEvent of
seConnect:
begin
inherited Event(Socket, SocketEvent);//执行默认的操作
//这里添加你自己增加的代码.
end;
else
inherited Event(Socket, SocketEvent);
end;
end;