多个客户端使用c语言的fifos连接到服务器

时间:2016-10-02 20:42:55

标签: struct while-loop server client fifo

我在这里有一个小问题,我需要帮助。我有一台服务器必须同时接受多个客户端,并且必须在将信息发送回客户端之前处理一些信息。它必须通过私有fifo(必须在服务器中为客户端创建)向每个客户端发送信息。我知道它必须用while循环做一些事情,服务器必须在没有连接的客户端时结束。

到目前为止,我已经有1个客户端连接到服务器。当没有其他任何东西可以处理时,服务器就会完成。我知道这可以通过套接字完成,但他们需要特别的fifos和while循环。感谢您的帮助,这是我需要修改的代码:

客户:

#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>


// creating the struct for the program
struct probsiz{
   int seg1 [1]; //segment 1 size
   int seg2 [1]; //segment 2 size
   int seg3 [1]; //segment 3 size
   int segsum [7]; //sum for all the segments
   int leftov [1]; //memory leftovers

} ;

struct probseg {     

   char segment[64]; //segments
};


main (void)
{
  struct probsiz numbs; //defining the structures
  struct probseg names; 

  int fda;  // to write to character server
  int fdb;  // to read response from character server


  memset(numbs.seg1, 0, 1); //setting up memory for the first segment
  memset(numbs.seg2, 0, 1); //setting up memory for the second segment
  memset(numbs.seg3, 0, 1); //setting up memory for the third segment
  memset(numbs.segsum, 0, 7);// setting up memory for the sum of all segments
  memset(numbs.leftov, 0, 1); //setting up memory for the first segment
  memset(names.segment, 0, 64);// setting up memory for the segments


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

  if((fdb=open("FIFO2", O_RDONLY))<0)
     printf("cant open fifo to read");

  //reading the requested memory and segment name from the user
  printf("Client: Please enter requested memory 1: ");
  scanf("%d", &numbs.seg1[0]);
  while (numbs.seg1 <=0){
      printf("Client: please enter a valid request: ");
      scanf("%d", &numbs.seg1[0]);
  }
  printf("Client: Please enter segment 1 name: ");
  scanf("%s", &names.segment[0]);


  printf("Client: Please enter requested memory 2: ");
  scanf("%d", &numbs.seg2[0]);
  while (numbs.seg1 <=0){
      printf("Client: please enter a valid request: ");
      scanf("%d", &numbs.seg2[0]);
  }
  printf("Client: Please enter segment 2 name: ");
  scanf("%s", &names.segment[1]);


  printf("Client: Please enter requested memory 3: ");
  scanf("%d", &numbs.seg3[0]);
  while (numbs.seg3 <=0){
      printf("Client: please enter a valid request: ");
      scanf("%d", &numbs.seg3[0]);
  }
  printf("Client: Please enter segment 3 name: ");
  scanf("%s", &names.segment[2]);

  //send and write into the fifos

  printf("\nClient: Got the sizes sent now waiting for server's response\n");

 write(fda, &numbs, sizeof(struct probsiz));
 write(fda, &names, sizeof(struct probseg));


  //read from the fifos
 read(fdb, &numbs, sizeof(struct probsiz));

 if (numbs.leftov[0] >=0) {

     printf("\nClient: address for segment 1 is: %d - %d", numbs.segsum[0], numbs.segsum[1]);
     printf("\nClient: address for segment 2 is: %d - %d", numbs.segsum[2], numbs.segsum[3]);
     printf("\nClient: address for segment 3 is: %d - %d", numbs.segsum[4], numbs.segsum[5]);
     printf("\nClient: leftover memory is: %d", numbs.leftov[0]);
     printf("\nall done!");
 }
 else
 {
     printf("\nClient: segment size is over the capacity, please try again");
     printf("\nall done!\n");

 }

  //this closes the fifos
  close(fda);
  close(fdb);


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

}

服务器:

