Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

//client.c
#include "csapp.h"

int main(int argc, char **argv)
{
    int connfd;
    rio_t rio;
    connfd = Open_clientfd(argv[1], argv[2]);
    Rio_readinitb(&rio, connfd);
    char buffer[MAXLINE];
    printf("Your balance is %s\n", buffer);

    Close(connfd);
    exit(0);

    int choice;
    float amount;
    int acc2;
    char buffer[MAXLINE];
    rio_t rio;

    Rio_readinitb(&rio, connfd);

    while (1)
    {
        printf("\nHello welcome to the Bank Management System\n");
        printf("----------------------\n");
        printf("1. Check Balance\n");
        printf("2. Deposit\n");
        printf("3. Withdraw\n");
        printf("4. Transfer\n");
        printf("5. Quit\n");
        printf("Enter your choice: ");

        Rio_writen(connfd, &choice, sizeof(int));

        if (choice == 1)
        {
            printf("Your balance is %s\n", buffer);
        }

        else if (choice == 2)
        {
            printf("Enter amount to deposit: ");

            Rio_writen(connfd, &amount, sizeof(float));

            printf("%s\n", buffer);
        }

        else if (choice == 3)
        {
            printf("Enter amount to withdraw: ");

            Rio_writen(connfd, &amount, sizeof(float));

            printf("%s\n", buffer);
        }

        else if (choice == 4)
        {
            printf("Enter amount to transfer: ");

            printf("Enter recipient account number: ");
            Rio_writen(connfd, &amount, sizeof(float));
            Rio_writen(connfd, &acc2, sizeof(int));

            printf("%s\n", buffer);

            printf("Recipient's new balance is %s\n", buffer);
        }

        else if (choice == 5)
        {
            Close(connfd);
            printf("Connection Closed!!\n");
            break;
        }
    }
    exit(0);
}

//server.c
#include "csapp.h"

void *thread(void *vargp);

int main(int argc, char **argv)
{
    int listenfd, *connfdp;
    socklen_t clientlen;
    struct sockaddr_storage clientaddr;
    pthread_t tid;

    listenfd = Open_listenfd(argv[1]);

    while (1)
    {
        clientlen = sizeof(struct sockaddr_storage);
        connfdp = Malloc(sizeof(int));
        *connfdp = Accept(listenfd, (SA *)&clientaddr, &clientlen);

        Pthread_create(&tid, NULL, thread, connfdp);
    }
}

void *thread(void *vargp)
{
    int connfd = *((int *)vargp);
    Pthread_detach(pthread_self());
    Free(vargp);

    FILE *fp = fopen("bank.txt", "r");

    char buf[MAXLINE];
    int account_number, balance;
    char name[MAXLINE];
    rio_t rio;
    
    while (fgets(buf, MAXLINE, fp) != NULL)
    {
        sscanf(buf, "%d %s %d", &account_number, name, &balance);
    }

    fclose(fp);


    sprintf(buf, "%d", balance);
    Rio_writen(connfd, buf, strlen(buf));   

    while (1)
    {
        if (Rio_readlineb(&rio, buf, MAXLINE) <= 0)
        {
            break;
        }

        int choice = buf[0] - '0';

        if (choice == 1)
        {
            sprintf(buf, "%d\n", balance);
            Rio_writen(connfd, buf, strlen(buf));
        }
        else if (choice == 2)
        {
            if (Rio_readlineb(&rio, buf, MAXLINE) <= 0)
            {
                break;
            }

            int deposit_amount = atoi(buf);

            balance += deposit_amount;
            sprintf(buf, "Deposit successful. New balance: %d\n", balance);
            Rio_writen(connfd, buf, strlen(buf));
        }
        else if (choice == 3)
        {           

            if (Rio_readlineb(&rio, buf, MAXLINE) <= 0)
            {
                break;
            }

            int withdrawal_amount = atoi(buf);           

            if (balance >= withdrawal_amount)
            {
                balance -= withdrawal_amount;
                sprintf(buf, "Withdrawal successful. New balance: %d\n", balance);
                Rio_writen(connfd, buf, strlen(buf));
            }
        }
        else if (choice == 4)
        {
            if (Rio_readlineb(&rio, buf, MAXLINE) <= 0)
            {
                break;
            }

            int transfer_amount = atoi(buf);

            if (Rio_readlineb(&rio, buf, MAXLINE) <= 0)
            {
                break;
            }

            int destination_account = atoi(buf);           

            if (balance >= transfer_amount)
            {
                balance -= transfer_amount;
                sprintf(buf, "Transfer successful. New balance: %d\n", balance);
                Rio_writen(connfd, buf, strlen(buf));
            }
        }
        else if (choice == 5)
        {
            Close(connfd);
            break;
        }
    }

    return NULL;
}


//bank.txt
100 John Smith 500.00
200 Jane Doe 1000.00
300 Luke Skylar 1500.00

Can you help me make a connection with the client and server and be able to read the text file?

 

 

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education