当前位置: 动力学知识库 > 问答 > 编程问答 >

Simple Client/Server C program not correctly communicating

问题描述:

I'm trying to set up a client/server program in C and have everything compiling but I can't get it to output correctly. Currently, it only outputs my printf statements and not the actual string inputted in the client. It's supposed to have the client take in three strings and send them to the server, which checks to see if they're all lowercase characters. If not, output an invalid string message. If so, count the number of vowels, and send all results to the client. It looks correct to me but I don't get why it isn't working.

Client code:

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

{

int l[1]; // stores number of vowels

int m[1]; // stores number of vowels

int n[1]; // stores number of vowels

char charbuff1[6]; // buffer holds a character

char outchar1[7]; // server puts string here

char charbuff2[6]; // buffer holds a character

char outchar2[7]; // server puts string here

char charbuff3[6]; // buffer holds a character

char outchar3[7]; // server puts string here

}

inst;

main (void)

{

memset(inst.charbuff1,0,6);

memset(inst.outchar1,0,7);

memset(inst.charbuff2,0,6);

memset(inst.outchar2,0,7);

memset(inst.charbuff3,0,6);

memset(inst.outchar3,0,7);

int fda; // to write to server

int fdb; // to read response from server

struct Client;

int i;

if((fda=open("FIFO1", O_WRONLY))<0) //open fifo to write to server

printf("cant open fifo to write");

if((fdb=open("FIFO2", O_RDONLY))<0) //open fifo to read from server

printf("cant open fifo to read");

printf("Client: Please enter up to 6 lowercase characters: ");

for(i=0;i<6;i++) //for loop to take the first input

{

scanf("%c", &inst.charbuff1[i]);

}

printf("Client: Please enter up to 6 lowercase characters: ");

for(i=0;i<6;i++) //for loop to take the second input

{

scanf("%c", &inst.charbuff2[i]);

}

printf("Client: Please enter up to 6 lowercase characters: ");

for(i=0;i<6;i++) //for loop to take the third input

{

scanf("%c", &inst.charbuff3[i]);

}

write(fda, &inst, sizeof(inst)); //write results to the struct

printf("\nClient: Got the characters sent, now waiting for response ");

read(fdb, &inst, sizeof(inst)); //read results from server struct

printf("\nClient: received from server %s", inst.outchar1);

printf("\nClient: Was this a valid string? ");

if(inst.l[1] = 9) //check if first string had an invalid character

{

printf("No");

}

else

{

printf("Yes");

printf("\nClient: The number of vowels in the string is: %d", inst.l[1]);

}

printf("\nClient: The number of vowels in the string is: %d", inst.l[1]);

printf("\nClient: received from server %s", inst.outchar2);

printf("\nClient: Was this a valid string? ");

if(inst.m[1] = 9) //check if second string had an invalid character

{

printf("No");

}

else

{

printf("Yes");

printf("\nClient: The number of vowels in the string is: %d", inst.m[1]);

}

printf("\nClient: received from server %s", inst.outchar3);

printf("\nClient: Was this a valid string? ");

if(inst.n[1] = 9) //check if third string had an invalid character

{

printf("No");

}

else

{

printf("Yes");

printf("\nClient: The number of vowels in the string is: %d", inst.n[1]);

}

close(fda);

close(fdb);

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

}

Server code:

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

{

int l[1]; // stores number of vowels

int m[1]; // stores number of vowels

int n[1]; // stores number of vowels

char charbuff1[6]; // buffer holds a character

char outchar1[7]; // server puts string here

char charbuff2[6]; // buffer holds a character

char outchar2[7]; // server puts string here

char charbuff3[6]; // buffer holds a character

char outchar3[7]; // server puts string here

}

inst;

main (void)

{

memset(inst.charbuff1,0,6);

memset(inst.outchar1,0,7);

memset(inst.charbuff2,0,6);

memset(inst.outchar2,0,7);

memset(inst.charbuff3,0,6);

memset(inst.outchar3,0,7);

struct Server;

int i;

int fda; // to read from client

int fdb; // to write to client

/* 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, &inst, sizeof(inst)); //read the struct from client

for( i = 0; i<6; i++) //for loop to write the first string from charbuff array to outchar array

{

inst.outchar1[i] = inst.charbuff1[i];

inst.outchar1[6] = 0;

}

printf("\nServer: first string is: %s", inst.outchar1);

inst.l[1] = 0;

for( i = 0; i<6; i++) //for loop to check for non-lowercase characters

{

if(inst.outchar1[i] < 'a' && inst.outchar1[i] > 'z') //if a character isn't lowercase, set l to 9

{

inst.l[1] = 9;

break;

}

else if(inst.outchar1[i] = 'a' || 'e' || 'i' || 'o' || 'u') //if a character is a vowel, increment l by 1

{

inst.l[1]++;

}

else //if a character is a lowercase consonant, do nothing

{

inst.l[1];

}

}

printf("\nServer: Checked for uppercase letters");

if(inst.l[1] = 9) //if an invalid character was encountered

{

printf("\nServer: There are uppercase letters");

}

else //if no invalid character was encountered

{

printf("\nServer: There are no uppercase letters");

printf("\nServer: The humber of vowels are: %d", inst.l[1]);

}

for( i = 0; i<6; i++) //for loop to write the second string from charbuff array to outchar array

{

inst.outchar2[i] = inst.charbuff2[i];

inst.outchar2[6] = 0;

}

printf("\nServer: second string is: %s", inst.outchar2);

inst.m[1] = 0;

for( i = 0; i<6; i++) //for loop to check for non-lowercase characters

{

if(inst.outchar1[i] < 'a' && inst.outchar1[i] > 'z') //if a character isn't lowercase, set m to 9

{

inst.m[1] = 9;

break;

}

else if(inst.outchar2[i] = 'a' || 'e' || 'i' || 'o' || 'u') //if a character is a vowel, increment m by 1

{

inst.m[1]++;

}

else //if a character is a lowercase consonant, do nothing

{

inst.m[1];

}

}

printf("\nServer: Checked for uppercase letters");

if(inst.m[1] = 9) //if an invalid character was encountered

{

printf("\nServer: There are uppercase letters");

}

else //if no invalid character was encountered

{

printf("\nServer: There are no uppercase letters");

printf("\nServer: The humber of vowels are: %d", inst.m[1]);

}

for( i = 0; i<6; i++) //for loop to write the third string from charbuff array to outchar array

{

inst.outchar3[i] = inst.charbuff3[i];

inst.outchar3[6] = 0;

}

printf("\nServer: third string is: %s", inst.outchar3);

inst.n[1] = 0;

for( i = 0; i<6; i++) //for loop to check for non-lowercase characters

{

if(inst.outchar1[i] < 'a' && inst.outchar1[i] > 'z') //if a character isn't lowercase, set n to 9

{

inst.n[1] = 9;

break;

}

else if(inst.outchar3[i] = 'a' || 'e' || 'i' || 'o' || 'u') //if a character is a vowel, increment n by 1

{

inst.n[1]++;

}

else

{

inst.n[1]; //if a character is a lowercase consonant, do nothing

}

}

printf("\nServer: Checked for uppercase letters");

if(inst.n[1] = 9) //if an invalid character was encountered

{

printf("\nServer: There are uppercase letters");

}

else //if no invalid character was encountered

{

printf("\nServer: There are no uppercase letters");

printf("\nServer: The humber of vowels are: %d", inst.n[1]);

}

write(fdb, &inst, sizeof(inst));

printf("\nServer: This says I am ready to close.\n ");

close(fda);

close(fdb);

unlink("FIFO1");

unlink("FIFO2");

return 0;

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: