problems at a half-duplix shared-memory based chat app

Hi there ...
I'm trying to build two process that uses Shared memory to exchange messages...
what I'm doing as you will see , is requesting for a shared memory and then putting a struct in it .
the struct consist of a string , Bool flag and an enum values ..
the string should hold the message , the flag should tell whether this message has been seen or not by the other side (cause no one is allowed to add a message if there is a unread message in the memory )
I'm suffering from several problems
1- i cant reach the string at the string....
2- when i replaced the string with an int , i face a problem at the client side when trying to get to the memory
and this is the code ...




Server's side
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
64
65
66
67
68
69
70
71
#include <iostream>
#include <sys/types.h>
#include <sys/ipc.h>
#include <string.h>
#include <sys/shm.h>
using namespace std;
enum e {Server,Client}; //enumeration that will be used to determine who wrote the message
struct chat             // struct that will reside in the memory
{
   string  letter;      //the message it self

    bool ifread;        //boolian variable to determine if the message has been raed or not
    e who;
    int x;
    };


int main(void)
{
   string name;

    cout << "Welcome to the chat room application....please enter your Alias" << endl;
    cin>>name; //taking the name that will be used in the chat

   int shmid; //the id of the shard memory area
   key_t key=ftok(".",'g');//generating a key for the shared memory
   shmid=shmget(key,sizeof(chat),IPC_CREAT | 0666); //creating the shared memory
    if (shmid<0)
    {cout<<"an Error has happend please try again"<<endl;
    return(1);
    }
    chat *str ;         // pointer to the struct
    str=(chat*)shmat(shmid,NULL,0);// attaching the shared memory area to the struct's pointer...
    int temp;           //temp area to store the read input
    /*while(temp!=5)    //keep waiting for the right input to quit
    {
        cout<<"please enter the num"<<endl;
        cin>>temp;

        if(str->ifread)
        {str->ifread=false;
        str->x=temp;
        str->who=Server;
        }
        else if(!str->ifread && str->who==Client)
        {
            cout<<str->x<<endl;

            }
        else{sleep(1);}

  }
*/
while(str->x!=5)
{
    str->ifread=false;
    cout<<"Enter #"<<endl;
    cin>>str->x;

while(str->ifread==false)
{
cout<<"waiting"<<endl;
sleep(1);
}

cout<<str->x<<endl;
}

shmctl (shmid, IPC_RMID, NULL);

}







Client's Side

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
64
65
66
67
68
69
70
#include <iostream>
#include <sys/types.h>
#include <sys/ipc.h>
#include <string.h>
#include <sys/shm.h>
using namespace std;
enum e {Server,Client}; //enumeration that will be used to determine who wrote the message
struct chat             // struct that will reside in the memory
{
   string  letter;      //the message it self

    bool ifread;        //boolian variable to determin if the message has been raed or not
    e who;
    int x;
    };


int main(void)
{
   string name;

    cout << "Welcome to the chat room application....please enter your Aliase" << endl;
    cin>>name; //taking the name that will be used in the chat

   int shmid; //the id of the shard memory area
   key_t key=ftok(".",'g');//generating a key for the shared memory
  
   shmid=shmget(key,sizeof(chat),0666); //requesting the shared memory
    if (shmid<0)
    {cout<<"an Error has happened please try again"<<endl;
    return(1);
    }
    chat *str ;         // pointer to the struct
    str=(chat*)shmat(shmid,NULL,0);// attaching the shared memory area to the struct's pointer...
    int temp;           //temp area to store the read input
   /* while(temp!=5)    //keep waiting for the right input to quit
    {
        cout<<"please enter the num"<<endl;
        cin>>temp;
        
        if(str->ifread)
        {str->ifread=false;
        str->x=temp;
        str->who=Client;
        }
        else if(!str->ifread && str->who==Server)
        {
            cout<<str->x;

            }
        else{sleep(1);}

    }

*/
while(str->x!=5)
{
str->ifread=false;
cout<<"please #"<<endl;
cin>>str->x;
while(str->ifread==false)
{sleep(1);
cout<<"waiting"<<endl;
}
cout<<str->x<<endl;


}
shmctl (shmid, IPC_RMID, NULL);
}





Thanks in advance , and sorry for the bad English .....
Last edited on
You can't do that. If you're going to use shared memory, you can only put POD (plain old data) types in it; you know, int, long, double, char ... and arrays of those types.

Why? Because objects (like string) allocate memory and use pointers to that memory. A pointer is only meaningful within a process.
Last edited on
Topic archived. No new replies allowed.