# IPC Client & Server reference

**URL:** <https://forum.legato.io/t/ipc-client-server-reference/123>\
**Category:** Legato Application Framework\
**Created:** [April 10, 2015, 8:07am UTC](https://forum.legato.io/t/ipc-client-server-reference/123 "2015-04-10T08:07:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielgomezdn](https://avatars.discourse-cdn.com/v4/letter/d/f9ae1b/32.png) [@danielgomezdn](https://forum.legato.io/u/danielgomezdn)\
**Post date:** [April 10, 2015, 8:07am UTC](https://forum.legato.io/t/ipc-client-server-reference/123/1 "2015-04-10T08:07:48Z")

</div>

Hi,

I have 3 components (1 server & 2 clients) executed in differents applications connected between IPCs. What I need is to know from server side what client is connected. I know the next Legato functions but I don’t know well how to use it:

```auto
MyServer_GetServiceRef() 
le_msg_GetClientUserId(sessionRef, &clientUserId);
```

I’ve check the le\_msg\_GetClientUserId example in the Legato documentation but I can’t implement well. This is the code:

```auto
le_msg_SessionRef_t *sessionRef;
*sessionRef = MyServer_GetServiceRef();
uid_t clientUserId;
if (le_msg_GetClientUserId(sessionRef, &clientUserId) != LE_OK)
{
}
else
{
    LE_INFO("My client has user ID %ud.", clientUserId);
}
```

The error: error: passing argument 1 of ‘le\_msg\_GetClientUserId’ from incompatible pointer type [-Werror]

Probably I’m getting service reference wrong but I’ve tried to do it with:

```auto
le_msg_SessionRef_t sessionRef;
sessionRef = MyServer_GetServiceRef();
uid_t clientUserId;
if (le_msg_GetClientUserId(sessionRef, &clientUserId) != LE_OK)
//...
```

In this case, the error: error: passing argument 1 of ‘le\_msg\_GetClientUserId’ from incompatible pointer type [-Werror]

What is the right way?

Note: If i force sessionRef to a integer value not using the function “MyServer\_GetServiceRef()” it seems to work but I think is not the right way.

```auto
le_msg_SessionRef_t sessionRef = 0;
uid_t clientUserId;
if (le_msg_GetClientUserId(sessionRef, &clientUserId) != LE_OK)
//...
```

---

<div class="post-metadata">

**Author:** ![danielgomezdn](https://avatars.discourse-cdn.com/v4/letter/d/f9ae1b/32.png) [@danielgomezdn](https://forum.legato.io/u/danielgomezdn)\
**Post date:** [April 15, 2015, 10:53am UTC](https://forum.legato.io/t/ipc-client-server-reference/123/2 "2015-04-15T10:53:52Z")

</div>

Hi,

Finally, the code worked doing this:

```auto
    le_msg_SessionRef_t sessionRef = 0;
    sessionRef = MyServer_GetClientSessionRef();

    uid_t clientUserId;
    if (le_msg_GetClientUserId(sessionRef, &clientUserId) != LE_OK)
    {
    }
    else
    {
        LE_INFO("My client has user ID %ud", clientUserId);
    }
```

The difference is the sessionRef initialization.

But now I have another question. I’m trying to do the same in other application. This application is unsandboxed and I can’t get the ID (only if I put the application sandboxed). Is this a Legato limitation?

Note: MyServer component is running within an unsandboxed application.

Thank you

---

<div class="post-metadata">

**Author:** ![kmurphy](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/kmurphy/32/65_2.png) [@kmurphy](https://forum.legato.io/u/kmurphy)\
**Post date:** [April 20, 2015, 7:50am UTC](https://forum.legato.io/t/ipc-client-server-reference/123/3 "2015-04-20T07:50:14Z")

</div>

When running as a unsandboxed app your UserID is 0, or root.

If you need a way to distinguish clients in this case, then perhaps you can add something to your API?

```auto
ENUM client_types
{
    CLIENT_A,
    CLIENT_B
};

FUNCTION SetClientId(client_types clientType);
```

```auto
le_msg_SessionRef_t ClientARef = NULL;
le_msg_SessionRef_t ClientBRef = NULL;

void MyServer_SetClientId(MyServer_client_types clientType)
{
    le_msg_SessionRef_t clientSessionRef = MyServer_GetClientSessionRef();

    switch (clientType)
    {
        case MYSERVER_CLIENT_A:
            ClientARef = clientSessionRef;
            break;

        case MYSERVER_CLIENT_B:
            ClientBRef = clientSessionRef;
            break;
    }
}
```

-Kelly

---

<div class="post-metadata">

**Author:** ![danielgomezdn](https://avatars.discourse-cdn.com/v4/letter/d/f9ae1b/32.png) [@danielgomezdn](https://forum.legato.io/u/danielgomezdn)\
**Post date:** [April 28, 2015, 10:36am UTC](https://forum.legato.io/t/ipc-client-server-reference/123/4 "2015-04-28T10:36:01Z")

</div>

Hi,

Thanks for your support and your suggestion. I’m trying to solve in this way but I’ve never created a ENUM or other within an .api file. Following the instructions and the documentation here:  
[url][http://www.legato.io/legato-docs/15\_01\_1/language\_independent\_syntax.html[/url]](http://www.legato.io/legato-docs/15_01_1/language_independent_syntax.html%5B/url%5D)

This is what I have:

```auto
ENUM client_types
{
    CLIENT_A,
    CLIENT_B
};

FUNCTION SetClientId
(
        client_types clientType IN
)
```

```auto
void MyServer_SetClientId(MyServer_client_types_t clientType) { .. }
```

I had to change MyServer\_client\_types → MyServer\_client\_types\_t because the code didn’t compile. Is it right?

The reason is because of the content of this file: MyServer\_server.h

```auto
typedef enum
{
    MYSERVER_CLIENT_A,
    MYSERVER_CLIENT_B
}
MyServer_client_type_t;
```

So, at compilation time, I don’t have any errors but when I’m trying to execute the client (and server is running) this is the log file:

```auto
02:20:53 =ERR= | serviceDirectory[613]/serviceDirectory_exe T=main | serviceDirectory.c DispatchToServer() 739 | Client (uid 1012 'appMyClient', pid 28896) disagrees with server (uid 0 'root', pid 28561) on protocol ID of service 'MyClient'
02:20:53 *EMR* | MyClient[28896]/framework T=main | messagingSession.c ReceiveSessionOpenResponse() 735 | Unexpected server response (-5).
02:20:53 *CRT* | supervisor[608]/security T=main | app.c app_SigChildHandler() 1635 | The process 'MyClient' in app 'MyClient' has faulted and will be ignored in accordance with its fault policy.
```

What I’m doing wrong?

Note: I’m not using this function from the client side but myclient\_interface.h file contains:

`typedef enum
{
    MYCLIENT_CLIENT_A,
    MYCLIENT_CLIENT_B
}
myclient_client_type_t;`

```auto
void myclient_SetClientId
(
    myclient_client_type_t client
        ///< [IN]
);
```

Thanks

Daniel

---

<div class="post-metadata">

**Author:** ![zblazek](https://avatars.discourse-cdn.com/v4/letter/z/a9adbd/32.png) [@zblazek](https://forum.legato.io/u/zblazek)\
**Post date:** [April 29, 2015, 5:06pm UTC](https://forum.legato.io/t/ipc-client-server-reference/123/5 "2015-04-29T17:06:12Z")

</div>

Hi,

From the runtime error, there is a mismatch between the client and server. This error usually happens if you makes changes to the .api file but don’t update both the client and server. Did you rebuild and re-install both the client and the server?

Also, related to your original question, you said that the server needs to know which client is connected. What does it do with this information? Is it just for local data on the server, or for something else?

– Zeljko

---

<div class="post-metadata">

**Author:** ![danielgomezdn](https://avatars.discourse-cdn.com/v4/letter/d/f9ae1b/32.png) [@danielgomezdn](https://forum.legato.io/u/danielgomezdn)\
**Post date:** [April 30, 2015, 8:21am UTC](https://forum.legato.io/t/ipc-client-server-reference/123/6 "2015-04-30T08:21:56Z")

</div>

Hi zblazek,

I don’t understand why could it be a rebuild error… because I have in my workspace the server component and the app which is linked and the same for the client component and app. So when I compile, I’m compiling all applications and components.

Regarding to the original question, this is needed because server will share different information depending on which client is connected or asking for data. Does it make sense?

Daniel

---

<div class="post-metadata">

**Author:** ![kmurphy](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/kmurphy/32/65_2.png) [@kmurphy](https://forum.legato.io/u/kmurphy)\
**Post date:** [September 15, 2016, 6:42pm UTC](https://forum.legato.io/t/ipc-client-server-reference/123/7 "2016-09-15T18:42:35Z")

</div>

Another option then is to export 2 instances of your API.

So, in your server you could have something along the lines of:

```
provides:
{
    api:
    {
        instA = myApi.api
        instB = myApi.api
    }
}

```

Then in your server.c

```
int instA_Server(void)
{
    return ClientAResponse;
}

int instB_Server(void)
{
    return ClientBResponse;
}

```

And you can bind the clients to their respective service instances.
