客户不读私人FIFO

时间:2016-10-05 00:38:52

标签: c private public fifo

我一直在研究客户端必须从服务器读取的代码。客户端将向服务器写入1个名称和3个值,服务器必须通过私有fifo返回该值。问题是,客户端没有从服务器读取私有fifo。我期望得到服务器创建的值。这是客户端和服务器的代码:

*编辑:根据评论的建议,这是我对客户端和服务器的输入...在服务器端,我键入" 1"客户端因此它接收1个客户端。客户端:我键入任何名称(主页可以说),然后对于数字100,150和200,服务器端接收所有这些,然后服务器应该发送"内存地址"每个通过私人fifo。客户端没有得到任何内存地址,只是得到随机的负数

客户端:

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

struct values {
  int intVal0[1];
  int intVal1[1];
  int intVal2[1];
  int adss[7];
  int freeSp[1];
  };
struct strings {
  char strbuff[100];
  char privateFIFO[14];
  };

main (void)
{
  struct values val;
  struct strings strs;
  int fda;  // to write to server
  int fdb;  // to read response from server
  int clientID;
  char temp[14];
  memset(val.freeSp,0,1);
  memset(strs.strbuff,0,100); 
  memset(val.intVal0,0,1); 
  memset(val.intVal1,0,1);
  memset(val.intVal2,0,1);
  memset(val.adss,0,7); 
  memset(strs.privateFIFO,0,14); 

  clientID = getpid();
  sprintf(strs.privateFIFO, "FIFO_%d", clientID);
  printf("\nFIFO name is %s\n", strs.privateFIFO);

  if((fda=open("FIFO1", O_WRONLY))<0)
     printf("cant open fifo to write");

  printf("You have 3 segments inwhich to put your program.");
  printf("\nYou have a total of 2000mu.");
  printf("\nClient: Please enter the name of the program: ");
  scanf("%s", &strs.strbuff);
  strcpy(strs.strbuff, strs.strbuff);
  printf("\nClient: Please enter a how the amount of memory for segment 0: ");
  scanf("%d", &val.intVal0);
  while(val.intVal0[0] <= 0)//while loop to make sure segment 0 gets a value greater than 0
    {
       printf("Client: Please reenter an amount of memory that is 0 or higher for segment 0: ");
       scanf("%d", &val.intVal0);
    }   
  printf("Client: Please enter a how the amount of memory for segment 1: ");
  scanf("%d", &val.intVal1);
  while(val.intVal1[0] <= 0)//while loop to make sure segment 1 gets a value greater than 0
    {
       printf("Client: Please reenter an amount of memory that is 0 or higher for segment 1: ");
       scanf("%d", &val.intVal1);
    }
  printf("Client: Please enter a how the amount of memory for segment 2: ");
  scanf("%d", &val.intVal2);
  while(val.intVal2[0] <= 0)//while loop to make sure segment 2 gets a value greater than 0
    {
       printf("Client: Please reenter an amount of memory that is 0 or higher for segment 2: ");
       scanf("%d", &val.intVal2);
    }

  write(fda, &strs, sizeof(struct strings));
  write(fda, &val, sizeof(struct values));

  printf("\nClient: Got the values sent, now waiting for response\n ");
  if((fdb=open(strs.privateFIFO, O_RDONLY))<0)
     printf("\ncant open fifo to read");

  read(fdb, &val, sizeof(struct values));
  if (val.freeSp[0] >= 0 && val.freeSp[0] >= val.intVal0[0] && val.freeSp[0] >= val.intVal1[0] && 
    val.freeSp[0] >= val.intVal2[0])// if free space is greater than 0, prints what is below
    {  
          printf("\nClient: address of segment 0: %d - %d", val.adss[0], val.adss[1]);
          printf("\nClient: address of segment 1: %d - %d", val.adss[2], val.adss[3]);
          printf("\nClient: address of segment 2: %d - %d", val.adss[4], val.adss[5]);
          printf("\nClient: free space available: %d\n", val.freeSp[0]);
          printf ("\nall done!\n");
    }
   else// if free space is less than or equal to 0, prints below
    {
         printf("\nClient: No memory allocated because segment sizes are too big");
         printf ("\nall done!\n");
    }
  close(fda);
  close(fdb);



}

客户输出:

Client: Please enter the name of the program: home

Client: Please enter a how the amount of memory for segment 0: 100
Client: Please enter a how the amount of memory for segment 1: 150
Client: Please enter a how the amount of memory for segment 2: 200

Client: Got the values sent, now waiting for response

cant open fifo to read
Client: address of segment 0: 0 - 134217728
Client: address of segment 1: -17028108 - -17701883
Client: address of segment 2: -18270303 - 134549970
Client: free space available: 134511872

服务器:

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

struct values {
  int intVal0[1];
  int intVal1[1];
  int intVal2[1];
  int adss[7];
  int freeSp[1];
  };

struct strings {
  char strbuff[100];
  char privateFIFO[14];
  };

