summaryrefslogtreecommitdiff
path: root/docs/tutorials/019/client.cpp
blob: e557256d37dbe3f3ee540fb5861f50b8c5d5c6e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

// $Id$

// Again, the common stuff
#include "shmem.h"

int main (int, char *[])
{
        /*
          Attach ourselves to the shared memory segment.
         */
    ACE_Shared_Memory_SV shm_client (SHM_KEY, SHMSZ);

        /*
          Get our reference to the segment...
        */
    char *shm = (char *) shm_client.malloc ();

        /*
          If the segment identified by SHM_KEY didn't exist then we'll 
          get back a 0 from malloc().  You should do this check even
          if you include the CREATE flag 'cause you never know when it 
          might fail.
         */
    if( ! shm )
    {
        ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) Could not get the segment!\n"),100);
    }

        /*
          Does this match what your server said?
        */
    ACE_DEBUG ((LM_INFO, "(%P|%t) Shared Memory is at 0x%x\n",
                shm ));

        /*
          Show the shared data to the user and convert it all to
          uppper-case along the way.
        */
    for (char *s = shm; *s != '\0'; s++)
    {
        putchar (*s);
        *s = toupper(*s);
    }
    
    putchar ('\n');

        /*
          Flag the server that we're done.
        */
    *shm = '*';

        /*
          Here, we use close() instead of remove().  Remember, that
          will just remove our attachment to the segment.  Look
          closely at the 'nattch' column of the ipcs output & you'll
          see that this decrements it by one.
        */
    shm_client.close();

    return 0;
}