[Gambas-user] Network Programming

John Dovey dovey.john at gmail.com
Tue May 18 16:43:38 CEST 2021


On Tue, 18 May 2021 at 09:25, T Lee Davidson <t.lee.davidson at gmail.com>
wrote:

> Inline.
>
> On 5/18/21 3:10 AM, John Dovey wrote:
> > I created this as a separate class in my project and instantiated the
> class from my main form. When I close my main form, the
> > program doesn’t. What do I need to do to kill the class?
>
> Gambas does not automatically close open sockets when a program
> terminates. You will have to explicitly call
> ServerSocket.Close(). Perhaps in a subroutine in the class like:
>
> Public Sub CloseServer()
>    If Srv Then ' Make sure object exists (or perhaps: If Not IsNull(Srv)
> Then)
>      Srv.Close()
>    End If
> End
>
> Perfect. I changed my Form_Close() to include the lines
If Server Then ' From:  Static Server As ClsServer
    Server.Srv.Close()
  End If
That works perfectly.

>
> > Also, I’m bother by the use of “LAST”. My concept is that I was wanting
> to have the server accept connections as they arrive,
> > and then manage the full session with the client, but the LAST is
> confusing me. Do I need to track the socket object as a list
> > of “connections”?
>
> The socket objects representing connections to unique clients are
> contained in an array of objects (ie. the Clients object
> array). When a connection request is received, a unique socket is created
> with ServerSocket.Accept() and is added to the Clients
> array (list?) with Clients.Add(); one socket per unique client. All the
> sockets created are attached to the "Socket" event
> observer. [http://gambaswiki.org/wiki/comp/gb.net/serversocket/accept]
>
> When a message is received from a client, the Socket_Read() event handler
> is triggered. Now, since we don't know exactly which
> client sent the message, we can use the Last keyword which represents
> whichever server socket received the current message from
> its connected client. And since Last, in this case, is a handle on a
> socket, all the properties and methods of Socket are
> available to us (ie. Last.RemoteHost).
>
> Thank you for the explanation. I'm going to have to step through my
program in debug mode until I understand exactly what is happening and what
information is stored where, because that's still not entirely clear to me.


> I'm not sure exactly what you mean by, "manage the full session with the
> client." Isn't the function of a chat server to
> basically forward a message sent by one client to all the other clients?
> What management of individual clients would be necessary?
>
>
I'm not quite sure what would be "necessary". That's what I'm trying to
figure out.  One aspect of where I'm heading is to have the server accept a
remote connection, then spawn off a brand new form which will echo locally
what is happening on the remote. The idea I have here, and I'm really
stumbling in the dark, is I'd like to have a client-server setup where I
can generate cairo commands on the server and send them to the remote
client where they would be rendered. I want to also render them locally so
that I can send (on the client's request) an SVG/PNG/PDF version of what's
been rendered. I did something similar many years ago using Winsock
connections, but there I had to maintain my own in-memory table of
connections etc.

To start with, I'd like to be able to just Telnet to the port I'm listening
on, receive a text menu, and then send a menu choice and have the server
respond depending on that choice. I'll figure it out eventually.

Thanks again for all your help.
John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20210518/d2d533d4/attachment-0001.htm>


More information about the User mailing list