Console App to handle TCPSockets


Did someone managed to have a successful Console app that can be used as a sever to manage some TCP server socket and some communication between a client and a server ?
If Yes, is there an example for this ? so far nothing found on XOJO or on the forum.

How do you handle the event handlers on a console app ? where do you put the code ? from what I see you cannot have the Socked called from the Module and you need to Set it as property in App but once you do that you loose most of the Event handlers , Is there a way to manage that ?

To be more specific I need a console app to be able to communicate via easytcp sockets with another app which is a desktop app.

This can be done as well on a web app ? and the app to run without any page ? if yes I assume that is done same way as a console app.


Two standard ways:

1: create a new class in your project (name: “CMySocket”, Super: “TCPSocket”).
Fill the events of “CMySocket” as you want.
Add a property in the app class (name: “Sock”, Type: “CMySocket”).
In app.Run, set Sock as a new CMySocket and don’t forget to have a loop with app.DoEvents inside it (otherwise, no socket can receive data unless you manually poll them).

2: keep the property in the app class as you tried (“Sock as TCPSocket”) without making an extra class.
Add methods to your app class to replace the socket’s events (add the ones you need), like this:
Sub CustomDataAvailable(MySocket as TCPSocket)
End Sub
Sub CustomError(MySocket as TCPSocket,e as RuntimeException)
End Sub
(note the method’s signature: always an object of type TCPSocket (representing the source socket) followed by the regular parameters of the event)
In the event, put this:
Sock=new TCPSocket
AddHandler Sock.DataAvailable,AddressOf CustomDataAvailable 'You may want to use WeakAddressOf instead
AddHandler Sock.Error,AddressOf CustomError
And fill the custom methods as if they were the Sock’s events (they’ll be called the same way).


Hi, thanks for sharing, that was my first idea as well only that I need to use ServerSocket along with EasyTCPSocket so I needed to create a module where I have all those and in the end I need to handle the ServerSocket , but I guess I got the idea , it is similar to what I did on the module, now it’s just a matter of matching the event definitions and event handlers.

Thanks again.

You’re welcome.
Since you have several related classes, it’s indeed a good idea to group them together.
A module won’t hold custom classes, true, but you can group them in a folder instead :wink:

Hint: when you define your methods to receive events, do one of these/both/similar things:
1: name your methods with the name of your class (e.g. MySocketDataAvailable or DataAvailableForMySocket) so you know which one is for what (as you may end up with similar methods for several objects, like Connected).
2: you may group your methods together in modules, one module per socket/object/functionality.