C语言,使用结构从客户端向服务器发送数据

时间:2016-09-18 19:19:34

标签: c struct server client

我必须创建一个客户端和服务器,客户端将发送一个字符和一个整数(使用结构),然后我的客户端将迭代字母n(整数)次,并将其发送回客户端。 / p>

例如:a和4从客户端发送,服务器将操作并将4 a(aaaa)的字符串发送回客户端。

我知道我非常接近获取代码但是一旦我发送了我的char和int,我得到了一个"分段错误(核心转储)错误"。我完成了我的研究,错误显示是由于缺少强制转换,访问的内存不应该访问,或者指针可能为空。

代码如下:

客户端:

// ClientTest.c
// opens fifo1 for writing and fifo2 for reading


#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 problem {        
    char letter[1];
    int number[1];
};
main (void)
{
  struct problem x=0; //Initializing structure to null
  int fda;  // to write to character server
  int fdb;  // to read response from character server
  int i;    // for the iteration


  printf("Client: Please enter a character: ");
  scanf("%c", &x.letter[0]); 
  printf("Client: Please enter an integer: ");
  scanf("%d", &x.number[0]);  

  memset(&x.letter, 0, 1);
  memset(&x.number, 0, 1);  


  if((fda=open("FIFO1", O_WRONLY))<0)//opening and validating fifos
     printf("cant open fifo to write");

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

  write(fda, x.number, 1);
  printf("\nClient: Got the integer sent, now waiting for response ");
  //sleep(0.250);
  write(fda, x.letter, 1);
  printf("\nClient: Got the character sent, now waiting for response ");

  char outletter[7];

  read(fdb, outletter, 7);
  printf("\nClient: received characters from server %c", outletter);

  close(fda);
  close(fdb);


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

}

服务器

// ServerTest.c
// makes 2 fifos named fifo1 and fifo2
// opens fifo1 for reading and fifo2 for writing

#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 problem {
    char letter[1]; // struct to store the character
    int number [1]; // struct to store the integer
    };

main (void)
{
  struct problem x;
  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 p;
  char outletter[7];

  memset(&x.letter, 0, 7);
  memset(&x.number, 0, 1);


  /* 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, x.letter, 1); //read the character
  read(fda, x.number, 1);   //read the integer


  printf("\nServer: just got character: , %c", x.letter[0]);
  printf("\nServer: just got integer: , %d", x.number[0]);

  p=x.number[0]-'0';


  for( i = 0; i<=p; i++)                // iteration to create the character's string
    outletter[p] = x.letter[0];


  printf("iteration: %d and character: %c\n", i, outletter[0]); // validating the character and integer received
  printf("\nServer: outchar is, %s", outletter); // this shows the character to be sent back to client

  write(fdb, outletter, p); 
  printf("\nServer: Got the characters sent: %s", outletter ); // this sends the letter back to client



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

1 个答案:

答案 0 :(得分:1)

您的代码存在许多问题,我已经让它运行但您仍然可以改进它。

客户端:

// ClientTest.c
// opens fifo1 for writing and fifo2 for reading


#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 problem {        
    char letter[1];
    int number[1];
};
main (void)
{
  struct problem x; 
  int fda;  // to write to character server
  int fdb;  // to read response from character server
  int i;    // for the iteration

  memset(&x, 0, sizeof(struct problem)) ;


  printf("Client: Please enter a character: ");
  scanf("%c", x.letter); 
  printf("Client: Please enter an integer: ");
  scanf("%d", x.number);  

// removed or else the letter and number would be reverted to zero
 // memset(&x.letter, 0, sizeof(char));
//  memset(&x.number, 0, sizeof(int));  


  if((fda=open("FIFO1", O_WRONLY))<0)//opening and validating fifos
     printf("cant open fifo to write");

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

  write(fda, x.letter, sizeof(char));
  printf("\nClient: Got the character sent, now waiting for response ");

  write(fda, x.number, sizeof(int));
  printf("\nClient: Got the integer sent, now waiting for response ");
  //sleep(0.250);

  char outletter[7];

  read(fdb, outletter, 7);
  printf("\nClient: received characters from server:") ;
  printf("%s\n", outletter);

  close(fda);
  close(fdb);


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

}

服务器:

// ServerTest.c
// makes 2 fifos named fifo1 and fifo2
// opens fifo1 for reading and fifo2 for writing

#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 problem {
    char letter[1]; // struct to store the character
    int number[1]; // struct to store the integer
    };

main (void)
{
  struct problem x;
  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 p;
  char outletter[7];

  memset(x.letter, 0, sizeof(char));
  memset(x.number, 0, sizeof(int));


  /* 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, x.letter, sizeof(char)); //read the character
  read(fda, x.number, sizeof(int));   //read the integer


  printf("\nServer: just got character: , %c", x.letter[0]);
  printf("\nServer: just got integer: , %d", x.number[0]);

  p=x.number[0];



  if (p > 6) p = 6;  // Cannot write more than 6 characters in outletter



  for( i = 0; i<=p; i++) {               // iteration to create the character's string
    outletter[i] = x.letter[0];
    printf("iteration: %d and character: %c\n", i, outletter[i]); // validating the character and integer received
  }
  outletter[p] = '\0'; // the string must finish with '\0'


  printf("\nServer: outchar is, %s", outletter); // this shows the character to be sent back to client

  write(fdb, outletter, p); 
  printf("\nServer: Got the characters sent: %s", outletter ); // this sends the letter back to client



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