TCPSocket on Loopback address to avoid firewall warnings

I’m testing out an app that uses a HTMLViewer (or the OLE equivalent) in one app to talk to another app, with the other app serving as a mini HTTP server.

I’ve got this working with a combination of ServerSocket and TCPSockets. The only problem is that when I launch the app, I’m getting the Windows Defender Firewall alert.

From reading these threads:

It sounds like the solution is to set up my ServerSocket and TCPSockets using the “Loopback Interface”?

Additional problem - I’m using an old version of RealStudio which doesn’t have NetworkInterface.Loopback.

Is this something I can do with Declares?

You should be able to, whether the framework will like it is another matter :slight_smile:

Try calling bind with a custom sockaddr set up for loopback then call listen.

That should be the same calls that the framework uses to get into the listen state.

I’ve not done this before though so its a bit of a guess, let me know if you get stuck and I’ll take a look tomorrow.

Thanks Julian - are you thinking of a pure-declare solution, or would I calling these functions on an existing Xojo socket class?

Pure declare, it should work unless xojo triggers some internal states when setting TCPSocket.NetworkInterface and calling TCPSocket.Listen which you wouldn’t be doing, but I don’t really see the reason for us being provided with TCPSocket.Handle if that were going to be the case so fingers crossed :slight_smile:

Oh, and just to be clear, I need to set up my ServerSocket to listen on the loopback address. Are you saying I’d forget about ServerSocket, and instead modify the TCPSocket, and then set that up as my server socket?

Ah you might be able to do it with serversocket too if you want to use that, sorry I overlooked that in you OP. Worth a tinker as it also exposes the handle. I’ll have a poke around with it in the morning if you don’t post back :slight_smile:

Update - i’m having partial success - I was able to add some declares to the bind and listen functions, and they seem to work. However there are some problems:

  • with a fresh ServerSocket, the value of ServerSocket.Handle = -1. It’s not valid until after you call [b]ServerSocket.Listen/b
  • however, after you call Listen, it seems that the socket has already been Bound to the network interface, and calling the declare to the [b]Ws2_32 bind/b function returns an error.
  • I’ve tried calling Socket.Listen followed by Socket.StopListening with similar results.

My next thought was to try to get the NetworkInterface object (which returns the LAN IP address of the machine) and twiddle the bytes to change it to the loopback address of 127.0.0.1.

The problem here is that NetworkInterface has no Handle property, so I can’t figure out how to get a pointer to it.

Any ideas?

I’ve just started to look at this too, you’re right about ServerSocket, thats a no go because of the lack of “getting into its guts” so to speak.

Getting inside a TCPSocket is also a no go because the socket isn’t created until Listen is called as the Handle returned is always -1 until then and by then its too late.

I’m currently working up my own loopback socket, I’ve got it all set up, I’m just prettying it up a bit.

Sorry Michael, I got sidetracked, here’s some code that should get you going:

https://www.dropbox.com/s/lumt98gxhn0sfh9/TestSocketListenOnLoopback.xojo_binary_project?dl=1

it’s just an example of listening, receiving and sending some data. You’ll need to wrap it up how you need it but it doesn’t trigger the firewall alert.

Not ideal, but it might get you where you need to go.

Thank you - that looks good, and is a pure-declare solution. I’ll see what I can make of it.

Maybe I missed it, but wouldn’t it be better to open that port for your app? If this is something you’re going to distribute, an installer can create the firewall rule for you.

In general, yes, but this is a weird situation, where I’m going to have 2 instances of an app talking to each other. Normally I’d use IPCSockets, but since one instance is talking to the other over HTTP I need something else.