Concept explainers
//Server.c
#include "csapp.h"
void *thread(void *vargp);
int main(int argc, char *argv[])
{
int listenfd;
socklen_t clientlen;
char client_hostname[MAXLINE], client_port[MAXLINE];
struct sockaddr_storage clientaddr;
pthread_t tid;
if (argc != 2)
{
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(0);
}
listenfd = Open_listenfd(argv[1]);
while (1)
{
clientlen = sizeof(struct sockaddr_storage);
int *connfdp = Malloc(sizeof(int));
*connfdp = Accept(listenfd, (SA *)&clientaddr, &clientlen);
Getnameinfo((SA *)&clientaddr, clientlen,
client_hostname, MAXLINE,
client_port, MAXLINE, 0);
printf("Connected to (%s, %s)\n", client_hostname, client_port);
Pthread_create(&tid, NULL, thread, connfdp);
}
}
void *thread(void *vargp)
{
int connfd = *((int *)vargp);
Pthread_detach(pthread_self());
Free(vargp);
size_t n;
rio_t rio;
int option;
char firstName[256];
char lastName[256];
char filefirstName[256];
char filelastName[256];
int age;
char major[256];
char buffer[256];
Rio_readinitb(&rio, connfd);
while (1)
{
bzero(buffer, 256);
if ((n = Rio_readlineb(&rio, buffer, 255)) == 0)
{
printf("Connection closed\n");
break;
}
option = atoi(buffer);
if (option == 1)
{
bzero(firstName, 256);
n = rio_readlineb(&rio, firstName, 255);
if (n < 0)
{
perror("ERROR reading from socket\n");
exit(1);
}
bzero(lastName, 256);
n = rio_readlineb(&rio, lastName, 255);
if (n < 0)
{
perror("ERROR reading from socket\n");
exit(1);
}
firstName[strlen(firstName) - 1] = '\0';
lastName[strlen(lastName) - 1] = '\0';
FILE *fptr;
int found = 0;
fptr = fopen("studentRecords.txt", "r");
if (fptr == NULL)
{
printf("Error opening file!\n");
exit(1);
}
bzero(buffer, 256);
while (fscanf(fptr, "%[^,],%[^,],%d,%[^\n]\n", filefirstName, filelastName, &age, major) != EOF)
{
if (strcmp(firstName, filefirstName) == 0 && strcmp(lastName, filelastName) == 0)
{
found = 1;
sprintf(buffer, "%s,%s,%d,%s\n", firstName, lastName, age, major);
n = rio_writen(connfd, buffer, strlen(buffer));
if (n < 0)
{
perror("ERROR writing to socket\n");
exit(1);
}
}
}
fclose
(fptr);
if (!found)
{
bzero(buffer, 256);
sprintf(buffer, "No record found!!\n");
n = rio_writen(connfd, buffer, strlen(buffer));
if (n < 0)
{
perror("ERROR writing to socket\n");
exit(1);
}
}
bzero(buffer, 256);
sprintf(buffer, "\n");
n = rio_writen(connfd, buffer, strlen(buffer));
if (n < 0)
{
perror("ERROR writing to socket\n");
exit(1);
}
}
else if (option == 2)
{
// Close the connection
Close(connfd);
break;
}
}
return NULL;
}
//client.c
#include "csapp.h"
int main(int argc, char *argv[])
{
int sockfd;
ssize_t n;
rio_t rio;
char buffer[256];
int option;
char firstName[256];
char lastName[256];
char major[256];
if (argc < 3)
{
fprintf(stderr, "usage %s hostname port\n", argv[0]);
exit(0);
}
sockfd = Open_clientfd(argv[1], argv[2]);
rio_readinitb(&rio, sockfd);
while (1)
{
printf("\n(1) Search record\n(2) Terminate\nSelect an option [1 or 2]: ");
bzero(buffer, 256);
if (fgets(buffer, 255, stdin) == NULL)
{
perror("ERROR reading value");
exit(1);
}
option = atoi(buffer);
n = rio_writen(sockfd, buffer, strlen(buffer));
if (n < 0)
{
perror("ERROR writing to socket");
exit(1);
}
if (option == 1)
{
printf("Enter first name: ");
bzero(firstName, 256);
if (fgets(firstName, 255, stdin) == NULL)
{
perror("ERROR reading value");
exit(1);
}
printf("Enter last name: ");
bzero(lastName, 256);
if (fgets(lastName, 255, stdin) == NULL)
{
perror("ERROR reading value");
exit(1);
}
n = rio_writen(sockfd, firstName, strlen(firstName));
if (n < 0)
{
perror("ERROR writing to socket");
exit(1);
}
n = rio_writen(sockfd, lastName, strlen(lastName));
if (n < 0)
{
perror("ERROR writing to socket");
exit(1);
}
bzero(buffer, 256);
printf("Message from server:\n");
while ((n = rio_readlineb(&rio, buffer, 255)) != 0)
{
if (strlen(buffer) == 1)
{
break;
}
buffer[strlen(buffer) - 1] = '\0';
printf("%s \n", buffer);
if (strcmp(buffer, "No record found!!") == 0)
{
break;
}
}
if (n < 0)
{
perror("ERROR reading from socket\n");
exit(1);
}
}
else if (option == 2)
{
close(sockfd);
printf("Connection Closed!!\n");
break;
}
}
exit(0);
}
Can you help me demonstrate of these two files?
as well as a problem solving approach, discussion of data structures,
Step by stepSolved in 3 steps
- ASP.NET MVC C# VS 2019Can you please help me with the syntax error? I was trying to display the calendar on the top of the page but I made an error. @model List<AdventureDB.Models.CalendarItem> @using System@using System.Web.Mvc @{var weeks = (int)Math.Ceiling((double)(Model.Count + 13) / 7);var startDate = Model.Count > 0 ? Model[0].Date : DateTime.Today; var endDate = startDate.AddYears(4);} @using (Html.BeginForm("Index", "Home", FormMethod.Get)){<label for="startDate">Training Start Date:</label><input type="date" id="startDate" name="startDate" /><input type="submit" value="Submit" />} @model IEnumerable<AdventureDB.Models.Training_Program>@using AdventureDB.Models@{// Create an instance of the data contextvar dbIEFW = new IEFWResourceDataContext();var dbIFE = new IFEResourcesDataContext();var dbREF = new REFDataContext();var dbAQC = new AQCDataContext();var dbAQCUSAF = new AQCUSAFDataContext();var dbIPC = new IPCDataContext();var dbIFTR =…arrow_forwardWhat are the pid values?arrow_forwardC++ What value is stored in num3 after the following pseudocode, where num1, num2, and num3 are integer variables and aQueue is a standard queue? num1 = 5 num2 = 1 num3 = 4aQueue.enqueue(num2) aQueue.enqueue(num3) aQueue.dequeue() aQueue.enqueue(num1 - num2) num1 = aQueue.peek() aQueue.dequeue() num2 = aQueue.peek() aQueue.dequeue()arrow_forward
- Complete the following code. The goal is to implement the producer-consumer problem. You are expected to extend the provided C code to synchronize the thread operations consumer() and producer() such that an underflow and overflow of the queue is prevented. You are not allowed to change the code for implementing the queue operations, that is the code between lines 25 and 126 as shown in the screenshot. You must complete the missing parts as shown in the screenshot as well as complete the missing codes of producer and consumer. #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <time.h> #include <pthread.h> #include <semaphore.h> #include <errno.h> #include <fcntl.h> #define MAX_LENGTH_CAP 100 #define INIT -127 #define UNDERFLOW (0x80 + 0x02) #define OVERFLOW 0x80 + 0x01 #define BADPTR (0x80 + 0x03) #define CONSUMER_TERMINATION_PROBABILITY 40 #define PRODUCER_TERMINATION_PROBABILITY 30 // ============= LOCKED…arrow_forwardComplete the following code. The goal is to implement the producer-consumer problem. You are expected to extend the provided C code to synchronize the thread operations consumer() and producer() such that an underflow and overflow of the queue is prevented. You are not allowed to change the code for implementing the queue operations, that is the code between lines 25 and 126 as shown in the Figure below. You must complete the missing parts between lines 226-261 as shown in the screenshot.arrow_forwardBackground Often, data are stored in a very compact but not human-friendly way. Think of how dishes in a menu can be stored in a restaurant database somewhere in the cloud. One way a dish object can be stored is this: { "name": "Margherita", "calories": 800, "price": 18.90, "is_vegetarian": "yes", "spicy_level": 2 } Obviously, this format is not user-readable, so the restaurant's frontend team produced many lines of code to render and display this content in a way that's more understandable to us, the restaurant users. We have a mini-version of the same task coming up. Given a list of similar complex objects (represented by Python dictionaries), display them in a user-friendly way that we'll define in these instructions. What is a "dish"? In this project, one dish item is a dictionary object that is guaranteed to have the following keys: "name": a string that stores the dish's name. "calories": an integer representing the calorie intake for one serving of the dish. "price": a float to…arrow_forward
- Programming Languages Pragmatics, 4th Editionarrow_forwardLab Activities: Exercise 1: 1) Write a thread class TabPrinter that prints the elements of an array of integers (in one line) every 2 seconds 5 times. Use the way of extending the class thread. Write the main method which creates and starts three threads Printer which will print different arrays of integers. After that it prints "Main won't wait. Main exits". 2) Modify the above thread program so that you implement the interface Runnable. Make the main thread waiting till all other threads finish execution.arrow_forward#SumCalculator Class: Create a class named SumCalculator that extends the Thread class. This class calculates the sum of numbers within a provided range. Declare three instance variables: start, end, and sum in the SumCalculator class . start and end are the lower and upper bounds of the range, respectively, while sum stores the sum of the numbers within the range. Create a constructor that takes in two parameters: start and end, and initializes the corresponding instance variables. The sum variable should be initialized to 0. Define the run method to calculate the sum of the numbers within the range. Inside of the run method use a for-loop to iterate over the numbers within the range and add each number to the sum variable. Define the accessor method getSum that returns the value of the sum variable. #Main Class: Create a class named Main that contains the main method, the entry point of the program. Create two objects of the SumCalculator class , with the first…arrow_forward
- Simple JAVA linkedlist code implementation please help and complete any part you can - Without using the java collections interface (ie do not import java.util.List,LinkedList, Stack, Queue...)- Create an implementation of LinkedList interface- For the implementation create a tester to verify the implementation of thatdata structure performs as expected Build Bus Route – Linked List- Your task is to:o Implement the LinkedList interface (fill out the implementation shell)o Put your implementation through its paces by exercising each of themethods in the test harnesso Create a client (a class with a main) ‘BusClient’ which builds a busroute by performing the following operations on your linked list:o§ Create (insert) 4 stations§ List the stations§ Check if a station is in the list (print result)• Check for a station that exists, and onethat doesn’t§ Remove a station§ List the stations§ Add a station before another station§ List the stations§ Add a station after another station§ Print the…arrow_forwardcorrect answer pleasearrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education