Write a program called PWriter that takes input from a file and places it one it
ID: 3940243 • Letter: W
Question
Write a program called PWriter that takes input from a file and places it one item at a time into a named pipe called "Mypiper". Write a second program called PReader that takes items from the named pipe "Mypiper" and sends them to the standard output for display; include a time tag on the output. Create a server process (program) which creates a socket named Mysocket and places some items (cake, turkey, and stuffing) into the socket file. Create a client process which requests a connection to Mysocket; show that you get a connection. Demonstrate that your client can request the items placed by the server into Mysocket and sort them.Explanation / Answer
Here I am sumitting code for Pwriter that will do the same as explained in the above question.
SERVER CODE::
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
#include<sortserver.h>
int main()
{
struct array arr;
int k;
socklen_t len;
int i,sockfd,new_fd,j,temp,min;
struct sockaddr_in servaddr;
pid_t pid;
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(23368);
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
listen(sockfd,5);
len=sizeof(servaddr);
new_fd=accept(sockfd,(struct sockaddr*)&servaddr,&len);
if(pid=fork()==0)
{
close(sockfd);
printf(" received data from client:");
recvfrom(new_fd,(char*)&arr,sizeof(arr),0,(struct sockaddr*)&servaddr,&len);
for(i=0;i<arr.size;i++)
puts(arr.str[i]);
if(arr.type==1)
{
arr=buble(arr,arr.size);
}
if(arr.type==2)
{
arr=selection(arr,arr.size);
}
if(arr.type==3)
{
arr=insertion(arr,arr.size);
}
printf(" hello!sorted elements are sent to client.");
sendto(new_fd,(char*)&arr,sizeof(arr),0,(struct sockaddr*)&servaddr,sizeof(servaddr));
close(new_fd);
exit(1);
}
close(new_fd);
close(sockfd);
}
CLIENT CODE ::
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<string.h>
#include<netinet/in.h>
#include<sortserver.h>
int main()
{
struct array arr;
socklen_t len;
int i,sockfd,new_fd;
//intialize socket struct ..
struct sockaddr_in servaddr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(23368);
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
len=sizeof(servaddr);
printf(" to server:");
printf(" enter size of array+1:");
scanf("%d",&arr.size);
printf(" enter %d elements:",arr.size-1);
for(i=0;i<arr.size;i++)
gets(arr.str[i]);
printf("1.Bubble Sort 2. Selection sort 3. Insertion sort ");
printf("enter ur choice");
scanf("%d",&arr.type);
sendto(sockfd,(char*)&arr,sizeof(arr),0,(struct sockaddr*)&servaddr,sizeof(servaddr));
recvfrom(sockfd,(char*)&arr,sizeof(arr),0,(struct sockaddr*)&servaddr,&len);
printf(" from server:");
if(arr.type==1)
{
printf("Sorting is done using bubble sort ");
}
if(arr.type==2)
{
printf("Sorting is done using Insertion sort ");
}
if(arr.type==3)
{
printf("Sorting is done using Selection sort ");
}
printf(" after sorting elements are: ");
for(i=0;i<arr.size;i++)
puts(arr.str[i]);
close(sockfd);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.