我在我的程序中遇到一个问题,我需要从客户端到服务器带一个字符串,然后服务器将使用该字符串创建一个私有FIFO。但是,当我在客户端上提示时键入一个名称,然后尝试在服务器上继续时,它会出错:没有文件或目录存在。
另外,我不明白如何将整数从服务器移动到客户端,因为write()方法不起作用(似乎只适用于字符和字符串)。
客户机侧:
main (void)
{
int fda; // to write to server
int fdb; // to read response from server
int mem_req[1]; // buffer holds a #
char outchar[7]; // server puts string here
char pfifo_name[10];
memset(mem_req,0,1);
memset(outchar,0,7);
memset(pfifo_name,0,10);
printf("\nClient: Please enter a private FIFO name (less than 10 chars): ");
scanf("%s", &pfifo_name);
printf("\nClient: Please enter a memory request number: ");
scanf("%d", &mem_req);
if((fda=open("FIFO1", O_WRONLY))<0)
printf("cant open fifo to write");
if((fdb=open(pfifo_name, O_RDONLY))<0)
printf("cant open fifo to read");
write(fda, mem_req, 1);
write(fda, pfifo_name, 10);
printf("\nClient: Pfifo and memory request sent to server ");
read(fdb, outchar, 7);
printf("\nClient: received from server %s", outchar);
close(fda);
close(fdb);
printf ("\nall done!\n");
了Serverside:
main (void)
{
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 fr_size; // frame size for memory management
int temp_mem;
int n_frame = 0;
int o_frame = 0;
int n_byte = 0;
int o_byte = 0;
int fr_num = 0;
int byte_num;
char mem_req[1]; // buffer holds a character
char outchar[7]; // server puts string here
char pfifo_name[10]; //client provides server a private fifo name
memset(mem_req,0,1);
memset(outchar,0,7);
memset(pfifo_name,0,10);
printf("\nServer: Please enter a desired frame(page) size: ");
scanf("%d", &fr_size);
read(fda, pfifo_name, 10);
/* Create the fifos and open them */
if ((mkfifo("FIFO1",0666)<0 && errno != EEXIST))
{
perror("cant create FIFO1");
exit(-1);
}
if ((mkfifo(pfifo_name,0666)<0 && errno != EEXIST))
{
perror("cant create private fifo");
exit(-1);
}
if((fda=open("FIFO1", O_RDONLY))<0)
printf("cant open fifo to read");
if((fdb=open(pfifo_name, O_WRONLY))<0)
printf("cant open fifo to write");
printf("\nServer: just got memeory request: ,%d", mem_req[0]);
temp_mem = mem_req[0];
n_frame = temp_mem/fr_size; // number of frames required for job
n_byte = temp_mem - (n_frame * fr_size); // ending byte location
write(fdb, &o_frame, sizeof(o_frame)); // output frame start location
printf("\nServer: Frame start location sent");
write(fdb, &o_byte, sizeof(o_byte));
printf("\nServer: Byte start location sent");
write(fdb, &n_frame, sizeof(n_frame));
printf("\nServer: Frame end location sent");
write (fdb, &n_byte, sizeof(n_byte));
printf("\nServer: Byte end location sent");
finish=read(fda, pfifo_name, 10); //read the character
当编译器在服务器上获得mkfifo(pfifo_name,0666)&lt; 0时,它会出错:
“无法创建私有fifo:不存在此类文件或目录”
我也从服务器上的所有4个写语句中收到错误(试图通过管道传递一个整数但它不起作用)
答案 0 :(得分:2)
在服务器中:
read(fda, pfifo_name, 10);
这会失败,因为fda
不包含有效的文件描述符,因为它从未初始化。
因此,没有任何内容被读入pfifo_name
,因此它只保留0个字节(因为之前调用memset
),即空字符串。然后将其传递给open
,它会尝试打开一个空白名称的文件,该文件无效。