Please help.
Getting Segmentation Fault when I run server.c in the background and client.c in the forground.
Oh, and reading from database where first line is name, second line is address and third line is phonenumber. And so on.
/* client.c */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>
#define FIFO_NAME "Port80"
#define PRIVATE "private"
main(){ // Will need to receive signal from server.
int fd[2], i, port;
char *pipename, name[80], query[80], *result;
// create named pipe
if (access(PRIVATE, F_OK) == -1) {
port = mkfifo(PRIVATE, 0777);
if (port != 0) {
fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME);
exit(0);
}
}
pipename = PRIVATE;
printf("pipename contains: %s\n", pipename);
// Read from user
// 1. person to search.
// 2. queiry type.
printf("Input name: ");
scanf("%s", &name);
printf("Search options: 'a' for address and 'p' for phone number.\n");
for(i =0; i<10; i++){
printf("Input search type: ");
scanf("%s", &query);
if(strcmp(query,"a")==0 || strcmp(query,"p")==0){
printf("Thank you. Search underway.\n");
break;
}
printf("Search options are 'a' for address. 'p' for phone number.\n");
}
printf("Name is %s with search type -%s. \n", name, query);
// Write to well known pipe(FIFO)
// 1. pipe number
// 2. person to search.
// 3. queiry type.
if (access(FIFO_NAME, F_OK) == -1) {
port = mkfifo(FIFO_NAME, 0777);
if (port != 0) {
fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME);
exit(0);
}
}
port = open(FIFO_NAME, O_WRONLY);
write(port, pipename, 80);
write(port, name, 80);
write(port, query, 2);
close(port);
// Read from private pipe
port = open(pipename, O_RDONLY);
i = read(port, result, 80);
close(port);
printf("result contains: %s\n", result);
printf("i contains: %i\n", i);
if(strcmp(result,"Person not found")==0 ){
printf("Person was not found!\n");
}
else if(strcmp(query, "a")==0){
printf("%s's address is: %s", name, result);
}
else if(strcmp(query, "p")==0){
printf("%s's phone number is: %s", name, result);
}
printf("ending\n");
exit(0);
}
/* Server.c */
// Mahad Ali
// 011088242
// Section 2
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>
#define FIFO_NAME "Port80"
#define FALSE 0
#define TRUE 1
void agent(char*, char*, char*); //do I need arguments or is void fine?
void searcher(char*, char*, char*); // Will also need to send a signal to client
main(){
char pipename[3], person_name[80], query[2], answer_to_query[80];
int port, i;
if (access(FIFO_NAME, F_OK) == -1) {
port = mkfifo(FIFO_NAME, 0777);
if (port != 0) {
fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME);
exit(0);
}
}
while(1){
// Read from Well known Pipe(FIFO)
port= open(FIFO_NAME, O_RDONLY);
system("clear");
//Will recieve 3 things
read(port, pipename, 80); //1. Name of named pipe(pipe)
read(port, person_name, 80); //2. Name of person
read(port, query, 2); //3. Type of query
close(port);
//i. 'a' for address
//ii. 'p' for phone number
printf("pipename contains: %s\n", pipename);
printf("person_name contains: %s\n", person_name);
printf("query contains: %s\n", query);
// When type of queiry is received
// call function agent.
// let it fork and take care of everything
// loop to see if there are anymore requests in port
agent(pipename, person_name, query);
}
}
void agent(char *pipename, char *person, char *query){
int status;
status = fork(); // Fork.
if(status == -1){
fprintf(stderr, "Error forking.\n");
exit(1);
}
if(status ==0){ // In child
// search database for person. call searcher.
searcher(pipename, person, query);
// searcher does the following:
// When found
//1. return to client address if query is 'a'
//2. return to client phone num if query is 'p'
// Not found
// return to client "Did not find person"
exit(0); //End child
}
}
// receives info and where to send result
void searcher(char *pipename, char *person, char *query)
{
FILE *ifp;
char name[80];
char address[80];
char phone[80];
int recordFound = FALSE;
int i, port;
// open file
ifp = fopen("database.txt", "r");
// error opening file
if (ifp == NULL)
{
fprintf(stderr, "Error opening file");
_exit(1);
}
port = open(pipename, O_WRONLY);
// search for user inputted name
while (fgets (name, 80, ifp) != NULL)
{
fgets(address, 80, ifp); // Gets address
fgets(phone, 80, ifp); // Gets phone
if (strncmp(person, name,strlen(name)-1) == 0)
{
recordFound = TRUE;
printf("found name in database\n");
// if name found then write appropriate result
if(strcmp(query,"a")==0){
i = write(port, address, 80);
printf("i contains: %i\n", i);
if(i==-1){
printf("Error writing address to client\n");
}
}
else if(strcmp(query,"p")==0){
i = write(port, phone, 80);
printf("i contains: %i\n", i);
if(i==-1){
printf("Error writing number to client\n");
}
}
}
}
// Write "not found" to client
if (recordFound == FALSE)
{
i=write(port, "Person not found", 80);
if(i==-1){
printf("Error writing to client\n");
}
printf("Did NOT find name in database\n"); // remove later
}
close(port);
recordFound = FALSE;
// close file
fclose(ifp);
}
Start Free Trial