TCP-Server example - Call a Method of Class / ServerSocket, Static reference to instance method:...

  1. 6 months ago

    Marco B

    11 Jan 2018 Pre-Release Testers, Xojo Pro Verden, Germany

    Hello,

    i am new with XoJo and i am trying to write a small TCP server. It handles a few connections and i want be able to send data or close specific connections from a Form. So i used ServerSocket. For example i have connected clients 1, 2, 3, 4, and 5 and want to close the TCP-connection to client 2.

    Problem: I want to do this from a timer which cares about the state of the clients.

    I tried a lot, like
    Call IncommingClientSocket.Close i
    where i is the ID of the object.

    But i get an error:
    Static reference to instance method: call this on an instance of class SocketCore.

    Is this a problem of ServerSocket/TCPSocket or do i make a mistace with handling the class?
    Please help, thank you.

    Here you can download the project:
    http://www.bungalski.de/xojo/TCPServer_Test.xojo_binary_project

    Add a property to the Modude_Modbus module, e.g. clientlist(20) As IncommingClientSocket. Then, in the Addsocket event, add the "Rueckgabe" instance to it:

    Function AddSocket() As TCPSocket
    //  Dim Rueckgabe As TCPSocket = New IncommingClientSocket(AktSocketNummer) //no
      Dim Rueckgabe As New IncommingClientSocket(AktSocketNummer) // yes
      SStatus(AktSocketNummer)=1
    
      clientlist(AktSocketNummer) = Rueckgabe // store Rueckgabe
      
      Return Rueckgabe
      
    End Function

    Then get the reference back in the DataTimeoutTimer.Action event:

    Sub Action()
      ' Check if data-timeout. If yes, disconnect client
      Dim i As Integer
      
      For i=1 To 20
        If DatenTimeout(i)>0 Then
          ' If timeout, then disconnect client
          If DatenTimeout(i)=1 Then
            System.DebugLog "Timeout: " + Str(i)
    
            Dim Rueckgabe As IncommingClientSocket = clientlist(i) // retrieve Rueckgabe 
            Rueckgabe.Close
            
          End If 
          DatenTimeout(i)=DatenTimeout(i)-1
        End If
      Next i  
    End Sub
  2. Tim P

    11 Jan 2018 Pre-Release Testers, XDC Speakers

    Here's the documentation on how to use SocketCore.Close:
    http://docs.xojo.com/index.php/SocketCore.Close

  3. Andrew L

    11 Jan 2018 San Francisco, CA, USA

    You need to call Close() on the IncommingClientSocket (TCPSocket), not the IncommingSocket (ServerSocket).

    In your project, look at the IncommingSocket.AddSocket event. Here, you're creating a IncommingClientSocket instance named "Rueckgabe" and returning it. You need to keep a reference to the "Rueckgabe" instance somewhere that can be accessed from the Timer, and then call its Close() method when you want to close it:

    Rueckgabe.Close()
  4. Marco B

    12 Jan 2018 Pre-Release Testers, Xojo Pro Verden, Germany

    Hello Andrew,
    how to save/keep the reference to Rueckgabe?
    Can you give me a code-example or the needed steps, please?
    Please note: I want to call something like Rueckgabe.Close() with an index to the socket should be closed.
    Or can you somewhere upload changed project?

  5. Andrew L

    12 Jan 2018 Answer San Francisco, CA, USA

    Add a property to the Modude_Modbus module, e.g. clientlist(20) As IncommingClientSocket. Then, in the Addsocket event, add the "Rueckgabe" instance to it:

    Function AddSocket() As TCPSocket
    //  Dim Rueckgabe As TCPSocket = New IncommingClientSocket(AktSocketNummer) //no
      Dim Rueckgabe As New IncommingClientSocket(AktSocketNummer) // yes
      SStatus(AktSocketNummer)=1
    
      clientlist(AktSocketNummer) = Rueckgabe // store Rueckgabe
      
      Return Rueckgabe
      
    End Function

    Then get the reference back in the DataTimeoutTimer.Action event:

    Sub Action()
      ' Check if data-timeout. If yes, disconnect client
      Dim i As Integer
      
      For i=1 To 20
        If DatenTimeout(i)>0 Then
          ' If timeout, then disconnect client
          If DatenTimeout(i)=1 Then
            System.DebugLog "Timeout: " + Str(i)
    
            Dim Rueckgabe As IncommingClientSocket = clientlist(i) // retrieve Rueckgabe 
            Rueckgabe.Close
            
          End If 
          DatenTimeout(i)=DatenTimeout(i)-1
        End If
      Next i  
    End Sub
  6. Marco B

    16 Jan 2018 Pre-Release Testers, Xojo Pro Verden, Germany

    Hello Andrew,

    thank you for quick help - it works :-)

    But:

    • I had to add the property at module Module_Modbus ClientList(20) As TCPSocket
    • To disconnect a TCP connection i had to use the Rueckgabe.Disconnect instead of Rueckgabe.Close.
    • I had to change/leave also
    Dim Rueckgabe As TCPSocket = ClientList(i) // retrieve Rueckgabe 
    Rueckgabe.Disconnect

    I will upload the new version next days. So this is a TCP-Server-Example with multiple connections (unfortunately not included in TCP-examples of XoJo).
    http://www.bungalski.de/xojo/TCPServer_Test.xojo_binary_project

  7. Wayne G

    16 Jan 2018 Pre-Release Testers, Xojo Pro New Zealand axisdirect.nz

    To access TCPSockets created using a ServerSocket you would use the ActiveConnections Method and cast the TCPSocket to your subclass.

    Your problem at

          ' PROBLEM HERE:
         Call IncommingClientSocket.Close i

    should become

          ' PROBLEM HERE:
          IncommingSocket.ActiveConnections(i).Close

    You can cast the TCP socket to your subclass by casting it as below

    Dim Rueckgabe As IncommingClientSocket = IncommingClientSocket(IncommingSocket.ActiveConnections(i))

    See http://docs.xojo.com/index.php/ServerSocket.ActiveConnections for more info.

or Sign Up to reply!