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
Here’s the documentation on how to use SocketCore.Close:
http://documentation.xojo.com/index.php/SocketCore.Close
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()
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?
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[/code]
Then get the reference back in the DataTimeoutTimer.Action event:
[code]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
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
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://documentation.xojo.com/index.php/ServerSocket.ActiveConnections for more info.