Program1接受来自用户的输入,然后Program2打印到屏幕

时间:2016-02-09 04:26:50

标签: c++

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

// 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);

}

-

// 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);
}

0 个答案:

没有答案