About ServerSockets, TCPSockets and EventHandling

I have instantiated an instance of a MySS::ServerSocket on my MainWindow.
AddSocket instantiates a MyTCPSocket returns it as a TCPSocket
MyTCPSocket class declares an event MyDataReceived, which it ‘raises’ (?) after the TCPSocket dataAvailable event is done.

How do I receive the MyDataRecieved event on my MainWindow? Who got the event?

On your MySS on MainWindow add an Event Handler.

You should see the MyDataReceived event if you defined it on the class and did not implement a handler for it elsewhere.

1 Like

I simply do not have my ‘Message’ event as ‘addable’.

Function AddSocket() Handles AddSocket as TCPSocket
  
  Dim ret As MLLP_HL7SegmentFactory = New MLLP_HL7SegmentFactory(mCurrentSocket)  
  mCurrentSocket = mCurrentSocket + 1 
  Return TCPSocket(ret)
  
End Function

This may be off-topic, but how do you get the class name to show in the IDE like this?

image

I ‘edited’ the screen shot, 'cause like the IDE doesn’t do that… but it would be a nice feature, wouldn’t it?

Have a close look again at Tim’s ScreenShot - and then at the one of your project structure.

Assuming you would add an MLLP_HL7SegmentFactory to your MainWindow, you could then add the EventHandler Message to MLLP_HL7SegmentFactory1 there.

You obviously can’t add the EventHandler Message to your MLPServerSocket1 on your MainWindow, because your MLPServerSocket does not define a (custom/own) EventDefinition Message.


Uff, a bit too tricky to explain in plain text. And different approaches that would be possible.

One possible way:

  • Start by adding EventDefinition Message to your MLLPServerSocket
  • When instantiating your MLLP_HL7SegmentFactory TCPSocket’s in there
    • AddHandler to receive their Message’s
    • likely you then want to forward their received messages by RaiseEvent Message

So the original Message is created in TCPSocket → listened by ServerSocket → forwarded to EventDefinition on ServerSocket → which can be added as an EventHandler on the Instance of your ServerSocket in your MainWindow to get the messages there.

1 Like

I’d prefer to use a Delegate here…

…anyway - here are two ways how to get a “Message” from TCPSocket → TCPServer → Window.

  • using Event
  • using Delegate

ServerSocketMessage.zip (524.2 KB)

Note: Code is quick&dirty, just to show the two mentioned approaches. Both could obviously be improved or done differently. The goal was really just to show the “flow” how a “Message” could go from TCPSocket to Window.

Run the example and click on the two Buttons (which opens the Browser at the listened ports, Sockets are writing a silly simple text to the browser).
And I hope the example shows how “Messages” in the TCPSockets are finally showing up on the Main Window.

I hope this example helps to understand what I’ve tried to write in the previous answer…

2 Likes