[Gambas-user] Writing classes to handle incoming socket connections with ServerSocket

Daniel daniel.campos at ...338...
Mon Apr 5 23:33:42 CEST 2004


El lun, 05-04-2004 a las 22:18, Ronan Chilvers escribió:
> Hi folks
> 
> I'm really interested in using Gambas to do some socket programming.  I want to write a 'dispatcher' class holding the ServerSocket, which hands off the incoming connections to a 'clienthandler' class to handle that particular connection.
> 
> The problem is I can't work out how to pass the sockets around between the classes.  This is the kind of thing I'm doing:-
> 
> Dispatcher.class
> 
> PRIVATE dSocket as ServerSocket
> 
> PUBLIC SUB _new(port)
> 	dSocket = new ServerSocket as "dSocket"
> END
> 
> PRIVATE SUB dSocket_Connection(dHost as String)
> 	DIM obj as ClientHandler
> 	obj = new ClientHandler(dSocket.Accept())
> END
> 
> ClientHandler.class
> PRIVATE cSocket as Socket
> 
> PUBLIC SUB _new(sock as Socket)
> 	cSocket = sock
> END
> 

I don't know if it is true in 0.92, but in previous versions,
you can not use a provate method to handle an event, so you
should use 'PUBLIC SUB cSocket_Read()' instead...

> PRIVATE SUB cSocket_Read()
> 	'Read data from socket and do stuff with it
> 	'***** This never executes *****
> END
> 



> That's the basic idea.  However when I connect to the listening port, the connection immediately drops and the _Read event never fires.  The ClientHandler object is created fine and the child socket is passed through fine, as I can read parameters such as RemoteHost from the cSocket property of the new ClientHandler object.
> 
> Any clues would be very much appreciated !  :-)
> 
> Cheers
> 
> Ronan





More information about the User mailing list