Crunch CRiSP File Editor 6 User Manual
Page 83

Page 83
/* We have either successfully or unsuccessfully */
/* connected to the remote host. */
/* We can tell the difference by whether the ipc_write */
/* is successful or not. */
if (ipc_write(ipc_id, "Hello\n") < 0) {
message("Writing data failed.");
}
/* If we keep the connection open at this point, make */
/* sure to unregister the IPC_TRIGGER_WRITE trigger */
/* otherwise it will keep firing. */
/* In this example, we simply close the connection when */
/* we are done. */
ipc_close(ipc_id);
}
The following example illustrates creating a service which other applications can connect to.
void
server_example()
{
int
ipc_id;
if ((ipc_id = ipc_create(IPC_TCP, "1234")) < 0) {
message("Failed to create server port");
return;
}
/* Register macro to handle new incoming connections. */
/* Note: we pass in the ipc_id as an argument so the */
/* callback knows which connection to use. */
ipc_register(ipc_id, IPC_TRIGGER_READ,
"new_connection " + ipc_id);
}
void
new_connection(int ipc_id)
{
int
ipc_id2;
stringstr;
if ((ipc_id2 = ipc_accept(ipc_id)) < 0) {
message("Failed to handle new connection request.");
return;
}
/* Read message request from the remote client */
str = ipc_read(ipc_id2);
message("Received '%s'", str);
/* Close down the connection */
ipc_close(ipc_id2);
}
{button See Also, ALink(ipc,,,)}