[Gambas-user] Network Programming

T Lee Davidson t.lee.davidson at gmail.com
Tue May 18 16:23:22 CEST 2021


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)
   End If

> 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).

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?

> On Mon, May 17, 2021 at 5:49 PM T Lee Davidson <t.lee.davidson at gmail.com <mailto:t.lee.davidson at gmail.com>> wrote:
>     On 5/17/21 3:19 PM, John Dovey wrote:
>      > Does anyone know what happened to this page?
>      > http://gambaswiki.org/wiki/tutorial/chat <http://gambaswiki.org/wiki/tutorial/chat>
>     <http://gambaswiki.org/wiki/tutorial/chat <http://gambaswiki.org/wiki/tutorial/chat>>
>      >
>      > It seems to be exactly what I needed as a sample for something else I want to do.
>      > Thanks
>      > John
>     I never even knew that page was there, lol. Anyway, there is example code at http://gambaswiki.org/wiki/doc/network#t13
>     <http://gambaswiki.org/wiki/doc/network#t13> that
>     could likely be used as a basis for your needs. I took that code and reworked it a bit to make a simple chat server. I have not
>     tested it so it may have (read, probably has) bugs.
>     ' Gambas class file
>     PUBLIC Clients AS Object[]
>     PUBLIC Srv AS ServerSocket
>     PUBLIC SUB FMain_Open()
>         Clients = NEW Object[]
>         Srv = NEW ServerSocket AS "Srv"
>         Srv.Port=3450
>         Srv.Type = Net.Internet
>         Srv.Listen()
>     END
>     PUBLIC SUB Srv_Connection(Host AS String)
>         PRINT "Accepting connection from --> " & Host
>         Clients.Add(Srv.Accept())
>     END
>     PUBLIC SUB Socket_Read()
>         DIM sCad AS String
>         READ #LAST, sCad, Lof(LAST)
>         FOR EACH oSocket as Socket in Clients
>           IF oSocket.RemoteHost = LAST.RemoteHost THEN ' Don't echo to sender
>             Continue
>           END IF
>           WRITE #oSocket, sCad, Len(sCad)
>         NEXT
>     END
>     PUBLIC SUB Socket_Closed()
>         PRINT "Connection closed"
>         Clients.Remove(Clients.Find(LAST))
>     END
>     -- 
>     Lee


More information about the User mailing list