Search code examples
ipcrealbasicxojo

Xojo IPCSocket connecting multiple times


I have done some network programming and IPC programming before, but never in Xojo. I have C++ and Python experience, but I am pretty new to Xojo and pretty lost.

Problem

Connecting two applications:

Application1 and Application2 are connected by IPCSocket. Application1 is the server that is listening.

Application2 is the client that is connected.

When Application2 is disconnected, and then restarted, it will not connect to Application1 any longer.

Inspection of Application1 in the debugger shows that error 102 Lost Connection is the LastErrorCode

Scenario

We have an app that is in Xojo. I have been following the IPCSocket example project that is included as well as using the documentation for the IPCSocket.

I can get the socket to work, but I can not get the socket to successfully disconnect and then reconnect to the same local server. The example does not give any indication on how this should be done. The documentation is what I would consider to be sparse.

Is it possible to disconnect a Xojo IPCSocket and reconnect similar to the way a Server connecting over TCP would function? or... Am I wasting my time attempting this route and should I explore another means of IPC?


Solution

  • Without looking at your code, I believe all you have to do is to implement the Error event at both ends, and if it fires, you close the IPCSocket and then re-open it (i.e. with calling Listen on one end, and calling Connect on the other). The Error event is effectively also acting as a "disconncted" event.

    Also, before you can Listen again, you have to delete the file at the specified Path, or the Listen will cause an error (at least on OS X).

    The way I use IPCSocket, the connecting app (app2) is always launched as a helper app by the listener (app1) using the Shell class in Async mode (theShell.Mode = 1). In that scenario, you can also notice if the helper app has quit by implementing the Shell class' Completed event. And if your server (app1) quits or crashes, the Shell will automatically quit the app2 as well. So, using Shell and IPCSocket in conjunction, with implementing said events, is the safest way to make sure you have full control over the lifetimes of both ends.