Question

In: Computer Science

Write a client program that writes a struct with a privateFIFO name (call it FIFO_XXXX, where...

Write a client program that writes a struct with a privateFIFO name (call it FIFO_XXXX, where XXXX is the pid that you got from the getpid( ) function) to the server. Have the server read the privateFIFO name and write a message back to the client. Read the message and print it on the client side. Take screenshots of the output from your client and server programs. Send the output to Blackboard. What I'm checking for on this homework is that the name got created in the client and passed correctly to the server and the server can respond to the client. Turn the background of your window white and increase the font size.

Write in C, Will be used on Putty.

Thank you.

Solutions

Expert Solution

client code can be written as:

#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>


int main (void)
{

struct values
{
    char privateFIFO[14];
    int intbuff;
}input;

int fda; // common FIFO to read to write to server
int fdb;      // Private FIFO to read from server
int clientID;
int retbuff;
char temp[14];

clientID = getpid();
strcpy(input.privateFIFO, "FIFO_");
sprintf(temp, "%d", clientID);
strcat(input.privateFIFO, temp);
printf("\nFIFO name is %s", input.privateFIFO);

// Open common FIFO to write to server
if((fda=open("FIFO_to_server", O_WRONLY))<0)
     printf("cant open fifo to write");

write(fda, &input, sizeof(input));    // write the struct to the server
close(fda);


// Open private FIFO to read
if((fdb=open(input.privateFIFO, O_RDONLY))<0)
     read(fdb, &retbuff, sizeof(retbuff));

printf("\nAll done\n");

close(fdb);

}


server code can be written as:

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>


struct values
{
    char privateFIFO[14];
    int intbuff;
}input;

int main (void)
{

int fda; //common FIFO to read from client
int fdb; //private FIFO to write to client
int retbuff;
int output;
// create the common FIFO
if ((mkfifo("FIFO_to_server",0666)<0 && errno != EEXIST))
        {
        perror("cant create FIFO_to_server");
        exit(-1);
}

// open the common FIFO
if((fda=open("FIFO_to_server", O_RDONLY))<0)
     printf("cant open fifo to write");
output = read(fda, &input, sizeof(input));

// create the private FIFO
if ((mkfifo(input.privateFIFO, 0666)<0 && errno != EEXIST))
{
    perror("cant create privateFIFO_to_server");
    exit(-1);
}


printf("Private FIFO received from the client and sent back from server is: %d", output);

//open private FIFO to write to client
if((fdb=open(input.privateFIFO, O_WRONLY))<0)
    printf("cant open fifo to read");
write(fdb, &retbuff, sizeof(retbuff));


close(fda);
unlink("FIFO_to_server");
close(fdb);
unlink(input.privateFIFO);


}


Related Solutions

In JAVA Write a brief program that writes your name to a file in text format...
In JAVA Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
In Java language. Write a brief program that writes your name to a file in text...
In Java language. Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
C++ Goals: Write a program that works with binary files. Write a program that writes and...
C++ Goals: Write a program that works with binary files. Write a program that writes and reads arrays to and from binary files. Gain further experience with functions. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of file, a pointer to an int array, and the size of the array. The function should open the specified file in binary made, write the contents into the array, and then close the file. write another...
write a Program in C++ Using a structure (struct) for a timeType, create a program to...
write a Program in C++ Using a structure (struct) for a timeType, create a program to read in 2 times into structures, and call the method addTime, in the format: t3 = addTime(t1, t2); Make sure to use add the code to reset and carry, when adding 2 times. Also, display the resultant time using a function: display(t3);
Write a program that gathers input from the user and writes the information out to a...
Write a program that gathers input from the user and writes the information out to a file (output.txt).   Your main method should gather the input, calculate the average, and write the output. You should have a separate method that writes the output to a file. You can have other methods as well if you choose. However, you MUST have at least one other method in addition to the main method. Inputs: Student Number Name Class name Grades 1-5 (5 individual...
Write a program that declares a struct to store the data of a football player (player’s...
Write a program that declares a struct to store the data of a football player (player’s name, player’s position, number of touchdowns, number of catches, number of passing yards, number of receiving yards, and the number of rushing yards). Declare an array of 10 components to store the data of 10 football players. Your program must contain a function to input data and a function to output data. Add functions to search the array to find the index of a...
Write a c program that creates a struct to be able to read a .img file...
Write a c program that creates a struct to be able to read a .img file and a .pat file.
Write a C program with a struct that contains a string of 12 characters and two...
Write a C program with a struct that contains a string of 12 characters and two integers: number1 and number2. Read all 3 values in from the keyboard using scanf. Print the string and the sum of the two integers using the fully qualified struct names.
Write a php program that writes numbers to a file. The file type should be .txt...
Write a php program that writes numbers to a file. The file type should be .txt and the file name should be numbers.tx. The numbers.txt should contain 10 random integers between 1 to 100 after the file is written.
Write a program in C language 1- Define a struct for students with the aforementioned attributes,...
Write a program in C language 1- Define a struct for students with the aforementioned attributes, test it by populating one initialized struct variable with arbitrary input and take screenshots of the output. 2- For each student, struct add an array of number grades for a class the students are enrolled in such as S E 185. Then write functions which find the max, average, and minimum score for a specified assignment identified by a number, for example, Assignment 0...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT