C++ 程序1接受用户的输入,然后程序2打印到屏幕上

C++ 程序1接受用户的输入,然后程序2打印到屏幕上,c++,C++,我有一个服务器和一个客户端程序。服务器程序提示用户输入文本,然后将用户输入发送到客户端。客户端将用户文本打印到屏幕上。到目前为止,服务器程序会提示用户输入文本,当我运行客户端程序时,它不会显示任何内容。任何使计划有效的建议。下面是两个程序 // server.cpp // g++ -o server server.cpp -lpthread -lrt #include <sys/types.h> #include <sys/ipc.h> #include <sys/

我有一个服务器和一个客户端程序。服务器程序提示用户输入文本,然后将用户输入发送到客户端。客户端将用户文本打印到屏幕上。到目前为止,服务器程序会提示用户输入文本,当我运行客户端程序时,它不会显示任何内容。任何使计划有效的建议。下面是两个程序

// server.cpp
// g++ -o server server.cpp -lpthread -lrt
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string>
#include <ctype.h>
#include <iostream>

using namespace std;

string UserInput(string);

#define SHMSZ 27
char SEM_NAME[]= "vik";


int main(void)
{
  char ch;
  int shmid;
  key_t key;
  char *shm,*s;
  sem_t *mutex;
  string input;

  //name the shared memory segment
  key = 1000;

  //create & initialize semaphore
  mutex = sem_open(SEM_NAME,O_CREAT,0644,1);
  if(mutex == SEM_FAILED)
    {
      perror("unable to create semaphore");
      sem_unlink(SEM_NAME);
      exit(-1);
    }

  //create the shared memory segment with this key
  shmid = shmget(key,SHMSZ,IPC_CREAT|0666);
  if(shmid<0)
    {
      perror("failure in shmget");
      exit(-1);
    }

  //attach this segment to virtual memory
  shm = (char*) shmat(shmid,NULL,0);

  //start writing into memory
  s = shm;

  // Enter user input
  //cout << "Enter your input: ";
  //cin >> input;

  // Display user input
  //cout << "You entered: "<< input << endl;

  //return 0;

  while(1)
    {
      cout << "Enter your input: ";
      cin >> input;
      sem_wait(mutex);
      //*s++ = count;
      sem_post(mutex);
      //return 0;
    }


  //the below loop could be replaced by binary semaphore
  while(*shm != '*')
    {
      sleep(1);
    }
  sem_close(mutex);
  sem_unlink(SEM_NAME);
  shmctl(shmid, IPC_RMID, 0);
  _exit(0);

}
//server.cpp
//g++-oserver.cpp-lpthread-lrt
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
字符串用户输入(字符串);
#定义SHMSZ 27
字符SEM_NAME[]=“vik”;
内部主(空)
{
char ch;
int shmid;
钥匙(t)钥匙;;
字符*shm,*s;
sem_t*互斥体;
字符串输入;
//命名共享内存段
密钥=1000;
//创建并初始化信号量
互斥量=sem_open(sem_NAME,O_CREAT,0644,1);
if(互斥==SEM_失败)
{
perror(“无法创建信号量”);
sem_取消链接(sem_名称);
出口(-1);
}
//使用此键创建共享内存段
shmid=shmget(键,SHMSZ,IPC|U创建| 0666);
如果(shmid输入;
//显示用户输入

//调试器说什么?我没有收到错误消息。当我运行server.cpp时,我输入字符串输入,程序处于循环中。这没关系。我运行client.cpp,但服务器的字符串输入没有发送到客户端?或者client.cpp没有捕获字符串输入。我的程序终于工作了。问题是内存指针没有引用用户输入。服务器代码未将字符串输入发送到内存位置。因此客户端无法输出到屏幕。我如何将此页面标记为已解决?如果有人感兴趣,我将显示我的代码!@ep7network:您可以在本文下方的回答框中回答自己的问题,类似于您发布问题的方式。
// client.cpp
// g++ -o client client.cpp -lpthread -lrt
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <ctype.h>

using namespace std;

string UserInput(string);

#define SHMSZ 27
char SEM_NAME[]= "vik";



int main(void)
{
  char ch;
  int shmid;
  key_t key;
  char *shm,*s;
  sem_t *mutex;
  string input;

  //name the shared memory segment
  key = 1000;

  //create & initialize existing semaphore
  mutex = sem_open(SEM_NAME,0,0644,0);
  if(mutex == SEM_FAILED)
    {
      perror("reader:unable to execute semaphore");
      sem_close(mutex);
      exit(-1);
    }

  //create the shared memory segment with this key
  shmid = shmget(key,SHMSZ,0666);
  if(shmid<0)
    {
      perror("reader:failure in shmget");
      exit(-1);
    }

  //attach this segment to virtual memory
  shm = (char*) shmat(shmid,NULL,0);

  //start reading
  s = shm;
  while(1)
    {

      cout << "You entered: " << input << endl;
      //cin >> input;
      sem_wait(mutex);
      putchar(*s);
      sem_post(mutex);
      return 0;
    }

  //once done signal exiting of reader:This can be replaced by another semaphore
  *shm = '*';
  sem_close(mutex);
  shmctl(shmid, IPC_RMID, 0);
  exit(0);
}