c++ - 基于共享内存的聊天应用程序的问题

标签 c++ linux memory shared-memory shared

我正在尝试构建一个具有两个使用共享内存交换消息的进程的应用程序... 正如您将看到的,我正在做的是请求共享内存,然后在其中放入一个结构。 该结构由字符串、 bool 标志和枚举值组成。 字符串应该保存消息,标志应该告诉对方是否已经看到该消息(因为如果内存中有未读消息,则不允许任何人添加消息) 我遇到了几个问题 1-我够不到绳子上的绳子...... 2-当我用 int 替换字符串时,我在尝试访问内存时在客户端遇到问题 这是代码...

服务器端:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <iostream>
#include <string>
#include <sys/wait.h>
using namespace std;
enum e {Server,Client};

struct chat             // struct that will reside in the memory
{


    bool ifread;        //boolian variable to determin if the message has been raed or not
    e who;
    char message[50];
    int msgl;


};

int main(void)
{
string temp;
int shmid;
//key_t key=ftok(".",'a');
key_t key=433;

if ((shmid = shmget(key, sizeof(chat), IPC_CREAT | 0666)) < 0)
{
cout<<"shmget"<<endl;
return(1);
}

chat *str ;
str = (chat *)shmat(shmid, NULL, 0);

pid_t pid;
pid=fork();
str->ifread==true;

str->who=Server;
if(pid==0)
{

    while(temp!="bye")
    {
        if(str->ifread==false && str->who==Client)
        {
            //cout<<"Client said : ";

            for(int i=0;i<str->msgl;i++)
            cout<<str->message[i];
    str->ifread==true;

        }

    }

}

else if (pid>0)
{
    while(temp!="bye")
    {
    getline(cin,temp);
    str->msgl=temp.length();
    if(str->ifread)
     {
        str->who=Server;
        for(int i=0;i<str->msgl;i++)
         {
            str->message[i]=temp.at(i);
         }

    str->who=Server;
    str->ifread==false;
      }

    else if (!str->ifread && str->who==Client)
       {
    sleep(1);
    waitpid(pid,NULL,0);
        }
}

}

shmctl (shmid, IPC_RMID, NULL);

}

客户端:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <iostream>
#include <string>
#include <sys/wait.h>
using namespace std;
enum e {Server,Client};

struct chat             // struct that will reside in the memory
{


    bool ifread;        //boolian variable to determin if the message has been raed or not
    e who;
    char message[50];
    int msgl;


};

int main(void)
{
string temp;
int shmid;
//key_t key=ftok(".",'a');
key_t key=433;

if ((shmid = s`hmget(key, sizeof(chat),  0666)) < 0)
{
cout<<"shmget"<<endl;
return(1);
}

chat *str ;
str = (chat *)shmat(shmid, NULL, 0);

pid_t pid;
pid=fork();


if(pid==0)
{

    while(temp!="bye")
    {
        if(str->ifread==false && str->who==Server)
        {
            //cout<<"Server said : ";

            for(int i=0;i<str->msgl;i++)
            cout<<str->message[i];
    str->ifread==true;

        }

    }

}

else if (pid>0)
{
    while(temp!="bye")
    {
    getline(cin,temp);
    str->msgl=temp.length();
    if(str->ifread)
     {
        str->who=Client;
        for(int i=0;i<str->msgl;i++)
         {
            str->message[i]=temp.at(i);
         }
    str->ifread=false;

      }

    else if (!str->ifread && str->who==Server)
       {
    sleep(1);
    //waitpid(pid,NULL,0);
        }
}

}

shmctl (shmid, IPC_RMID, NULL);

}

提前致谢,抱歉英语不好......

编辑: 谢谢aix,但还有另一个问题,即客户端首先无法从共享内存中获取任何数据,即使我使用 int x 在它们之间交换数字 即使服务器已经放置了另一个值,客户端首先仍然给出 0 ,过了一会儿,它在调用 shmget() 时开始给出错误;

编辑(2):我做了一些更改,但它仍然不起作用......

编辑(3):问题已解决,谢谢大家,结果是对标志的排序错误 再次感谢...

最佳答案

主要问题是 std::string 在幕后使用堆分配。

这意味着

struct chat
{
   string  letter;

不会将整个字符串放入结构中。它放置字符串对象,但不一定是与字符串关联的字符数据

解决此问题的一种方法是将 std::string 替换为 char[N]

一般来说,在将共享内存与涉及指针或引用的任何内容一起使用时应格外小心。

此外,为了避免构造/破坏的复杂性,最好确保 chatPOD .

关于c++ - 基于共享内存的聊天应用程序的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10429559/

相关文章:

c++ - 为什么插入 set<vector<string>> 这么慢?

c++ - 为什么将子指针向上转换为基类有时会更改指针值?

linux - 使用 grep 查找两个 's' 由空格分隔的单词

c++ - 不能包含/usr/include/linux 文件

c - c中的内存泄漏错误

c++ - main 在 pthread 之后不继续

c# - 将 esp8266 代码(部分)转换为 c/c++ 或 c#

linux - 获取结果状态代码 "systemd-run <command>"的简单方法?

c - C的智能指针/安全内存管理?

linux - 执行c6accel样本后出现Segmentation fault错误