BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Why acces violation at end of program using TServerSocket??

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock
View previous topic :: View next topic  
Author Message
Bo Berglund
Guest





PostPosted: Sat Sep 27, 2003 1:47 pm    Post subject: Why acces violation at end of program using TServerSocket?? Reply with quote



I am trying to write a socket server for remote usage of local RS232
ports. So I have created a server class that creates a TServerSocket
in the constructor and then I start listening on a separate Start
call.
My problem is that although the basic setup works I am always getting
an access violation when I close my program. Why does this happen?

If I execute the Start method and then the Stop method but there is no
connection done (and there is no handler created) I still get a
violation at program end.
If I just start then exit my program then there is no error. It
appears that doing a TServerSocket.Open sets the system up for access
violation....
Even though I am doing TServerSocket.Close (in the Stop method) I
still get the violation.

Any help appreciated!

/Bo

Below is a shortened version of the class code:

TPortServer = class(TObject)
private
FOwner: TObject;
FServerSocket: TServerSocket;
procedure OnClientConnect(Sender: TObject; Socket:
TCustomWinSocket);
procedure AddPortHandler(Socket: TCustomWinSocket);
procedure SetPort(Port: integer);
public
constructor Create;
destructor Destroy; override;
property ServerPort: integer read FServerPort write SetPort;
function Start: Boolean;
function Stop: Boolean;
end;

constructor TPortServer.Create;
begin
FServerSocket := TServerSocket.Create(NIL);
FServerSocket.OnClientConnect := OnClientConnect;
FServerPort := PORTSERVER_PORT;
end;

destructor TPortServer.Destroy;
begin
if Assigned(FServerSocket) then
begin
if FServerSocket.Active then
FServerSocket.Active := false;
FServerSocket.Free;
end;
inherited;
end;

function TPortServer.Start: Boolean;
begin
if FServerSocket.Active then
Result := true
else
begin
try
FServerSocket.Port := FServerPort;
FServerSocket.Open;
Result:= True;
except
on E: Exception do
begin
LogError('PortServer - Exception on start: ' + E.Message);
Result:= False;
end;
end;
end;
end;

function TPortServer.Stop: Boolean;
begin
try
FServerSocket.Close;
while FServerSocket.Active do
Application.ProcessMessages;
Result:= True;
except
on E: Exception do
begin
LogError('PortServer - Exception on stop: ' + E.Message);
Result:= False;
end;
end;
end;

procedure TPortServer.OnClientConnect(Sender: TObject; Socket:
TCustomWinSocket);
var
PortHandl: TPortHandler;
begin
AddPortHandler(Socket);
end;


Back to top
Bo Berglund
Guest





PostPosted: Sun Sep 28, 2003 7:34 am    Post subject: Re: Why acces violation at end of program using TServerSocke Reply with quote



Disregard post!
I fixed it by inheriting from TComponent instead of TObject.
And I had a buggy Registry read there too.
Sorry for wasting bandwidth!

/Bo

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.