// creating the struct for the program
struct probsiz{
   int seg1 [1]; //segment 1 size
   int seg2 [1]; //segment 2 size
   int seg3 [1]; //segment 3 size
   int segsum [7]; //sum for all the segments
   int leftov [1]; //memory leftovers

} ;

struct probseg {     

   char segment[64]; //for the segments
};

main (void)
{
  struct probsiz numbs; //  structure definitions
  struct probseg names;


  int fda;  // to read from client char
  int fdb;  // to write to client char
  int finish;   // lets me know that client is done
  int i;    // because C needs this defined as int
  int m=2000; //total memory units
  int mtot;  //memory total from the sum of all the segments

  //setting up the memory 

  memset(numbs.seg1, 0, 1); //setting up memory for the first segment
  memset(numbs.seg2, 0, 1); //setting up memory for the second segment
  memset(numbs.seg3, 0, 1); //setting up memory for the third segment
  memset(numbs.segsum, 0, 7);// setting up memory for the sum of all segments
  memset(numbs.leftov, 0, 1); //setting up memory for the first segment
  memset(names.segment, 0, 64);// setting up memory for the segments


  /* Create the fifos and open them  */
  if ((mkfifo("FIFO1",0666)<0 && errno != EEXIST))
    {
    perror("cant create FIFO1");
    exit(-1);
    }
  if ((mkfifo("FIFO2",0666)<0 && errno != EEXIST))
    {
    perror("cant create FIFO2");
    exit(-1);
    }
  if((fda=open("FIFO1", O_RDONLY))<0)
     printf("cant open fifo to write");
  if((fdb=open("FIFO2", O_WRONLY))<0)
     printf("cant open fifo to read");



  read(fda, &numbs, sizeof(struct probsiz)); //read the sizes
  finish=read(fda, &names, sizeof(struct probseg)); //read the segments

  //printing out the characters on the server side to validate the data in

  strcpy(names.segment, names.segment);
  mtot=numbs.seg1[0]+numbs.seg2[0]+numbs.seg3[0];
  numbs.leftov[0]=m-mtot;

  //printf("\nServer: just got segment 1: %s", names.segment[0]);
  printf("\nServer: just got segment 1 size: %d", numbs.seg1[0]);

  //printf("\nServer: just got segment 2: %s", names.segment[1]);
  printf("\nServer: just got segment 2 size: %d", numbs.seg2[0]);

  //printf("\nServer: just got segment 3: %s", names.segment[2]);
  printf("\nServer: just got segment 3 size: %d", numbs.seg3[0]);


  //calculation of memory addresses
  numbs.segsum[0]=0;
  numbs.segsum[1]= numbs.seg1[0]-1;
  numbs.segsum[2]= numbs.seg1[0];
  numbs.segsum[3]= numbs.segsum[2]+numbs.seg2[0]-1;
  numbs.segsum[4]= numbs.segsum[3]+1;
  numbs.segsum[5]= numbs.segsum[4]+numbs.seg3[0];
  numbs.segsum[6]=0;


  write(fdb, &numbs, sizeof(struct probsiz));

  if (numbs.leftov[0] >=0) {

     printf("\nClient: address for segment 1 is: %d - %d", numbs.segsum[0], numbs.segsum[1]);
     printf("\nClient: address for segment 2 is: %d - %d", numbs.segsum[2], numbs.segsum[3]);
     printf("\nClient: address for segment 3 is: %d - %d", numbs.segsum[4], numbs.segsum[5]);
     printf("\nClient: leftover memory is: %d", numbs.leftov[0]);
     printf("\nall done!");
 }
 if (numbs.leftov[0]<=0)
 {
     printf("\nClient: segment size is over the capacity, please try again");
     printf("\nall done!\n");

 }

  if(finish == 1)
    printf("\nServer: This says I am ready to close ");
  close(fda);
  close(fdb);
  unlink("FIFO1");
  unlink("FIFO2");

}

0 个答案:

没有答案