From Xojo Documentation
Jump to: navigation, search

Class (inherits from Object)

Used for interprocess communication between two applications on the same computer. Use TCPSocket for interprocess communication between applications on different computers.

Connected DataAvailable Error

BytesAvailable ReadOnlyProperty.png Handle ReadOnlyProperty.png LastErrorCode ReadOnlyProperty.png
BytesLeftToSend ReadOnlyProperty.png IsConnected ReadOnlyProperty.png Path

Close Listen ReadAll
Connect LookAhead ReadError
EOF Poll Write
Flush Read WriteError

Class Constants

These constants indicate which error occurred. To determine which error occurred, compare the value of LastErrorCode to these constants. They are the same error codes returned by SocketCore.

Error Code Class Constant
100 OpenDriverError
102 LostConnection
103 NameResolutionError
105 AddressInUseError
106 InvalidStateError
107 InvalidPortError
108 OutOfMemoryError


The IPCSocket class implements the Readable and Writeable class interfaces.


IPCSocket works similarly to a regular TCPSocket, but instead of using an IP address and Port number, a local file path is used, leading to a so-called socket file. The same path must used on both ends of the connection, and it should preferrably be a unique file location inside a temporary folder that's not usually visible to the user. The file might remain in existance even after closing the connection, so you should delete any leftover files from previous connections when you make a new connection.

To connect, first one side must invoke Listen after having set the Path. Then the other can connect to the socket file using the Connect method.

To terminate the connection, call the Close method. This will invoke the Error method on the other end, with its LastErrorCode set to 102.


The time between the message being sent and the DataAvailable being fired can be as low as 350 microseconds but it varies considerably depending on the type of your application (GUI or Console), what is currently happening on your system, accesses to your hard drive...

For GUI applications, it may take more than 80000 microseconds (80 milliseconds). If your application needs high performance (low latency) exchanging data over a socket, consider these optimizations:

  • Call the Flush method to push out written data without further delay.
  • Use a Timer with a very low Period value, e.g. 1, to call the Poll method in order to have the DataAvailable event fire as quickly as possible after receiving new data. The Timer can be anywhere in your app, as long as it's active and in continuous mode. Keep in mind that such a fast firing Timer will raise the CPU consumption of your application significantly, though. Fine-tune this adequately.

IPCSockets used in a Console application react faster than in a GUI application, most notably because there is no event loop to take care of the graphical interface and user events. In the following code:

In the Run event of a Console application
App.DoEvents n //n = 10 or 1

When n=10, the maximum latency was 20000 microseconds (20 ms). When n=1, the maximum latency was almost 10 times less (2500 microseconds) but the CPU time consumption raised to 3-4%. Compare the values to those given for a GUI application above. The "App.DoEvents" line does the polling of the IPCSocket.

Note: the values given were measured on a Core2Duo iMac at 2.16 GHz with a SSD main hard drive running Mac OS X 10.6.8. Your experience may vary a lot.


The following example establishes communications with another copy of the application. The main window has a ListBox for displaying information sent by the other copy of the application, status information, and error messages. There are PushButtons for listening, connecting, and for closing the connection. There is a TextArea that you use to enter the messages that you send to the other instance of the application. It sends the contents of the TextArea when the TextArea has the focus and the Enter key (Return on Macintosh) is pressed. An IPCSocket has been added to the window.

Before two ends can connect, they both need to set their path to the same location, using a unique name, e.g. like this:

MyIPCSocket.Path = SpecialFolder.Temporary.Child("com.mydomain.appname.socket").NativePath

The Connect button calls the Connect method and disables itself and the Listen button:


Me.Enabled = False
ListenButton.Enabled = False

The Listen button calls the Listen method and disables itself and the Connect button.


Me.Enabled = False
ConnectButton.Enabled = False

The Close button calls the Close method to close the connection:


The KeyDown event handler for the TextArea is:

If Key = Chr(13) or Key = Chr(3) then // Return or Enter pressed?
IPCSocket1.Write( Me.Text )
IPCSocket1.Flush // makes sure the data gets sent without delay
Return True
Return False

The event handlers for the IPCSocket have code that populate the Listbox. Its Connected event handler is:

ListBox1.Addrow "Connected"

Its DataAvailable event handler is:

ListBox1.AddRow "DataAvailable: " + Me.ReadAll

Its Error event handler is:

ListBox1.AddRow "Error: " + Str( Me.LastErrorCode )

To run the example, launch two copies of the application. In one instance of the application, click the Listen button. Then click the Connect button in the other instance of the application. You should get the message "Connected" in the ListBoxes. Then you can send messages via the TextField.

See Also

SocketCore, TCPSocket classes.

Personal tools

Starting Out
Dig Deeper
More Help