【问题标题】:Sending a process id from client to server将进程 ID 从客户端发送到服务器
【发布时间】:2020-02-28 06:00:40
【问题描述】:

我的任务是编写一个客户端程序,它将一个具有 privateFIFO 名称(FIFO_XXXX,其中 XXXX 是我们从 getpid() 函数获得的 pid)的结构写入服务器。然后,让服务器读取 privateFIFO 名称并将消息写回客户端。即,阅读消息并在客户端打印。我在将 FIFO_XXXX 发送到服务器程序并将消息从服​​务器写回客户端时遇到问题。

client code:

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>


int main (void)
{

  struct values
  {
    char privateFIFO[14];
    int intbuff;
  }input;

  int fda;  // common FIFO to read to write to server
  int fdb;      // Private FIFO to read from server
  int clientID;
  int retbuff;
  char temp[14];

  clientID = getpid();
  strcpy(input.privateFIFO, "FIFO_");
  sprintf(temp, "%d", clientID);
  strcat(input.privateFIFO, temp);
  printf("\nFIFO name is %s", input.privateFIFO);

  // Open common FIFO to write to server
  if((fda=open("FIFO_to_server", O_WRONLY))<0)
     printf("cant open fifo to write");

  write(fda, &input, sizeof(input));    // write the struct to the server
  close(fda);


  // Open private FIFO to read
  if((fdb=open(input.privateFIFO, O_RDONLY))<0)
     read(fdb, &retbuff, sizeof(retbuff));

  printf("\nAll done!\n");

  close(fdb);

}

server code:

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>


struct values
  {
    char privateFIFO[14];
    int intbuff;
  }input;

int main (void)
{

  int fda;  //common FIFO to read from client
  int fdb;  //private FIFO to write to client
  int retbuff;
  int output;

// create the common FIFO 
  if ((mkfifo("FIFO_to_server",0666)<0 && errno != EEXIST))
        {
        perror("cant create FIFO_to_server");
        exit(-1);
 }

// open the common FIFO
  if((fda=open("FIFO_to_server", O_RDONLY))<0)
     printf("cant open fifo to write");


  output = read(fda, &input, sizeof(input));

// create the private FIFO
  if ((mkfifo(input.privateFIFO, 0666)<0 && errno != EEXIST))
  {
    perror("cant create privateFIFO_to_server");
    exit(-1);
  }


  printf("Private FIFO received from the client and sent back from server is: %d", output);

  //open private FIFO to write to client
  if((fdb=open(input.privateFIFO, O_WRONLY))<0)
    printf("cant open fifo to read");
  write(fdb, &retbuff, sizeof(retbuff));


  close(fda);
  unlink("FIFO_to_server");
  close(fdb);
  unlink(input.privateFIFO);


}


【问题讨论】:

    标签: c server client


    【解决方案1】:

    使用 IPC 消息队列。它更好更简单。确实,这种机制管理读写操作、并发访问之间的同步……:

    编写过程:

    #include <stdio.h> 
    #include <sys/ipc.h> 
    #include <sys/msg.h> 
    
    // structure for message queue 
    struct mesg_buffer { 
        long mesg_type; 
        char mesg_text[100]; 
    } message; 
    
    int main() 
    { 
        key_t QueueKey; 
        int msgid; 
    
        // ftok : generate a unique OPC key 
        QueueKey = ftok("FIFO_XXXX", 65); 
    
        // msgget creates a message queue and returns identifier 
        msgid = msgget(QueueKey, 0666 | IPC_CREAT); 
    
        // Sending Data
        message.mesg_type = 1; 
        printf("Write Data : "); 
        gets(message.mesg_text); 
    
        // msgsnd : Send message to the queue
        msgsnd(msgid, &message, sizeof(message), 0); 
    
        // display the message 
        printf("Data send is : %s \n", message.mesg_text); 
    
        return 0; 
    }   
    

    读者流程:

    #include <stdio.h> 
    #include <sys/ipc.h> 
    #include <sys/msg.h> 
    
    // structure for message queue 
    struct mesg_buffer { 
        long mesg_type; 
        char mesg_text[100]; 
    } message; 
    
    int main() 
    { 
        key_t QueueKey; 
        int msgid; 
    
        // ftok to generate unique key 
        QueueKey = ftok("FIFO_XXXX", 65); 
    
        // msgget creates a message queue and returns identifier 
        msgid = msgget(QueueKey, 0666 | IPC_CREAT); 
    
        // msgrcv to receive message 
        msgrcv(msgid, &message, sizeof(message), 1, 0); 
    
        // display the message 
        printf("Data Received is : %s \n",  message.mesg_text); 
    
        // IPC_RMID : destroy the message queue 
        msgctl(msgid, IPC_RMID, NULL); 
    
        return 0; 
    } 
    

    备注:
    请注意,一个进程既可以是写入者,也可以是读取者。为此,必须为每个进程创建两个消息队列。第一个允许例如接收,第二个允许写入。如果你真的想为每个进程分离读/写空间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-15
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      • 2011-08-15
      • 2013-04-11
      • 2021-12-07
      相关资源
      最近更新 更多