main (void)
{
  struct values val;
  struct strings strs;
  int fda;  // to read from client
  int fdb;  // to write to client
  int finish;   // lets me know that client is done
  int i;    // because C needs this defined as int
  int n =0;  //counter for amount of clients 
  int m = 0;  //counter for addresses on server
  int mem = 2000; // set allocated memory
  int inputMemTot;// total amount of segmented memory from client
  int numClient; // input of number of clients
  int upkeep = 0; // keeps the last place of memory address 
  int adssOnSvr[100];  // array that keeps track of all client addresses, only on server
  char prgName[10][14];// array that will store program names

  memset(strs.strbuff,0,100); 
  memset(val.intVal0,0,1); 
  memset(val.intVal1,0,1);
  memset(val.intVal2,0,1);
  memset(val.freeSp,0,1);
  memset(val.adss,0,7); 
  memset(strs.privateFIFO,0,14);
  printf("\nServer: Please enter the name of clients: ");
  scanf("%d", &numClient);

  for( i = 0; i < numClient; i++)
  {
     if ((mkfifo("FIFO1",0666)<0 && errno != EEXIST))
     {
         perror("cant create FIFO1");
         exit(-1);
     }

     if((fda=open("FIFO1", O_RDONLY))<0)
         printf("cant open fifo to write");


     read(fda, &strs, sizeof(struct strings));
     read(fda, &val, sizeof(struct values));    //read the character
     memset(val.adss,0,7); 
     strcpy(strs.strbuff, strs.strbuff);
     strcpy(strs.privateFIFO, strs.privateFIFO);
     //strcpy(prgName[i][14], strs.strbuff);
     inputMemTot = val.intVal0[0] + val.intVal1[0] + val.intVal2[0];// adds up all segment sizes 
     val.freeSp[0] = mem - inputMemTot;// puts the value of free space into here
     printf("Server: just got the program name: %s", strs.strbuff);
     printf("\nServer: just got the size of segment0: %d", val.intVal0[0]);
     printf("\nServer: just got the size of segment1: %d", val.intVal1[0]);
     printf("\nServer: just got the size of segment2: %d\n", val.intVal2[0]);

     if(upkeep = 0)
     {
         val.adss[0] = 0;  // set address location for starting value of segment 0
         adssOnSvr[0] = 0;
     }
     else 
         val.adss[0] = upkeep;
     val.adss[1] = val.intVal0[0] - 1; // set address location for ending value of segment 0
     adssOnSvr[1 + m] = val.adss[1];
     val.adss[2] = val.intVal0[0]; // set address location for starting value of segment 1
     adssOnSvr[2 + m] = val.adss[2];
     val.adss[3] =  val.adss[2] + val.intVal1[0] - 1; // set address location for ending value of segment 1
     adssOnSvr[3 + m] = val.adss[3];
     val.adss[4] = val.adss[3] + 1; // set address location for starting value of segment 2
     adssOnSvr[4 + m] = val.adss[4];
     val.adss[5] =  val.adss[4] + val.intVal2[0];// set address location for ending value of segment 2
     adssOnSvr[5 + m] = val.adss[5];
     val.adss[6] = 0;
     upkeep = val.adss[5];
     m = m + 5;

     if ((mkfifo(strs.privateFIFO,0666)<0 && errno != EEXIST))
      {
         perror("cant create client fifo");
         exit(-1);
      }

     if((fdb=open(strs.privateFIFO, O_WRONLY))<0)
         printf("cant open %s to read", strs.privateFIFO);

     write(fdb, &val, sizeof(struct values)); 

     close(fda);
     close(fdb);
     unlink("FIFO1");
     unlink(strs.privateFIFO);
     /*if(i == numClient)
         break;*/
    }
  if (val.freeSp[0] >= 0)// if free space is greater than 0, prints what is below
    {  
      for(n = 0; n < numClient; n++)
      {
         m = 0;
         //printf("\nServer: Program %s", prgName[n][14]);
         if(n = 0)
             printf("\nServer: address of segment 0: %d - %d", adssOnSvr[0], adssOnSvr[1 + m]);
         else
             printf("\nServer: address of segment 0: %d - %d", adssOnSvr[0 + m], adssOnSvr[1 + m]);

         printf("\nServer: address of segment 1: %d - %d", adssOnSvr[2 + m], adssOnSvr[3 + m]);
         printf("\nServer: address of segment 2: %d - %d", adssOnSvr[4 + m], adssOnSvr[5 + m]);
         m = m + 5;
      }
      printf("\nServer: free space available: %d\n", val.freeSp[0]);

    }
  if(val.freeSp[0] <= 0)// if free space is less than or equal to 0, prints below
    {   
       printf("\nServer: Segment sizes are to big. No memory allocated.\n");

    }

   printf("\nServer: This says I am ready to close \n");
  /*close(fda);
  close(fdb);
  /*unlink("FIFO1");*/
}

服务器输出:

Server: Please enter the name of clients: 1
Server: just got the program name: home
Server: just got the size of segment0: 100
Server: just got the size of segment1: 150
Server: just got the size of segment2: 200

这是服务器在此之后什么都不做的地方,我必须使用control + c

手动退出

0 个答案:

没有答案